我有foreach循环,无法将数据插入db。如果我将插入语句移出循环,则可以正常工作,但仅适用于1个项目。同样,回声不返回任何数据。如果有人可以检查我的代码并指出我的错误,我将不胜感激。
假定所有连接都已建立,并且所有$ _POST值正确。非常感谢
<?php
session_start();
include($_SERVER['DOCUMENT_ROOT'] . '/lsorg/users/Connections/ls.php');
$company = strtoupper(mysqli_real_escape_string($conn, $_SESSION['ls_idcode_usr']));
$activity = "Box Return";
$address = ucwords(mysqli_real_escape_string($conn, $_POST['address']));
$service = ucwords(mysqli_real_escape_string($conn, $_POST['service']));
$success = 'SUCCESS';
$authorised = mysqli_real_escape_string($conn, $_SESSION['ls_name_usr']);
$dept = strtoupper(mysqli_real_escape_string($conn, $_POST['dept']));
$boxitems = $_POST['sub'];
$box = implode(",",$boxitems);
$array = array();
$array = $boxitems;
foreach ($array as $boxes) {
$outString .= "$box" . " ";
//$boxitem = $box;
$sql = "INSERT INTO act
(service, activity, department,
company, address, user,
`date`, item, new)
VALUES ('".$service."', '".$activity."', '".$dept."',
'".$company."', '".$address."', '".$authorised."',
NOW(), '".$boxes."', '1')";
$result = mysqli_query($conn, $sql) or die ('Error inserting box:' . mysqli_error());
}
echo $outString;
mysqli_close($conn); // Connection Closed
?>
添加调试代码后
警告:implode()[function.implode]:在第16行的C:\ wamp \ www \ lsorg \ users \ brtn.php中传递了无效的参数警告:为C:\ wamp \中的foreach()提供了无效的参数www \ lsorg \ users \ brtn.php在第22行string(10)“ DEMOBOX050”注意:未定义变量:C:\ wamp \ www \ lsorg \ users \ brtn.php在第39行的outString
回复$_POST['sub']
DEMOBOX044,DEMOBOX050
答案 0 :(得分:3)
您的变量不正确。
编辑为@RiggsFolly敏锐的眼睛! (爆破->爆炸)
查看代码:
// OLD: $boxitems = implode(",", $_POST['sub']);
$boxitems = explode(",", $_POST['sub']);
foreach ($boxitems as $box) {
$outString .= "$box" . " ";
...
不需要所有这些。
$boxitems = $_POST['sub'];
$box = implode(",",$boxitems);
$array = array();
$array = $boxitems;