HuntsvilleMan Posted June 10, 2011 Share Posted June 10, 2011 I need help with an INSERT statement. Had it working but then missed some point about how to do it right. The problem is the statement below. $query_status = mysql_query("INSERT INTO nneighbors (kp, pid, aid, mt, vt, ct, cid) VALUES ($kp, $pid, $aid, $mt, $vt, Now(), NULL)"); The test code I'm working with to focus on the problems is below. Really appreciate a suggestion about what I'm messing up. Thanks Mike <?php //this is a short test program that fails $kp = "1"; // key pressed $pid = 2; // person id $aid = 3; // appointment id $mt = "V"; // message type $vt = "D"; // visit type $myD = "rem"; //database $myT = "nne"; //table $myU = "off"; //username $myP = "dat"; //password /* save call completion information*/ $link_status = mysql_connect("mysql", $myU, $myP); if (!$link_status) echo "failed mysql_connect" . "<br />"; /* Insert the values into the table where cid is an AutoIncrement field*/ $database_status = mysql_select_db($myD); if (!$database_status) echo "failed mysql_select_db" . "<br />"; $query_status = mysql_query("INSERT INTO " . $myT . " (kp, pid, aid, mt, vt, ct, cid) VALUES ($kp, $pid, $aid, $mt, $vt, Now(), NULL)"); if (!$query_status) echo "failed mysql_query" . "<br />"; echo "finished"; ?> echo values: kp = 1 pid = 2 aid = 3 mt = V vt = D myD = rem myT = nne myU = off myP = dat failed mysql_query finished Table structure: kp char(1) pid int(11) aid int(11) mt char(1) vt char(1) ct char(19) (note - intentionally stored in charcter format to accomodate other programs that expect charcater data) cid int(11) auto increment Link to comment https://forums.phpfreaks.com/topic/238986-need-help-finding-error-in-a-mysql-insert-statement/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2011 Share Posted June 10, 2011 String (character) data must be enclosed in single-quotes so that it is treated as string data, instead of as a mysql keyword or identifier. If you were using mysql_error() as part of your error reporting logic, you would probably get an error something like - unknown column V, because mysql it treating the unquoted $mt data as a column name instead of a string. Link to comment https://forums.phpfreaks.com/topic/238986-need-help-finding-error-in-a-mysql-insert-statement/#findComment-1227983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.