我正在尝试为多个插入值编写代码,但无法正常工作。错误是这个警告:为foreach()提供的参数无效。任何帮助都会受到欢迎。
if (isset($_POST['mul_pr'])) {
$counter = $mysqli->real_escape_string($_POST["counter"]);
$pr = $mysqli->real_escape_string($_POST["pr"]);
$pr_qty = $mysqli->real_escape_string($_POST["pr_qty"]);
$unit = $mysqli->real_escape_string($_POST["unit"]);
$pr_date = $mysqli->real_escape_string($_POST["pr_date"]);
$gss_date = $mysqli->real_escape_string($_POST["gss_date"]);
$request = $mysqli->real_escape_string($_POST["requested"]);
$approve = $mysqli->real_escape_string($_POST["approved"]);
$desig_r = $mysqli->real_escape_string($_POST["designation_r"]);
$desig_a = $mysqli->real_escape_string($_POST["designation_a"]);
$year = $mysqli->real_escape_string($_POST["year"]);
$pr_id = $mysqli->real_escape_string($_POST["pr_id"]);
if (count($counter > 0))
{
$new = array();
foreach ($counter as $key => $value)
{
$new[] = "('" . $counter . "', '" . $pr . "', '" . $pr_qty . "', '" . $unit . "','" . $pr_date . "', '" . $gss_date . "', '" . $request . "', '" . $approve . "'
, '" . $desig_r . "', '" . $desig_a . "', '" . $year . "', '" . $pr_id . "')";
}
if (count($new) > 0)
{
$result = $mysqli->query("INSERT INTO purchase_request (counter, pr, total_quantity, unit, pr_date, gss_date, requested, designation_r, approved, designation_a,year,pr_id)
VALUES " . implode(', ', $new));
}
}
}
答案 0 :(得分:1)
首先是错误:
if (count($counter > 0))
应该是
if (count($counter) > 0)
您可能会收到警告,因为count($counter)
可能为0并且由于伪造的if条件,它可能会在非数组或空数组上执行for循环,因此错误
答案 1 :(得分:0)
1. foreach失败的原因是你的案例中的“$ counter”不是一个数组或一个迭代的对象......
我只是建议硬编码你的sql语句,因为你已经在那里
了$ result = $ mysqli-> query(“INSERT INTO purchase_request(counter,pr,total_quantity,unit,pr_date,gss_date,requested,designation_r,approved,designation_a,year,pr_id)
VALUES('$ counter','$ pr','');
2。或者,如果你想“自动化”你的名字值对进入sql,你将不得不准备一个更完整的“$ new = array()”..像这样的东西
$new["counter"] = $mysqli->real_escape_string($_POST["counter"]);
$new["pr"] = $mysqli->real_escape_string($_POST["pr"]);
//and so on...
然后你不再需要使用“array_keys($ new)”
来循环$result = $mysqli->query("INSERT INTO purchase_request (".implode(",", array_keys).")
VALUES ('" . implode("','" , array_values($new))) . "');";
答案 2 :(得分:0)
Use this , it work for u
if (isset($_POST['mul_pr'])) {
$counter = $mysqli->real_escape_string($_POST["counter"]);
$pr = $mysqli->real_escape_string($_POST["pr"]);
$pr_qty = $mysqli->real_escape_string($_POST["pr_qty"]);
$unit = $mysqli->real_escape_string($_POST["unit"]);
$pr_date = $mysqli->real_escape_string($_POST["pr_date"]);
$gss_date = $mysqli->real_escape_string($_POST["gss_date"]);
$request = $mysqli->real_escape_string($_POST["requested"]);
$approve = $mysqli->real_escape_string($_POST["approved"]);
$desig_r = $mysqli->real_escape_string($_POST["designation_r"]);
$desig_a = $mysqli->real_escape_string($_POST["designation_a"]);
$year = $mysqli->real_escape_string($_POST["year"]);
$pr_id = $mysqli->real_escape_string($_POST["pr_id"]);
if (count($counter) > 0))
{
$new = array();
for($i=0;$i<count($counter);$i++)
{
$new[] = "('" . $counter . "', '" . $pr . "', '" . $pr_qty . "', '" . $unit . "','" . $pr_date . "', '" . $gss_date . "', '" . $request . "', '" . $approve . "'
, '" . $desig_r . "', '" . $desig_a . "', '" . $year . "', '" . $pr_id . "')";
if (count($new) > 0)
{
$result = $mysqli->query("INSERT INTO purchase_request (counter, pr, total_quantity, unit, pr_date, gss_date, requested, designation_r, approved, designation_a,year,pr_id)
VALUES " . implode(', ', $new));
$new="";
}
}
}
}