删除查询不会工作PHP

时间:2017-12-13 04:37:22

标签: php mysqli

我能够使用我的编辑按钮,但是当我尝试单击删除按钮时,它会重新加载页面并且没有任何反应。

HTML

<table width='80%' border=0>
    <tr bgcolor='#CCCCCC'>
        <td>ID</td>
        <td>Voucher</td>

    </tr>
    <?php 

        $vouchlist = mysqli_query($mysqli, "SELECT * FROM vouchers");
    while($resvouch = mysqli_fetch_array($vouchlist)) {         
        echo "<tr>";
        echo "<td>".$resvouch['id']."</td>";
        echo "<td>".$resvouch['voucher']."</td>";
        echo "<td><a href=\"editvoucher.php?id=$resvouch[id]\">Edit</a> | 
                  <a href=\"deletevoucher.php?id=$resvouch[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }
    ?>
</table>      

deletevoucher.php

$id = $_GET['id'];
//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM vouchers WHERE id = $id ");
header("Location:protected_page.php");

编辑:

将此添加到我的deletevoucher中。我得到这个“2Data成功添加。 回到管理页面。“返回。我得到了我点击我的主要php页面的价值。但它仍然没有删除......

$id = $_GET['id'];

// checking empty fields
if(empty($id) ) {                
    if(empty($id)) {
        echo "<font color='red'>voucher id is empty.</font><br/>";
    }


    //link to the previous page
    echo "<br/><a href='~Airways/protected_page.php'>Go Back</a>";
} else { 
    // if all the fields are filled (not empty)             
    //insert data to database
    $result = mysqli_query($mysqli, "DELETE FROM vouchers WHERE (id) = ('$id') ");


    //display success message
    echo $id;
    echo "<font color='green'>Data added successfully.";
    echo "<br/><a href='protected_page.php'>Back to Admin Page.</a>";
}

Table structure

3 个答案:

答案 0 :(得分:0)

<table width='80%' border=0>
    <tr bgcolor='#CCCCCC'>
        <td>ID</td>
        <td>Voucher</td>

    </tr>
    <?php 

        $vouchlist = mysqli_query($mysqli, "SELECT * FROM vouchers");
    while($resvouch = mysqli_fetch_assoc($vouchlist)) {         
        echo "<tr>";
        echo "<td>".$resvouch['id']."</td>";
        echo "<td>".$resvouch['voucher']."</td>";
        echo "<td><a href=\"editvoucher.php?id=$resvouch['id']\">Edit</a> | 
                  <a href=\"deletevoucher.php?id=$resvouch['id']\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }
    ?>
</table> 


$id = $_GET['id'];
//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM vouchers WHERE id = '".$id."' ");
header("Location:protected_page.php");

答案 1 :(得分:0)

您必须检查$ result条件是真还是假,例如     $ result = mysqli_query($ mysqli,&#34; DELETE FROM vouchers WHERE id =&#39; $ id&#39;&#34;);     如果($结果){     标题(&#34;位置:protectedpage.php&#34;);

答案 2 :(得分:-1)

1首先,您应该检查错误。

2您还应该检查是否&#39; id&#39;值已通过,使用,

if(isset($ _ GET [&#39; id&#39;]){

//你的冷漠

}

3您在网址$ resvouch [id]中传递的ID值    应该像这样携带一个列;

$ resvouch [&#39; ID&#39]。

4将您的查询更改为;

{{1}}