我的数据库中有一个名为faculty的表。我从可以看到院系名称的地方做了一个下拉列表。我还尝试按下一个按钮来删除当前选中的元素。但它所做的一切,就是将所选项目更改为另一项,而不是从数据库中删除教师。我是PHP的新手,有点帮助吗?
<?php
$query = "SELECT name, faculty_id FROM faculty ORDER BY name ASC";
$result = mysqli_query($con, $query) or die(mysqli_error());
?>
<select name="categories">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['faculty_id'] . "'>" . $row['name'] . "</option>";
$row = $row['faculty_id'];
}
?>
</select>
<?php
if (isset($_POST['delete_faculty'])) {
$sql = "DELETE FROM faculty WHERE faculty_id = $row";
$result = mysqli_query($con, $sql);
}
?>
<form action="admin" method="post">
<button type="submit" name="delete_faculty">Delete</button>
</form>
&#13;
答案 0 :(得分:1)
<?php
$query = "SELECT name, faculty_id FROM faculty ORDER BY name ASC";
$result = mysqli_query($con, $query) or die(mysqli_error());
?>
<form action="#" method="post">
<select name="categories">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['faculty_id'] . "'>" . $row['name'] . "</option>";
$row = $row['faculty_id'];
}
?>
</select>
<input type="submit" name="delete_faculty" value="Delete">
</form>
<?php
if (isset($_POST['delete_faculty'])) {
$cato = $_POST['categories'];
$sql = "DELETE FROM faculty WHERE faculty_id = $cato";
$result = mysqli_query($con, $sql);
header("Refresh:0");
}
?>
答案 1 :(得分:-1)
您忘记在delete
查询中使用正确的行ID。这应该是诀窍:
$facultyId = (int)$_POST['delete_faculty'];
$sql = "DELETE FROM faculty WHERE faculty_id = " . $facultyId;
此外,您应该查看准备好的陈述。他们会帮助您避免SQL注入