这是我的代码:
$text = $_REQUEST["enable"];
$description = 'Debited For Service';
foreach($text as $data)
{
foreach($check as $data1)
{
$insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$data', '$description')";
mysql_query($insert);
}
}
结果是运行了这些查询,但我只想要前两个查询:
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('2', '200', 'Debited For Service')
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('3', '100', 'Debited For Service')
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('2', '200', 'Debited For Service')
INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('3', '100', 'Debited For Service')
答案 0 :(得分:1)
只使用一个循环,而不是嵌套循环,并一起访问两个数组的相应元素。
foreach ($text as $i => $data) {
$data = mysql_real_escape_string($data);
$data1 = mysql_real_escape_string($check[$i]);
$insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$data', '$description')";
mysql_query($insert);
}
还有一个问题:您只列出要插入的3列,但您有4个值。我猜这只是一个复制错误。
答案 1 :(得分:0)
它可能是因为你在内循环中触发查询,
请一起来,
$text = $_REQUEST["enable"];
$description = 'Debited For Service';
foreach($text as $data)
{
foreach($check as $data1)
{
$inData = $data;
}
$insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$inData', '$description')";
mysql_query($insert);
}
猜这会起作用
答案 2 :(得分:0)
替换为以下代码,以便快速解决问题
$quick_fix=1;
foreach($text as $data)
{
if($quick_fix==1){
foreach($check as $data1)
{
$insert="INSERT INTO `wallet_transaction`(`wallet_id`, `debit_amount`, `description`) VALUES ('$esc_id', '$data1', '$data', '$description')";
mysql_query($insert);
}
$quick_fix++;
}
}