如何通过HTML删除按钮从MySQL中删除数据?

3
我有一份物品清单,每个物品都有一个删除按钮,但当我按下它时什么都没有发生。数据库已连接,因为它在表格中显示了这些物品,但是我不知道为什么删除按钮不能正常工作。如果有人发现错误,请告诉我。非常感谢。此外,这是我的第一个网站,所以我的代码现在比较混乱。

注:我确实关闭了数据库,所以这不是问题。

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;">&times;</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);
    }
?>

1
不需要使用 HTML 和 CSS 标签。 - Krupal Panchal
你的代码容易受到 SQL 注入攻击。你应该使用预处理语句。 - Dharman
4个回答

1
你需要将你的HTML代码更改为以下内容:
<form action="user_functions.php" method="post">
    <?php echo $row['ingredient_name'];?>
    <input type="hidden" name="ingredient_name" value="<?php echo $row['$ingredient_name']; ?>">
    <button type="submit" name="delete" style="float:right; border:none;">&times</button>
</form>

并编辑user_functions.php文件为:

<?php
    if(!empty($_POST['submit']) && isset($_POST['ingredient_name'])){
        $stmt = $db->prepare("DELETE FROM ingredient WHERE ingredient_name=?");
        $stmt->bind_param('s', $_POST['ingredient_name']);
        $stmt->execute();
    }
?>

0
如果您的问题是在按下 × 后无法提交表单,则应更改 button type="submit"
<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="submit" name="delete" style="float:right; border:none;">&times</button>
</form>

0
  <input type="hidden" name="ingredient_name" value="<?php $row['$ingredient_name']; ?>">

两件事情

  1. 您在上面的输入框中显示值的方式不正确,应该是$row['ingredient_name'],括号内去掉$,并且您还需要使用echo语句进行输出。

    所以应该是<?php echo $row['ingredient_name'];?>

  2. 您必须将按钮类型设置为Submit而不是button,否则表单将无法提交。


-1
除了其他两个答案,还有另一个问题。
如果你所展示的user_function.php中的代码是全部内容,那么变量$db在user_function.php中不存在。
因此,你需要将连接和HTML分别放在两个文件中(例如config.php、user.php),并将config.php包含或要求到user_function.php中。即:

config.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());
?>

user_function.php:

<?php
    require_once("config.php");
    if(isset($_POST['delete'])){
        $ingredient_name=$_POST['ingredient_name'];
        $sql="DELETE FROM ingredient WHERE ingredient_name=$ingredient_name";
        $result = mysqli_query($db, $sql);
    }
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接