如何在更新查询php中使用多个复选框

如何在更新查询php中使用多个复选框,php,mysql,database,Php,Mysql,Database,我想有一个更新页面,在那里的衬衫价格可以由管理员更新,填写新的价格,并选择衬衫的价格将发生变化。但是,如果我选中多个复选框,它就不起作用 这是我的表格: <form action="" method="post"> <label for= "priceedit">change price:</label> <input type=

我想有一个更新页面,在那里的衬衫价格可以由管理员更新,填写新的价格,并选择衬衫的价格将发生变化。但是,如果我选中多个复选框,它就不起作用

这是我的表格:

        <form action="" method="post">
            <label for= "priceedit">change price:</label>
            <input type= "number" name="priceedit"><br>
            <input type= "checkbox" name="shirtsort" value="010">
                    <label for="shirtsort">Casual v neck cropped Shirt</label><br>
            <input type= "checkbox" name="shirtsort" value="020">
                    <label for="shirtsort">Tie Dye Letter Graphic Tee</label><br>
            <input type= "checkbox" name="shirtsort" value="030">
                    <label for="shirtsort">Casual Text Slogan Shirt</label><br>
            <input type= "checkbox" name="shirtsort" value="040">
                    <label for="shirtsort">Neck Frill Trim Ruched Top</label><br>
            <input type= "submit" name="verwerkupdate" value="Updaten"> <br>
        </form>
    </body>

更改价格:

休闲v领剪裁衬衫
扎染字母图案T恤
休闲文字标语衬衫
颈部褶边装饰褶边上衣

这是我的PHP代码

<?php 
if (ISSET($_POST['verwerkupdate'])){
if(!empty($_POST['shirtsort'])) {
foreach ($_POST['shirtsort'] as $idedit) ;
}
$priceedit = ($_POST['priceedit']);
echo "<br>".$priceedit."<br>";
echo "".$idedit."";
    
try {
$db=new PDO("");
$query = $db->prepare("UPDATE kleur SET price= $priceedit WHERE id LIKE '$idedit%'");
if($query->execute()){
echo "Data updated.";
}else{ 
    echo "Error";
}
}catch (PDOException $e) {
die("Error!: " . $e->getMessage());
}

}
?>

我根据您的HTML和PHP代码编写了一个简短的示例。这对我来说很好。请记住,数组只返回选中的复选框,而不是未选中的复选框

<?php
if (ISSET($_POST['verwerkupdate']))
{
 if(!empty($_POST['shirtsort']))
 {
  foreach ($_POST['shirtsort'] as $idedit)
  {
   echo("$idedit<br>" . PHP_EOL);
  }
 }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="" method="post">
 <label for= "priceedit">change price:</label>
 <input type= "number" name="priceedit"><br>
 <input type= "checkbox" name="shirtsort[]" value="010">
 <label for="shirtsort">Casual v neck cropped Shirt</label><br>
 <input type= "checkbox" name="shirtsort[]" value="020">
 <label for="shirtsort">Tie Dye Letter Graphic Tee</label><br>
 <input type= "checkbox" name="shirtsort[]" value="030">
 <label for="shirtsort">Casual Text Slogan Shirt</label><br>
 <input type= "checkbox" name="shirtsort[]" value="040">
 <label for="shirtsort">Neck Frill Trim Ruched Top</label><br>
 <input type= "submit" name="verwerkupdate" value="Updaten"> <br>
</form>
</body>
</html>

无标题
更改价格:

休闲v领剪裁衬衫
扎染字母图案T恤
休闲文字标语衬衫
颈部褶边装饰褶边上衣


在HTML中,您需要将name属性定义为数组,方法是在name后面加上[]。如:name=“shirtsort[]”中所述,我在名称后添加了[],但遗憾的是,它不起作用。编写PHP代码是为了处理数组。添加[]应该有效。啊,可能还有其他问题。你检查过PHP错误日志了吗?你有什么错误吗?我从未见过没有连接字符串的PDO连接。我的意思是,$db=newpdo(“”;“)。在双引号内,应该包含有关sql server位置、用户名、密码、数据库以及可能的端口的信息。另外,标签属性“for”应该指向元素“id”,而不是“name”。此外,必须将[]放在具有该名称的每个元素之后。(可能)旁注:不要使用字符串插值或串联将值放入SQL查询中。这很容易出错,并且可能使您的程序容易受到SQL注入攻击。使用参数化查询。见和。