bluebyyou Posted February 13, 2007 Share Posted February 13, 2007 I can't figure out why the insert query wont work. I use the same connection function to run select queries on the DB. Can anyone see what the problem is? <?php //includes.php /* function dbcon stores the database connection information and the connection functions */ /* $result must be handled on each individual page. for example: $row = mysql_fetch_array($result) .. */ function dbcon($sql) { $host = "*******"; $user = "*********"; $password = "***********"; $database = "*********"; global $result; $connection = mysql_connect($host,$user,$password) or die ("couldnt connect to rockypages server."); $db = mysql_select_db($database, $connection) or die ("couldnt select rockypages database."); $result = mysql_query($sql) or die ("Couldn't execute query."); return $result; } /* This Function takes the numerical data from the PostingType Table and converts it to the corresponding text. */ /* $input is the $key field in the database */ function posting_type($input) { $switch = $input; switch ($switch) { case "1": $output = "Job"; break; case "2": $output = "For Sale"; break; case "3": $output = "Wanted"; break; case "4": $output = "Housing"; break; case "5": $output = "Services Offered"; break; case "6": $output = "Event"; break; } echo $output; } ?> <?php //insert.php include("includes.php"); if (isset($_POST['submit'])) { $today = date("Y-m-d"); $query = sprintf("INSERT INTO Posting (date,type,price,title,desc,email,reply) VALUES ('%s','%s','%s','%s','%s','%s','%s')", $today, $_POST['type'],$_POST['price'],$_POST['title'],$_POST['desc'], $_POST['email'],$_POST['reply']); dbcon($query); // <-- database connection function located on includes.php } else { ?> <html> <head> <title>Untitled Document</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> Posting Type<select name="type"> <option value="0">Select One..</option> <?php $query = "Select * FROM PostingType"; dbcon($query); while ($row = mysql_fetch_array($result)) { extract($row); ?> <option value="<?php echo $key; ?>"><?php echo $name; ?></option> <?php } ?> </select><br><br> Title<input name="title" type="text"><br> Price<input name="price" type="text"><br><br> Description<br> <textarea name="desc" cols="40" rows="4"></textarea><br><br> Email<input name="email" type="text"><br> Verify<input name="vemail" type="text"><br><br> <input name="reply" type="radio" value="0" checked>Reply to my E-Mail<br> <input name="reply" type="radio" value="1">Do not show my e-mail<br><br> Upload image (Coming Soon..)<br> <input name="h" type="text" disabled><input name="browse" type="button" disabled value="Browse..."><br><br> <input name="submit" type="submit" value="Submit"> </form> </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 13, 2007 Share Posted February 13, 2007 date and desc are reserved words in sql, your query would need to look more like.... INSERT INTO Posting (`date`,type,price,title,`desc`,email,reply).... Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 13, 2007 Author Share Posted February 13, 2007 I didn't know what they were reserved...I'll try that. Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 13, 2007 Author Share Posted February 13, 2007 I may have misunderstood, I added the single quotes, and had no effect, do I need to rename the fields in my DB to something other than date and desc? Also I have another field in the db "number" which is the primary key and it is auto incrementing. I thought I could leave it out, but is it possible that omitting that field is messing up the INSERT query? Quote Link to comment Share on other sites More sharing options...
trq Posted February 13, 2007 Share Posted February 13, 2007 They are back ticks ` not single quotes. Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 13, 2007 Author Share Posted February 13, 2007 That worked thank you. What exactly do the back ticks tell MySQL? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted February 13, 2007 Share Posted February 13, 2007 They are specific identifiers. It labels it as "variable". Basically atleast. They aren't generally required but in some circumstances as this they are. Quote Link to comment Share on other sites More sharing options...
trq Posted February 13, 2007 Share Posted February 13, 2007 I'm not sure how you'd put it exactly but it tells mysql that you want to use a reserved word where you should'nt really be using them. Your best off (if you can) to just avoid reserved words in the first place. Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 13, 2007 Author Share Posted February 13, 2007 I will make sure to look those up and avoid them in the future, thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.