簡體   English   中英

如何從html刪除按鈕中刪除mysql數據?

[英]How to remove mysql data from html delete button?

我有一個項目列表,每個項目都有一個刪除按鈕,但是當我按下它時沒有任何反應。 數據庫已連接,因為它顯示表中的項目,但我不知道為什么刪除按鈕不起作用。 如果有人看到錯誤,請告訴我。 非常感謝。 另外,這是我的第一個網站,所以我的代碼現在很亂。

注意:我確實關閉了數據庫,所以這不是問題。

用戶.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 代碼更改為:

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

如果您的問題是不按×提交表單,那么您應該更改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>
  <input type="hidden" name="ingredient_name" value="<?php $row['$ingredient_name']; ?>">

兩件事情

  1. 您在上面的輸入中錯誤地顯示了值,它應該是$row['ingredient_name']刪除括號內的$加上您應該回顯它。

    所以它應該是<?php echo $row['ingredient_name'];?>

  2. 您必須使用按鈕類型作為Submit ,如果沒有按鈕類型提交,則不會提交button表單。

除了其他兩個答案之外,還有一個問題。

如果您向我們展示的 user_function.php 中的代碼是 user_function.php 中的所有內容,則 user_function.php 中不存在變量 $db。

因此,您需要將連接和 HTML 分成兩個文件(例如 config.php、user.php)並將 config.php 包含或要求到 user_function.php。 IE

配置文件:

<?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);
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM