php 循环内的 Mysql 查询
mysql_query("INSERT INTO fatture_servizi (id, rif_fattura, servizio, quantita, prezzo_unitario, prezzo_servizio, iva) VALUES ('NULL', '$invoice_rif', '{$services_global[$i]['service']}', '{$services_global[$i]['amount']}', '{$services_global[$i]['unit_price']}', '{$services_global[$i]['service_price']}', '{$services_global[$i]['service_vat']}')");
我需要将它放在一个 php 循环中,就像
$c=count($services);
for($i=0;$i<$c;$i++){
mysql_query("INSERT INTO fatture_servizi (id, rif_fattura, servizio, quantita, prezzo_unitario, prezzo_servizio, iva) VALUES ('NULL', '$invoice_rif', '{$services_global[$i]['service']}', '{$services_global[$i]['amount']}', '{$services_global[$i]['unit_price']}', '{$services_global[$i]['service_price']}', '{$services_global[$i]['service_vat']}')");
};
问题是它只将数组的第一个值插入数据库,而不是每个值。
$services_global 数组看起来像这样,但它可能会发生变化,因为它来自一些动态生成的输入(在主页中您可以添加任意数量的服务)。
Array ( [0] => Array ( [service] => Hostess [amount] => 1 [unit_price] => Eu 120,00 [service_price] => Eu 120,00 [service_vat] => 21 ) [1] => Array ( [service] => Pullman [amount] => 4 [unit_price] => Eu 122,00 [service_price] => Eu 488,00 [service_vat] => 21 ) [2] => Array ( [service] => Cena [amount] => 100 [unit_price] => Eu 100,00 [service_price] => Eu 10.000,00 [service_vat] => 10 ))
mysql_query("INSERT INTO fatture_servizi (id, rif_fattura, servizio, quantita, prezzo_unitario, prezzo_servizio, iva) VALUES ('NULL', '$invoice_rif', '{$services_global[$i]['service']}', '{$services_global[$i]['amount']}', '{$services_global[$i]['unit_price']}', '{$services_global[$i]['service_price']}', '{$services_global[$i]['service_vat']}')");
i need to put it inside a php loop like
$c=count($services);
for($i=0;$i<$c;$i++){
mysql_query("INSERT INTO fatture_servizi (id, rif_fattura, servizio, quantita, prezzo_unitario, prezzo_servizio, iva) VALUES ('NULL', '$invoice_rif', '{$services_global[$i]['service']}', '{$services_global[$i]['amount']}', '{$services_global[$i]['unit_price']}', '{$services_global[$i]['service_price']}', '{$services_global[$i]['service_vat']}')");
};
The problem is that it only inserts into the db the first value of the array and not every value.
The $services_global array looks like this but it could change since it comes from some dynamically generated inputs(into the main page you can add as many services as you wish).
Array ( [0] => Array ( [service] => Hostess [amount] => 1 [unit_price] => Eu 120,00 [service_price] => Eu 120,00 [service_vat] => 21 ) [1] => Array ( [service] => Pullman [amount] => 4 [unit_price] => Eu 122,00 [service_price] => Eu 488,00 [service_vat] => 21 ) [2] => Array ( [service] => Cena [amount] => 100 [unit_price] => Eu 100,00 [service_price] => Eu 10.000,00 [service_vat] => 10 ))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜测这里的问题是
id
是您的自动递增主键,并且您试图在每次迭代时将静态字符串'NULL'
分配给它。试试这个:
...或者更好的是,一起删除
id
查询:此外,您最好使用
foreach
循环(EDITED 帮助您调试问题):I'm guessing that the problem here is that
id
is your auto-incrementing primary key, and you are trying to assign the static string'NULL'
to it on every iteration.Try this:
...or better yet, remove
id
query all together:Also, you would be better using a
foreach
loop for this (EDITED to help you debug the problem):我认为使用 foreach 更容易:
我希望这能解决您的问题!
I think it is easier to use a foreach:
I hope this will fix your problem!
为什么在 VALUES() 中使用 $services_global 并使用 count($services)...它们相同吗?这可能是您的错误...
另请注意,一个好的循环策略(称为循环,而不是循环),您可以使用 FOREACH...
我希望就是这样!
Why are you using $services_global in the VALUES() and using count($services)... Are they the same? This could be your error...
Also note, a good strategy for looping (its called a loop, not a cycle), you can use FOREACH...
I hope thats it!
我会尝试猜测
id
是您的主键,并且您尝试插入具有相同id = 'NULL'
的所有记录。通常主键是整数,所以奇怪的是你没有得到异常而不是数据插入。
最常见的解决方案是拥有整数自动增量主键,那么您不必担心插入它:
SQL:
PHP:
此外,
INSERT
是相当繁重的操作。如果可能的话,我会使用批量插入:并且不要忘记转义传递给数据库的数据!
I'll try to guess that
id
is your primary key and you are trying to insert all your records with the sameid = 'NULL'
.Usually primary key is integer so it is strange that you don't get the exception instead of data insertion.
Most common solution is to have integer auto incremental primary key, then you don't have to worry about inserting it:
SQL:
PHP:
Besides,
INSERT
is rather heavy operation. If it is possible, I'd use bulk inserts:And don't forget about escaping the data you pass to the database!