execute(); $stmt->-6ren">
gpt4 book ai didi

php - 从下拉列表中删除值

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:03 24 4
gpt4 key购买 nike

我正在尝试编写一个允许我从下拉列表中删除选定值的函数。

<?php
require_once("db.inc.php");
?>
</head>
<body>
<form action="" method="POST">
<?php





$stmt = $mysqli->prepare("SELECT anr, name FROM artikel");
$stmt->execute();
$stmt->bind_result($anr, $name);


echo "<select name='selected_name'><br />";

while ($stmt->fetch()) {

echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>';


if(isset($_POST['loeschen'])){

$stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?");
$stmt->bind_param('i', $anr);
$stmt->execute();
$stmt->close();
$mysqli->close();

}

}


?>
<input type="submit" value="Datensatz löschen" name="loeschen">
</form>
</body>
</html>

我的问题是,即使我不按提交按钮,这些值也会被删除。提前感谢您的建议。

最佳答案

使用 Post 值而不是 $anr。

<?php
require_once("db.inc.php");
?>
</head>
<body>
<?php
if(isset($_POST['selected_name'])){

$stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?");
$stmt->bind_param('i', $_POST['selected_name']);
$stmt->execute();
$stmt->close();
$mysqli->close();

}

}
?>
<form action="" method="POST">
<?php

$stmt = $mysqli->prepare("SELECT anr, name FROM artikel");
$stmt->execute();
$stmt->bind_result($anr, $name);
echo "<select name='selected_name'><br />";
while ($stmt->fetch()) {
echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>';
}
echo "</select>";
?>
<input type="submit" value="Datensatz löschen" name="loeschen">
</form>
</body>
</html>

关于php - 从下拉列表中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38048899/

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