gpt4 book ai didi

php - 复选框值带有单个值

转载 作者:行者123 更新时间:2023-11-29 12:29:52 27 4
gpt4 key购买 nike

我的代码可以工作,但是如果我选择表单中的值,然后只选择单个增值数据库,则会出现问题。哪里出了问题。

<?php
$con = mysqli_connect('localhost', 'root', '12345', 'fruit');
if ($_SERVER['REQUEST_METHOD'] != 'POST' && !isset($_POST['submit'])) {
echo '<form action="" method="post">
<input type="checkbox" name="fruits[]" value="Apple" />APPLE<br />
<input type="checkbox" name="fruits[]" value = "Lemon" />LEMON<br />
<input type="submit" name="submit" value="POST">
</form>';
}elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
$fruit = $_POST['fruits'];
foreach ($fruit as $fruits){
$fruits = mysqli_real_escape_string($con, strip_tags($fruits));
}
$sql_add = mysqli_query($con, "INSERT INTO fruit (name) VALUES ('$fruits')");
if (mysqli_affected_rows($con) == 1) {
echo "Ok not problem.";
}else {
echo "There are some problem. Please submit your choice again.";
}
}

最佳答案

在insert语句后结束for循环,

foreach ($fruit as $fruits){
$fruits = mysqli_real_escape_string($con, strip_tags($fruits));
$sql_add = mysqli_query($con, "INSERT INTO fruit (name) VALUES ('$fruits')");
}

关于php - 复选框值带有单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27721541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com