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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)