Jump to content

For each() mysql_insert


tball1

Recommended Posts

Hello,

Can someone please tell what is wrong with this I keep getting SQL error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near" .  I can't seem to find what is wrong with it. 

 

foreach ($_POST['po_item'] as $row=>$name)

{

$poid = $PoId;  // comes from above returned insert id.

$poitem = $name;

$qty = $_POST['qty'][$row];

$rate = $_POST['rate'][$row];

$linetotal = $_POST['line_item_total'][$row];

mysql_query('INSERT INTO po_line_items (poid,po_item,qty,rate,line_item_total) VALUES("'.$poid.','.$poitem.','.$qty.','.$rate.','.$linetotal.')') or die(mysql_error());

//mysql_query('INSERT INTO `mytable` VALUES("' . $username . '", ' . $phonenum . ')') or die(mysql_error());

}

Link to comment
https://forums.phpfreaks.com/topic/201038-for-each-mysql_insert/
Share on other sites

Try this:

 

foreach ($_POST['po_item'] as $row=>$name)
{
$poid = $PoId;  // comes from above returned insert id.
$poitem = $name;
$qty = $_POST['qty'][$row];
$rate = $_POST['rate'][$row];
$linetotal = $_POST['line_item_total'][$row];
mysql_query("INSERT INTO po_line_items (poid,po_item,qty,rate,line_item_total) VALUES('$poid','$poitem','$qty','$rate','$linetotal')") or die(mysql_error());
//mysql_query('INSERT INTO `mytable` VALUES("' . $username . '", ' . $phonenum . ')') or die(mysql_error());
} 

It is your quotes, compare these 2 lines:

 

mysql_query("INSERT INTO po_line_items (poid,po_item,qty,rate,line_item_total) VALUES('$poid','$poitem','$qty','$rate','$linetotal')")
mysql_query('INSERT INTO po_line_items (poid,po_item,qty,rate,line_item_total) VALUES("'.$poid.','.$poitem.','.$qty.','.$rate.','.$linetotal.')')

 

You're using single quotes in single quotes. Use single quotes in double quotes and when your trying to do what you are, for example using variables in mysql_query(), use double quotes and you don't need to link them via the dots.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.