我有一个表显示来自 mysql 表的信息,在最后一列中我有一个如下所示的复选框:
<form method="POST" action="php/delete.php">
<thead>
<tr
<th>Nom</th>
<th>Espéce</th>
<th>Cri</th>
<th>Propiétaire</th>
<th>Age (années)</th>
<th><input type="submit" name="supprimer" value="Supprimer" /></th>
</tr>
</thead>
<tbody>
<?php
$connexion = mysql_connect($hote, $login, $mdp);
mysql_select_db($bd, $connexion);
$req = "Select * from animaux;";
$resultat = mysql_query($req, $connexion);
while (list($id, $nom, $esp, $cri, $prop, $age) = mysql_fetch_row($resultat)) {
?>
<tr>
<td><?= $nom ?> </td>
<td><?= $esp ?></td>
<td><?= $cri ?></td>
<td><?= $prop ?></td>
<td><?= $age ?></td>
<td> <input type="checkbox" name="choix[]" value=<?= $id ?>></td>
</tr>
<?php } ?>
</form>
我正在使用以下脚本来删除行:
include("../BD/bd_params.inc.php");
$connexion = mysql_connect($hote,$login,$mdp);
mysql_select_db($bd, $connexion);
$del = $_POST['choix'];
foreach ($del as $val) {
$req = "delete from `animaux` where id = '$val'";
$resultat = mysql_query($req,$connexion);
if (!resultat) {
die('Requête invalide : ' . mysql_error());
break;
}
}
但是,当我提交表单时,没有任何反应,没有显示任何内容,也没有删除任何内容。有谁可能导致我的脚本无法工作吗?
(哦,对不起我的英语......)
最佳答案
if (!resultat) {
应该是
if (!$resultat) {
^--missing $
“break”调用是多余的。 die() 将终止脚本,因此不可能到达中断点。
您是否已验证是否有内容正在发送到服务器。 var_dump($_POST)
显示什么?
关于php - 在 php 中使用带有表单的复选框从数据库中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7971351/