-1

I am able to get my edit button working but when i try and click the delete button it reloads the page and nothing happens.

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");

EDIT:

added this to my deletevoucher. i get this "2Data added successfully. Back to Admin Page." returned. i get the value that i click on my main php page. but it still is not deleteing...

$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

Steph
  • 31
  • 5

3 Answers3

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");
Raman
  • 624
  • 5
  • 13
0

You have to check if the $result condition is true or false e.g $result = mysqli_query($mysqli, "DELETE FROM vouchers WHERE id = '$id'"); if($result){ header("Location: protectedpage.php");

-1

1 First, you should check for errors.

2 You should also check if 'id' value is been passed, using,

If (isset($_GET['id']){

// your cold here

}

3 The id value you passed in your url $resvouch[id] Should carry a single column like this;

$resvouch['id'].

4 Change your query to;

"DELETE FROM vochers WHERE id = '$id.";
  • No errors appear, nothing appears with isset no matter what i put. and my code 500s when i single column it – Steph Dec 13 '17 at 05:24