Jump to content

Cant Insert into MySQL with a PDO Prepare Statement


Recommended Posts

I have a page that adds printer info into an Inventory tracking site. It passes form values to insert_printer.php via _$POST. I have used the following code before to insert a row with no problems and yet now it is not working. I have verified the php with a validator and that is correct. I put in a a print_r ($_POST) and the values are being passed to the insert page ok. I have looked at this thing for hours and nothing is popping out. Perhaps a new set of eyes will see it right away:

$stmt = $link->prepare("INSERT INTO Printer_Inventory SET Printer_Name = :Printer_Name, Physical_Loc = :Physical_Loc, Static_IP_Addr = :Static_IP_Addr,Printer_Model = :Printer_Model, Black_Toner_Cart_Num = :Black_Toner_Cart_Num, Blk_Toner_Changed = :Blk_Toner_Changed,Cyan_Toner_Cart_Num = :Cyan_Toner_Cart_Num, Cyan_Toner_Changed = :Cyan_Toner_Changed,Mgnt_Toner_Cart_Num = :Mgnt_Toner_Cart_Num, Mgnt_Toner_Changed = :Mgnt_Toner_Changed,Yllw_Toner_Cart_Num = :Yllw_Toner_Cart_Num, Yllw_Toner_Changed = :Yllw_Toner_Changed");$stmt->execute(array(':Printer_Name' => $Printer_Name,':Physical_Loc' => $Physical_Loc,':Static_IP_Addr' => $Static_IP_Addr,':Printer_Model' => $Printer_Model,':Black_Toner_Cart_Num' => $Black_Toner_Cart_Num,':Blk_Toner_Changed' => $Blk_Toner_Changed,':Cyan_Toner_Cart_Num' => $Cyan_Toner_Cart_Num,':Cyan_Toner_Changed' => $Cyan_Toner_Changed,':Mgnt_Toner_Cart_Num' => $Mgnt_Toner_Cart_Num,':Mgnt_Toner_Changed' => $Mgnt_Toner_Changed,':Yllw_Toner_Cart_Num' => $Yllw_Toner_Cart_Num,':Yllw_Toner_Changed' => $Yllw_Toner_Changed));

This works ok manually from phpMyAdmin:

INSERT INTO Printer_Inventory (`Printer_Name`,`Physical_Loc`,`Static_IP_Addr`,`Printer_Model`) VALUES ('HP','SHS','172.16.xxx.xxx','HP 3102');

Get an error message:

I am correct in assuming I dont have to account for the primary key id field? I never had to before. I imported a .csv file to the database ok.

Attached is the SQL structure.

Printer_Inventory.txt

Edited by alan_k
Link to post
Share on other sites

The query you posted on bottom has a syntax error because there is a single quote after the Printer_Model field name instead of a backtick.

I am correct in assuming I dont have to account for the primary key id field?

If it is an auto-increment field then you can omit it from the query and it will take the next available value.By default PDO ignores database errors. Did you change that to have it throw an exception or something else to tell you if something went wrong?
Link to post
Share on other sites

This was the trouble:

Black_Toner_Cart_Num = :Black_Toner_Cart_Num

Table was Blk_Toner_Cart_Num. Is there anyway to find these MySQL error messages like you can with PHP?

Edited by alan_k
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...