douchen4915 2017-02-12 21:36
浏览 70
已采纳

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

What I am trying to do is have a PHP page display the contents of a MySQL database and and each line it displays, it give me a delete button so I can delete individual rows. I have the code kind of working. The code delete the lines of code, but not the one I select, instead it deletes the last line and not the one I tell it to delete.

Here is a screenshot, I know it does not look pretty, I was going to clean it up after I get the code working.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条回答 默认 最新

  • dtpa98038 2017-02-12 21:45
    关注

    Your loop will result in multiple delete_series inputs - within the same form.

    You could create separat forms for each option:

    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条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)