Jump to content

Cant Insert into MySQL with a PDO Prepare Statement


alan_k
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...