I want to insert mutiple rows in mysql table using php. Data is in arrays and I am trying to employ following codes but its not working. Please help me.
Query String is:
new_document.php?doctype=Inv&paytype=u&docno=3&bookno=31&prname=40329&pename=1001&paydate=07%2F22%2F2011&indate=7%2F30%2F2011&accname1=Man+Site&DrAmount1=20&CrAmount1=1&accname2=Man+Site&DrAmount2=0&CrAmount2=5&ref=Jul%2F2011
$rows = explode("&", $_SERVER['QUERY_STRING']);
$rowcount =((count($rows)-9)/3); // Count set of rows received , 9 is the costant no of inputs
$serialno = array();
$acc_name = array();
$debit = array();
$credit = array();
$serialno [] = $i;
array_unshift($serialno,"");
unset($serialno[0]);
$acc_name[] = ($_GET['accname'.$i]);
array_unshift($acc_name,"");
unset($acc_name[0]);
$debit[] = ($_GET['DrAmount'.$i]);
array_unshift($debit,"");
unset($debit[0]);
$credit[] = ($_GET['CrAmount'.$i]);
array_unshift($credit,"");
unset($credit[0]);
echo 'Serial Numbers : '.print_r($serialno);
echo '<br>A/C Names : '.print_r($acc_name);
echo '<br>Debit Amount : '.print_r($debit);
echo '<br>Credit Amount : '.print_r($credit);
This gives me following result :
ARRAY
(
[1] => 1
[2] => 2
)
SERIAL NUMBERS : 1ARRAY
(
[1] => MAN SITE
[2] => MAN SITE
)
A/C NAMES : 1ARRAY
(
[1] => 20
[2] => 0
)
DEBIT AMOUNT : 1ARRAY
(
[1] => 1
[2] => 5
)
CREDIT AMOUNT : 1
for ($i=1;$i=$rowcount;$i++)
{
$amount = ($debit[$i]-$credit[$i]);
$accname=$acc_name[$i];
$insert2 = "INSERT INTO `khata2`.`docitems`
(
`dateinput`,
`docno`,
`itemno`,
`accountname`,
`amount`,
`picrefno`,
`updatestamp`)
VALUES (
NOW(''),
'$docno',
'$i',
'$accname',
'$amount',
'$docno',
Null)";
}
if (!mysql_query($insert2)){
echo '<br><a style="color:RED">Zero</a> items saved! Try Again..<br>';
die('Error: ' . mysql_error());
break;
}
echo '<br><a style="color:GREEN">'.$rowcount.' </a>record inserted.';
exit();
RESULTS AS :
Fatal error: Maximum execution time of 30 seconds exceeded in F:\server\htdocs\xampp\Khata2\processor\new_document.php on line 184 ( Line 184 is '$amount',)
ignoring the fact that this is wildly insecure, here's your problem:
for ($i=1;$i=$rowcount;$i++)
this should probably be:
for ($i=1;$i<$rowcount;$i++)
right now you're assigning $rowcount to $i which is always
true
, so the for loop never terminates.1) You do:
but yoou need:
2) Maybe some faster will be to do 1 query than 1 query for every row:
You can use following syntax:
3) PDO or mysqli with prepared statements is great;)