【问题标题】:How to use multiple check boxes in UPDATE query php如何在UPDATE查询php中使用多个复选框
【发布时间】:2021-06-01 23:03:52
【问题描述】:

我想要一个更新页面,管理员可以在其中更新衬衫的价格,方法是填写新价格并选择要更改的衬衫价格。但是,如果我选择多个复选框,它将不起作用。

这是我的表格:

        <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>

这是我的 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());
}

}
?>

相同型号但颜色不同的衬衫有不同的 ID,这就是为什么我希望查询选择像“01%”等的衬衫。我希望这不是问题的原因。

【问题讨论】:

  • 在您的 HTML 中,您需要将 name 属性定义为一个数组,方法是将 [] 放在名称之后。如:name="shirtsort[]"
  • 我在名字后面加了[],可惜没用
  • 您的 PHP 代码是为处理数组而编写的。添加 [] 应该可以。啊,也许还有其他问题。您检查过 PHP 错误日志吗?你有任何错误吗?我从未见过没有连接字符串的 PDO 连接。我的意思是,'$db=new PDO("");'。在双引号内应该有关于 sql server 位置、用户名、密码、数据库和端口的信息。
  • 还有一点,标签属性'for'应该指向元素'id',而不是'name'。此外,[] 必须放在具有该名称的每个元素之后。
  • (可能)旁注:不要使用字符串插值或连接将值获取到 SQL 查询中。这很容易出错,并且可能使您的程序容易受到 SQL 注入攻击。使用参数化查询。见"How to include a PHP variable inside a MySQL statement""How can I prevent SQL injection in PHP?"

标签: php mysql database


【解决方案1】:

我根据您的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多