我想使用php在mysql表中插入多行。数据是在数组中,我试图使用以下代码,但它不起作用。请帮帮我。
查询字符串是:
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',)
答案 0 :(得分:1)
忽略了这个非常不安全的事实,这是你的问题:
for ($i=1;$i=$rowcount;$i++)
这应该是:
for ($i=1;$i<$rowcount;$i++)
现在你将$ rowcount分配给$ i,它总是true
,因此for循环永远不会终止。
答案 1 :(得分:0)
1)你这样做:
for ($i=1;$i<=$rowcount;$i++){
//generate query
}
//do query
但你需要:
for ($i=1;$i<=$rowcount;$i++){
//generate query
//do query
}
2)对于每一行,可能更快一次查询而不是1次查询:
您可以使用以下语法:
INSERTY INTO table(field1,field2) VALUES(v11,v12),(v21,v22),(v31,v32);
$insert2=array();
for ($i=1;$i=$rowcount;$i++)
{
$amount = ($debit[$i]-$credit[$i]);
$accname=$acc_name[$i];
$insert2[] = "(
`dateinput`,
`docno`,
`itemno`,
`accountname`,
`amount`,
`picrefno`,
`updatestamp`)
VALUES (
NOW(''),
'$docno',
'$i',
'$accname',
'$amount',
'$docno',
Null)";
}
if (!mysql_query('INSERT INTO tblname VALUES'.implode(',',$insert2))){
echo '<br><a style="color:RED">Zero</a> items saved! Try Again..<br>';
die('Error: ' . mysql_error());
break;
}
3)准备好的陈述的PDO或mysqli很棒;)