我正在制作"删除"功能在我的表格上。我不太高兴,因为它不起作用!我试过这段代码:
while($row = $result->fetch_assoc())
{
$ordernr = $row['ordernr'];
$klantnaam = $row['klantnaam'];
$productnaam = $row['productnaam'];
$productid = $row['productid'];
echo "<tr>";
echo "<td width='150px'>" . $ordernr . "</td>";
echo "<td width='150px'>" . $klantnaam . "</td>";
echo "<td width='300px'>" . $productnaam . "</td>";
echo "<td width='100px'>" . $productid . "</td>";
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
$delete = $_POST['delete'];
if (isset($_POST['verwijderen'])) {
foreach($delete as $ordernr => $delete)
{
$ordernr = mysqli_real_escape_string($ordernr);
$query = mysqli_query("DELETE FROM overzicht WHERE ordernr= $ordernr");
}
}
你们知道我做错了吗?
提前致谢!
答案 0 :(得分:1)
1)
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
转换为
echo "<td align='center' width='50px'><input name='delete' type='checkbox' value='$ordernr'></td>";
2)
foreach($delete as $ordernr => $delete)
do it:
foreach($delete as $ordernr => $del)
你正在覆盖$ _POST
答案 1 :(得分:0)
您在重复使用变量时进行迭代:
foreach($delete as $ordernr => $delete)
第二个$ delete将覆盖第一个$的值,并在工作中抛出一个扳手。
我建议将$ delete的先前实例重命名为$ deleteList并执行:
foreach($ deleteList as $ ordernr =&gt; $ delete)
请记住,您的大部分代码都可以通过更合适的方式完成。当你学习PHP时,你会发现这些。
答案 2 :(得分:0)
mysqli_query语法是
mysqli_query($con,"your query");
答案 3 :(得分:0)
我会说你的输入复选框需要一个值。像
<input type="checkbox" name="delete[1]" value="1" />
另一方面,那么
会更容易<input type="checkbox" name="delete[]" value="$ordernr" />
然后你用
循环foreach ($_POST["delete"] as $id) delete(id)
答案 4 :(得分:0)
您的查询中的con变量...
$query = mysqli_query($con,"DELETE FROM overzicht WHERE ordernr= $ordernr");