如何使用单选按钮删除一行数据(PHP)

时间:2014-11-09 10:56:48

标签: php mysql button radio

 <?php
      $con=mysqli_connect("127.0.0.1", "user", "pass","mydatabase");
        // Check connection
        if (mysqli_connect_errno())
           {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
           }
              $result = mysqli_query($con,"SELECT * FROM mytable");

        // create form
              echo '<form action = "deleteuser.php" method = "POST" name = "mydeleteform">';
              while($row = mysqli_fetch_array($result))
              {
                echo '<input type = "radio" style = "inline" name ="selectdelete">';
                echo "First name: ". $row["firstname"]. " Last name: ". $row["lastname"]. 
                     " Age: " . $row["Age"] . " Email: " . $row["email"]. " Phone number: 
                     ".$row["Phonenumber"]."<br>";
              } 
                echo '<input type = "submit" value = "DELETE" name = "submitdelete">';
                echo '</form>';
 ?>
 <?php ?>

所以现在这个工作,这个php代码的主要目的是从phpmyadmin中提取数据并在浏览器中显示它。

现在我的问题是每个数据显示所有数据都有一个单选按钮。

我的目标是=如果使用单击单选按钮,它将删除整个勾选数据行。我试过但它只显示已删除但实际上它仍然在phpmyadmin的表中。

1 个答案:

答案 0 :(得分:0)

为每个提供值的单选按钮使用唯一值,就像数据库表主键

一样
             while($row = mysqli_fetch_array($result))
              {
                echo '<input type = "radio" style = "inline" name ="selectdelete"     value="'.$row['idOfYourTable'].'>"';
                //idOfYourTable is your primary key of table
                echo "First name: ". $row["firstname"]. " Last name: ". $row["lastname"]. 
                     " Age: " . $row["Age"] . " Email: " . $row["email"]. " Phone number: 
                     ".$row["Phonenumber"]."<br>";
              } 
                echo '<input type = "submit" value = "DELETE" name = "submitdelete">';
                echo '</form>';
                ?>

            **deleteuser.php**

        $selectedRow=$_POST['selectdelete'];

并删除表格中的行,其中idOfYourTable = $ selectedRow like -

        $sql="DELETE FROM TABLE WHERE idOfYourTable='$selectedRow'";