使用PHP按钮删除单个行/行

时间:2017-02-12 21:36:51

标签: php html mysql

我要做的是让PHP页面显示MySQL数据库的内容,并显示它显示的每一行,它给我一个删除按钮,这样我就可以删除单个行。我的代码有点工作。代码删除代码行,但不删除我选择的代码行,而是删除最后一行,而不删除我要删除的行。

这是一个屏幕截图,我知道它看起来不漂亮,我打算在代码工作后清理它。enter image description here

HTML:

    <?php
        // Delete php code
        if(isset($_POST['delete_series']))
        {
            // here comes your delete query: use $_POST['deleteItem'] as your id
            $delete = $_POST['delete_series'];
            $delete_sql = "DELETE FROM `Dropdown_Series` where `id` = '$delete'"; 

            if(mysql_query($delete_sql, $conn))
            {
                echo "Row Deleted </br>";
                echo "$delete";

            };
        }

        //Insert Code
        if(isset($_POST['add_series']))
        {           
            $insert_sql = "INSERT INTO dropdown_series (series) VALUES ('$_POST[add_series]' )";

            if(!mysql_query($insert_sql, $conn))
                {
                    die ('Error: ' . mysql_error());
                } 

            echo "Series Added <br><br>";

        }

    ?>
    <br />
    <h1>Add New Series Title</h1>

    <h3>Add New: </h3><br />

    <form name = action="add_series.php" method="POST">

    <input type = "text" name = "add_series" required/><br /><br />
    <input type ="submit" name="submit" value="Add">

    </form>

    <h3>Current Series: </h3><br />


    <?php
            //Delete button on each result of the rows
            $query = mysql_query("SELECT id, series FROM Dropdown_Series");     // Run your query

            // Loop through the query results, outputing the options one by one
            echo "<form action='' method='POST'>";
            while ($row = mysql_fetch_array($query)) 
            {

                echo $row['series'] . " ";
                echo $row['id'];
                echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
                echo "<input type='submit' name='submit' value='Delete'>";
                //echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
                echo "<br />";

            } 

        echo "</form>";
    ?>

2 个答案:

答案 0 :(得分:1)

您的循环将导致多个delete_series输入 - 在同一表单中。

您可以为每个选项创建分隔表单:

while ($row = mysql_fetch_array($query)) 
{
  echo "<form action='' method='POST'>";
  // ...
  echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
  echo "<input type='submit' name='submit' value='Delete'>";
  // ...
  echo "</form>";
} 

答案 1 :(得分:0)

我相信一个非常快速的答案是

        while ($row = mysql_fetch_array($query)) 
        {
            echo "<form action='' method='POST'>";
            echo $row['series'] . " ";
            echo $row['id'];
            echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
            echo "<input type='submit' name='submit' value='Delete'>";
            //echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
            echo "<br />";
            echo "</form>";

        }

如果我是你,我宁愿在逻辑中注入一些javascript,并使用ajax调用执行Delete请求。

您可以参考本教程,以便更好地了解我所指的https://scotch.io/tutorials/submitting-ajax-forms-with-jquery