我有一个脚本,用于检查db中的重写值并通过结果循环,并使用json回显该值。但是,它只显示1个记录而不是3个正在发送到php的记录。如果有人能指出我的错误,我将不胜感激。感谢
$boxitems = mysqli_real_escape_string($conn, $_POST['box']);
$array = array();
$array = $boxitems;
foreach ($array as $boxes) {
$sql = "SELECT item FROM act WHERE item = '".$boxes."' GROUP BY item HAVING COUNT(*) > 1";
$result = mysqli_query($conn, $sql) or die('Error selecting item: ' . mysqli_error());
$num_rows = mysqli_num_rows($result);
if($num_rows) {
while ($row = mysqli_fetch_array($result)) {
$data[] = $row['item'];
}
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: red; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo json_encode($data) . ' already exists. Please enter a unique box reference.';
echo '</div>';
exit;
} else {
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: #63c84c; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo 'No dupes found in database.';
echo '</div>';
exit;
}
}
更新屏幕截图
答案 0 :(得分:1)
将您想要回显的内容放入在foreach之外声明的单个变量中并连接每个div(每个div对应一个foreach执行的操作)。然后在循环之后回复它。
$boxitems = $_POST['box'];
$insertedItems = array();
$duplicateItems = array();
foreach ($boxitems as $boxes) {
$escapedBoxes = mysqli_real_escape_string($conn, $boxes);
$sql = "SELECT item FROM act WHERE item = '".$escapedBoxes."' GROUP BY item HAVING COUNT(*) > 0";
$result = mysqli_query($conn, $sql) or die('Error selecting item: ' . mysqli_error());
$num_rows = mysqli_num_rows($result);
if($num_rows) {
while ($row = mysqli_fetch_array($result)) {
$duplicateItems[] = $row['item'];
}
}
else
{
$insertedItems[] = $escapedBoxes;
}
}
if(!empty($duplicateItems)) {
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: red; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo json_encode($duplicateItems) . ' already exists. Please enter a unique box reference.';
echo '</div>';
} else {
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: #63c84c; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo 'No dupes found in database.<br/>';
echo json_encode($insertedItems) . ' has been entered successfully into the database.';
echo '</div>';
}
编辑:考虑了u_mulder评论更新了代码。
答案 1 :(得分:0)
$boxitems = mysqli_real_escape_string($conn, $_POST['box']);
$array = array();
$array = $boxitems;
foreach ($array as $boxes) {
$sql = "SELECT item FROM act WHERE item = '".$boxes."' GROUP BY item HAVING COUNT(*) > 1";
$result = mysqli_query($conn, $sql) or die('Error selecting item: ' . mysqli_error());
$num_rows = mysqli_num_rows($result);
if($num_rows) {
while ($row = mysqli_fetch_array($result)) {
$data[] = $row['item'];
}
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: red; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo json_encode($data) . ' already exists. Please enter a unique box reference.';
echo '</div>';
exit;
} else {
echo '<div style="width: 50%; margin-bottom: 20px; border-radius: 5px; border: 1px solid black; background: #63c84c; font-size: 16px; color: white; height: 50px; padding: 15px; line-height: 1.3;">';
echo 'No dupes found in database.';
echo '</div>';
}
exit;
}
任何函数,循环或条件内的退出都会使程序退出并继续,因此它不会迭代,它会在第一次迭代后退出。