【问题标题】:How to remove mysql data from html delete button?如何从 html 删除按钮中删除 mysql 数据?
【发布时间】: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;">&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);
    }
?>

【问题讨论】:

  • 不需要html和css标签。
  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。

标签: php html mysql database


【解决方案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();
    }
?>

【讨论】:

    【解决方案2】:

    如果您的问题是没有按 × 提交表单 那么你应该改变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>
    

    【讨论】:

      【解决方案3】:
        <input type="hidden" name="ingredient_name" value="<?php $row['$ingredient_name']; ?>">
      

      两件事

      1. 您在上面的输入中错误地显示了值,它应该是 $row['ingredient_name'] 删除括号内的$ 加上 你应该回应它。

        所以应该是&lt;?php echo $row['ingredient_name'];?&gt;

      2. 您必须使用按钮类型为Submit 而不是button 形式将 如果没有按钮类型提交,则不提交。

      【讨论】:

        【解决方案4】:

        除了其他两个答案,还有一个问题。

        如果您向我们展示的 user_function.php 中的代码是您的 user_function.php 中的所有内容,则 user_function.php 中不存在变量 $db。

        因此,您需要将连接和 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);
            }
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-25
          • 1970-01-01
          • 1970-01-01
          • 2016-10-01
          • 2015-07-21
          • 1970-01-01
          相关资源
          最近更新 更多