我有一些代码用于分隔逗号分隔的条目以包含在数据库中。但是,它只插入1个项目而不是3,这就是我输入的内容。
如果我做var_dump($ _ SESSION);在NIADMIN.PHP中,它显示所有条目都已正确传递。但是,它会在数据库中插入1个项目。
如果有人能在我的代码中指出错误,我将不胜感激。非常感谢
BOXINTAKE_RESULTS.PHP
<?php
if (isset($_POST['box_add'])) {
if (!empty($_POST['box_add'])) {
$data = split(',', $_POST['box_add'][0]);
$adds = array();
$duplicates = array();
foreach ($data as $val) {
if ($val != "") {
if (count(array_unique($data)) < count($data)) {
echo "<span style=\"color: red; font-size: 12px; font-weight: bold;\">$val</span>" . ' ' . ' Box input is a duplicate. Each box name must be unqiue.' . '<br />' . 'Please only input boxes that have unique names.';
echo '<br /><br />';
echo '<input type="button" value="Return" onclick="history.go(-1);return false;" />';
return;
}
$sql = "SELECT custref FROM boxes WHERE custref='$val' AND customer = '" . $_SESSION['kt_idcode_usr'] . "' Union SELECT item FROM act WHERE item='$val' AND company = '" . $_SESSION['kt_idcode_usr'] . "'";
$qry = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($qry)) {
$duplicates[] = $val;
} else {
$adds[] = $val;
$flag = 1;
}
} else {
$boxerror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'BOX ERROR! ' . '</span>' . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . 'You must enter a box for intake' . '</span>';
echo "<script language=\"JavaScript\">\n";
echo 'alert("BOX ERROR:\nYou must enter a box for intake.");';
echo "</script>";
echo $boxerror;
}
}
if (count($adds)) {
echo 'You wish to add box(es): ' . '<div style="word-wrap:break-word;white-space: pre-wrap;overflow:auto !important;height: 100px; width: 250px; border: 1px solid #666; background-color: #fff; padding: 4px;">' . '<span style="font-weight:bold;color: #000;">' . join(', ', $adds) . '</span>' . '</div>' . '<p />';
//if(count($duplicates) == 0) // add to session only if we have no duplicates
$_SESSION['box_add'] = $adds; /// change here, do not store $_POST since it can contain duplicates
}
if (count($duplicates)) {
$boxduperror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'BOX ERROR:' . " " . 'Box' . '</span>' . " " . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . '(' . join(', ', $duplicates) . ')' . " " . 'This box is already in the database' . '</span>';
echo "<script language=\"JavaScript\">\n";
echo 'alert("BOX ERROR:\nThis box is already in the database");';
echo "</script>";
echo $boxduperror;
}
}
}
?>
在新窗口中打开
NIADMIN.PHP
<?php
if (empty($_SESSION['datepick'])) {
//Box Intake
$activity = $_SESSION['box_add']; {
foreach ($activity as $k => $v) {
// add to action list
$query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `new`) VALUES (\'' . $service . '\', \'' . $boxsupply . '\', \'' . $deptname . '\', \'' . $customer . '\', \'' . $address . '\', \'' . $user . '\', \'' . strtoupper($v) . '\', DATE_ADD(NOW(), INTERVAL 7 YEAR), NOW(), \'' . $new . '\');';
mysql_query($query) or die('Error: ' . $query . 'query failed');
echo $query;
echo "<script language=\"JavaScript\">\n";
//echo 'alert("BOX INTAKE SUCCESSFULL.\nYou will now be redirected to the requests area.");';
//echo 'location.href = "boxes.php"';
echo "</script>";
}
}
}
?>
在新窗口中打开
来自niadmin.php的VAR-DUMP $ _session
["box_add"]=> array(2) { [0]=> string(10) "demo234567" [1]=> string(10) "demo345678" }
DATABASE QUERY
数据库中只插入了1个未2的项目
INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `new`) VALUES ('Standard', 'New Intake', 'DEMO', 'DEMO', 'Demo Road Anytown EC4 5RT', 'Demo User', 'DEMO234567', '2024-08-22', NOW(), '1');