循环使用pg_fetch_row
问题描述:
how if I need is looping in php to insert the data for up to 6 rows. Because if I use this code it just only appear once insertion and not repeated.
while ($row=pg_fetch_row($sql1))
{
$sqlinsert="insert into rpt_printing_detail values ( '$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]','$row[9]')";
}
My code just insert one insertion :
insert into rpt_printing_detail values ( '201501', 'Detail Billing', '1', '1', '16397', '0', '0', '0', '0', '0' )
如果我需要在php中循环以插入最多6行的数据。 因为如果我使用这个代码,它只会出现一次插入而不会重复。 p>
while($ row = pg_fetch_row($ sql1))
{
$ sqlinsert =“ 插入rpt_printing_detail值('$ row [0]','$ row [1]','$ row [2]','$ row [3]','$ row [4]','$ row [5 ]”, '$行[6]', '$行[7]', '$行[8]', '$行[9]')“; \ N}
代码> PRE>
我的代码只插入一个插入内容: p>
\ n
插入rpt_printing_detail值('201501','详细结算','1','1','16397','0','0','0','0',' 0')
code> pre>
div>
答
Your code creates a SQL query as a string. It doesn't do anything to the database.
Presumably a later statement outside the loop runs the query in the variable, like:
while ($row=pg_fetch_row($sql1))
{
$sqlinsert="insert into rpt_printing_detail values ( '$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]','$row[9]')";
}
pg_query($sqlinsert);
Since there's only one query - you overwrite it with each loop - it only makes one change.
Irrespective of the issue here, you have a major bug. Read the PHP documentation on SQL injection then use pg_query_params or preferably PDO.