如何在while循环中处理删除

时间:2017-06-10 14:11:17

标签: php mysqli

我有一个代码,如果按下按钮,应该从while循环中删除记录。我通过$ _GET传递值,但即使它显示数组(1),然后deletedID =“1”我的代码仍然无法正常结束直接进入else语句。你能帮帮我吗?

<?php
include "delete.php";

    echo "
    <table>
        <tr>
            <th>xXx</th>
            <th>Expense name</th>
            <th>Expense amount</th>
            <th>Expense date</th>
            <th>Expense month</th>
        </tr>";



//do while
$expResult = "SELECT * FROM expenses";
$result = mysqli_query($conn, $expResult);


  if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while ($row = mysqli_fetch_assoc($result)) {
    //<td><a name='delete' href='?deletedId=$row[id]'>Delete</a></td> 
    //<td><button name'deletedId' method='POST' type='submit' 
  action='".delete($conn)."'>X</button></td>
    echo "
        <tr>

            <td><a name='delete' href='?deletedId=$row[id]'>Delete</a></td> 
            <td>" . $row["expName"]. "</td>
            <td>" . $row["expAmount"]. "</td>
            <td>" . $row["expDate"]. "</td>
            <td>" . $row["expMonth"]. "</td>
        </tr>";

 }

 } else {
 echo "0 results";
 }

    if(isset($_GET['deletedId'])) {
        $expDelete = $_GET["deletedId"];
        $sql = "DELETE FROM expenses WHERE id = '$expDelete'";
        $delete = mysqli_query($conn, $sql);
        echo "<meta http-equiv='refresh' content='0'>"; 
    }else {
        mysqli_error($conn);
        echo "wrong";
    }  

    echo "

    </table>

    ";

?>
</div>

2 个答案:

答案 0 :(得分:0)

在此处更改您的代码

if(isset($_GET['deleteId'])) {

deletedId按钮

时,您发送delete而非Delete

答案 1 :(得分:0)

您的代码问题似乎发生在这段代码中:

if(**isset($_GET['delete']**) > 1) {
    $expDelete = $_GET["deletedId"];
    $sql = "DELETE FROM expenses WHERE id = '$expDelete'";
    $delete = mysqli_query($conn, $sql);
} else {
    echo "wrong";
}  

请执行var_dump($ _ GET [&#39; delete&#39;]);在你的if语句之前。那输出是什么?这是正确的结果吗?如果没有,请使用正确的更改$ _GET [&#39;删除&#39;]。可能是$ _GET [&#39; deleteID&#39;]?我不确定。如果您发布查询字符串,那么我们可以帮助您更好。