I am doing a multiple row insert into a table from a form. This worked initially but seems to have blown up. The problem is with the commas.
function do_insert( $zen )
{
$values = array();
for( $i = 0; $i < count($_SESSION['quant_array']); $i++)
{
array_push( $values, $_POST[ $_SESSION['quant_array'][$i] ] );
}
//values has corresponding values entered in by user from 0 -- n
//$_SESSION['crop_array'] has corresponding crops to values from 0 --n
//insert anyone?
//$_SESSION['customer_id'] is 'static', $_SESSION['week'] is 'static'
$insert_q = sprintf("INSERT INTO zen_CSACustomOrders (customers_id, csaWk, quant, csaCrop)
VALUES ");
for($i = 0; $i < count($values); $i++)
{
//value not = 0
if( $values[$i] != 0 )
{
$insert_q .= "( " . $_SESSION['customer_id'] . ", "
. $_SESSION['week'] . ", " . $values[$i] . ", "
. $_SESSION['crop_array'][$i] . " ) ";
if( count($values) > ($i+1) )
{
//add a comma
$insert_q .= ", ";
}
}
}//end for
$insert_q .= "; ";
print $insert_q;
$handle_rawr = mysql_query($insert_q, $zen) or die(mysql_error());
the resulting print statement is
INSERT INTO zen_CSACustomOrders (customers_id, csaWk, quant, csaCrop) VALUES ( 3, 1, 3, 46 ) , ( 3, 1, 2.5, 48 ) , ; 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 '' at line 2
The if test is not working properly to determine whether or not to add a comma.