【发布时间】:2020-03-25 13:19:15
【问题描述】:
我有一个项目列表,每个项目都有一个删除按钮,但是当我按下它时没有任何反应。数据库已连接,因为它显示表中的项目,但我不知道为什么删除按钮不起作用。如果有人看到错误,请告诉我。非常感谢。另外,这是我的第一个网站,所以我的代码现在很乱。
注意:我确实关闭了数据库,所以这不是问题。
user.php:
<?php
$user_id = '999';
$host="127.0.0.1";
$port=3306;
$socket="";
$user="root";
$password="root";
$dbname="peas";
$db = new mysqli($host, $user, $password, $dbname, $port, $socket) or die ('Could not connect to the database server' . mysqli_connect_error());
?>
//continued
<div style: "height:1000px; class="col-md-3" id="ingredients">
<ul class="list-group" style="max-height:900px; overflow-y:auto; overflow-x: hidden">
<?php
$query = "SELECT ingredient_name FROM Ingredient";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){ ?>
<li style="text-align: left; padding:10px"
class="list-group-item">
<form action="user_functions.php" method="post">
<?php echo $row['ingredient_name'];?>
<input type="hidden" name="ingredient_name" value= "<?php $row['$ingredient_name']; ?>">
<button type="button" name="delete" style="float:right; border:none;">×</button>
</form>
</li>
<?php }
mysqli_free_result($result);
} else{
echo "<li>"."Pantry is Empty!"."</li>";
}
?>
</ul>
</div>
user_functions.php:
<?php
if(isset($_POST['delete'])){
$ingredient_name=$_POST['ingredient_name'];
$sql="DELETE FROM ingredient WHERE ingredient_name=$ingredient_name";
$result = mysqli_query($db, $sql);
}
?>
【问题讨论】:
-
不需要html和css标签。
-
您的代码易受 SQL 注入攻击。您应该使用准备好的语句。