Jump to content

Need help finding error in a mySQL INSERT statement


HuntsvilleMan

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.