foreach(($no_items as $key=>$txt ) && ($b_cost as $key=>$txt1 ))
{
$sql1 = "INSERT INTO bulk_s (`no_items`,`b_cost`) VALUES ('$txt[$key]','$txt1[$key]') ";
}
我的php页面中有两个动态创建的数组,其值需要存储在同一行的DB中
我的js代码
function changeIt()
{
my_div.innerHTML = my_div.innerHTML +"<br> items <input type='text' name='txt[]'+ i>"
my_div.innerHTML = my_div.innerHTML +" cost <input type='text' name='txt1[]'+ j>"
i++;
j++;
}
答案 0 :(得分:3)
您的foreach
语法无效PHP。如果你想同时迭代两个具有相同键的数组,你必须反复遍历键:
foreach (array_keys($no_items) as $key) {
$txt = $no_items[$key];
$txt1 = $b_cost[$key];
$sql1 = "INSERT INTO bulk_s …";
}
但是,请注意you should never construct SQL statements like this,这使他们对SQL injections负责。这是一种不可接受的编码实践。在最好的情况下,它会使你的程序不那么优雅,在最坏的情况下,它会让你接受诉讼,there is a very simple superior solution。
答案 1 :(得分:0)
试试这个
$i = 0;
foreach($no_items as $key=>$items) {
$data[$i] = array(
'variable1' => $items['no_items'],
'variable2' => $items['b_cost'],
);
$sql1 = "INSERT INTO bulk_s (`no_items`,`b_cost`) VALUES ('$data['variable1']','$data['variable2']') ";
$i++;
}