Languages :: PHP :: query not executing in mysql |
|||
| By: irish_paddy |
Date: 20/03/2003 00:00:00 |
Points: 30 | Status: Answered Quality : Excellent |
|
Hi below are is a php file that inserts information into a table in a mysql database. It seems to be working (no error messages are generated) but when i do a select statement in the mysql database the query in the php file does'nt seem to be executed. Any help is much appreciated. Cheers ----------------------php file (submitform.php)----------------------- <html> <body> <?php if ((isset($_POST["traintime"])) &&(isset($_POST["traintype"])) &&(trim($_POST["traintime"])!='') &&(trim($_POST["traintype"])!='')): $traintime=addslashes($_POST["traintime"]); $traintype=addslashes($_POST["traintype"]); $db = mysql_connect ("localhost"); mysql_select_db ("mysql" ,$db); $sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ($traintime, $traintype)"; $result = mysql_query($sql); printf("<tr><td>%s\n", $traintime); print (" "); printf("<tr><td>%s\n", $traintype); else: print ("Error in post data"); endif; ?> </body> </html> ---------------------------html file ------------------ <html> <head> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!-- function sendform() { if ((document.form.traintime.value!='')&&(document.form.traintype.value!='')) { document.form.submit(); } else { alert("You must enter a first and last name"); } } --> </SCRIPT> </head> <body> <form name="form" action="submitform.php" method="post"> First Name: <input type="text" name="traintime" size="25" maxlength="25"> Last Name: <input type="text" name="traintype" size="25" maxlength="25"> <p> <input type="button" onClick="javascript:sendform()"> </form> </body> </html> |
|||
| By: sumotimor | Date: 20/03/2003 09:03:00 | Type : Comment |
|
| Hi I'll start with a personnal advice : Don't mess around in the mysql database. I suggest that you create another database where to stock you data, and don't mess with the database that mysql is using to maintain is system. ^_^ As for your problem, if you get no error message i don't know what it could be, try to echo your $sql Another thing This printf("<tr><td>%s\n", $traintime); print (" "); printf("<tr><td>%s\n", $traintype); should be printf("<table><tr><td>%s</td></tr>\n", $traintime); printf("<tr><td>%s</td></tr></table>\n", $traintype); |
|||
| By: VGR | Date: 20/03/2003 09:27:00 | Type : Answer |
|
| agree 3000% with the above. Create yourself a database 8-) At least, you know that the 'mysql' one is connectable all the time :D Let me review your code : <?php if ((isset($_POST["traintime"])) &&(isset($_POST["traintype"])) &&(trim($_POST["traintime"])!='') &&(trim($_POST["traintype"])!='')): // I suggest you use { and } and not : //here note that PHP does NOT do what Turbo-Pascal and Delphi do : partial Boolean evaluation (optimization). This means that the above will issue a NOTICE or WARNING (probably) about $_POST[] being not set and trimed anyway $traintime=addslashes($_POST["traintime"]); $traintype=addslashes($_POST["traintype"]); // so I guess that your magic_quoting is off or else you'll addslashes to already-addslashed values 8-) $db = mysql_connect ("localhost"); // please do add "or die("failed to connect ".mysql_error());" mysql_select_db ("mysql" ,$db); // please do add "or die("failed to select DB ".mysql_error());" // would be surprising given it's "mysql" DB $sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ($traintime, $traintype)"; $result = mysql_query($sql); // please do add "or die("bad query '$query' : ".mysql_error());" printf("<tr><td>%s\n", $traintime); // badly terminated table line construct, </td></tr> please print (" "); // useless in a TABLE (would print touside of it) and meaningless in HTML : white space is and CRLF is printf("<tr><td>%s\n", $traintype); else:// I suggest you use { and } and not : print ("Error in post data"); endif;// I suggest you use { and } and not : ?> |
|||
| By: VGR | Date: 20/03/2003 09:28:00 | Type : Comment |
|
| ALSO the most classical error of all : you forgot the QUOTES around values assed to the SQL. It's EVIL :D Your request should be : $sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ('$traintime', '$traintype')"; |
|||
| By: VGR | Date: 20/03/2003 09:30:00 | Type : Comment |
|
| the most classical and trivial errors are always the ones you see last 8-)) |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








