我想要一个更新页面,管理员可以在其中更新衬衫的价格,填写新价格并选择将更改哪些衬衫的价格。但是,如果我选择了多个复选框,则它不起作用。
这是我的表格:
<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 和 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句