Delete button not functioning

I created a that submits straight into A database example with a table in the database named validation. After the form it’s been submitted I retrieve the data submitted from the database. I had no problems with the retrieving of data from the Database to a page I created and named it Receive.php. I created a Do While Loop , so that every row of data will have a delete Button and an Edit button. The problem am facing is my delete button don’t seem to be functioning and I also want to use PhP only in making it work thus no JavaScript or jquery etc would or should be used.
[size=12pt]My code is as follows :[/size]

[php] <?php

   include 'sql.php'; 
   $query = "SELECT * FROM validation";
  mysqli_query($con , $query);
$result = mysqli_query($con , $query); 
$tottals = mysqli_num_rows($result);
     
     
     
   if (isset($_POST['del'])){ 
      
      $id = mysqli_real_escape_string($_POST['ID']);
      $query1 = "DELETE FROM validation WHERE ID = '$id' "; 
      $results = mysqli_query($con , $query1); 
      }
     ?>

Personal Details of Customers

     </tr>

  
    <?php 
    while ($rows = mysqli_fetch_assoc($result)) 
{ 
echo"
 
  
  <tr> 
  
  <td bgcolor="#FFFFCC"><input type="radio" name="ID" value=" $rows['ID']"></td>
  <td bgcolor="#FFFFCC">  $rows ['ID']</td> 
  <td bgcolor="#FFFFCC">  $rows ['Name'] </td> 
  <td bgcolor="#FFFFCC">  $rows ['Email']</td>
  <td bgcolor="#FFFFCC">  $rows ['Website']</td> 
  <td bgcolor="#FFFFCC">  $rows ['Comment']</td> 
  <td bgcolor="#FFFFCC">  $rows ['Time']</td>  
  
   <td>&nbsp;</td>
   <td> <input type="submit" name="del" value="Delete" /> &nbsp; <input type="button" name="edit" value="Edit"  /> </td>
   </tr>
    ";  
  } ?>;  

 </table>
[/php]
  Full Name Email Address Website Comment Time
No JavaScript would or should be used

So it’s homework.

You have two options, being as neither your edit or delete buttons will work or could work.

  1. Make the buttons links, with the id being passed so you know what to delete or edit. ( prefered )

  2. Make a form for each row. Then a delete/ update is tied to a specific row. Not the way to do it, but you could.

  3. Use checkboxes and only have a single form with submit button.

i did but am having problems with my loop and its still not deleting…

[php]

<?php include 'sql.php'; $query = "SELECT * FROM validation"; $result = mysqli_query($con , $query); $rows = mysqli_fetch_assoc($result) ; $totals = mysqli_num_rows($result) ; ?>
        <div id="css">
<form >
   <table width="80%" border="0" cellpadding="2" cellspacing="2" > 
      <caption><h2>Personal Details of Customers</h2></caption> 
      <tr class="white"> 
          <td bgcolor="#330033"> &nbsp; </td>
          <td bgcolor="#330033"> Id Number </td>
          <td bgcolor="#330033"> Full Name </td>
          <td bgcolor="#330033"> Email Address </td>
          <td bgcolor="#330033"> Website </td>
          <td bgcolor="#330033"> Comment </td> 
          <td bgcolor="#330033"> Time </td>   
<?php while($rows=mysqli_fetch_assoc($result) {
  <input type="raido" name="ID" value="<?php echo $rows['ID']; ?>"  />
  <td bgcolor="#FFFFCC"><?php  echo $rows['ID'];?></td> 
  <td bgcolor="#FFFFCC"><?php  echo $rows['Name'];?> </td> 
  <td bgcolor="#FFFFCC"><?php  echo $rows['Email'];?></td>
  <td bgcolor="#FFFFCC"><?php  echo  $rows['Website'];?></td> 
  <td bgcolor="#FFFFCC"><?php  echo $rows['Comment'];?></td> 
  <td bgcolor="#FFFFCC"><?php  echo $rows['Time'];?></td>  
  
   <td>&nbsp;</td>
 
   <td> <a href="delete.php? ID= "$rows[ID]" /"> <input type="submit" name="del" value="Delete" /> </a> &nbsp;                                                     <input type="button" name=   "edit" value="Edit"  /> </td> 
   </tr>
          } ?>[/php]

 </table>

Where is the code for delete.php?

When I said you had two options, that means pick one, not use both.

When a user clicks on the anchor tag, (tag not submit button), it will redirect to delete.php and pass the get value of the id that they want removed. You will need to see IF there is a get variable named “ID” and if there is what do you want to do with it?

here is it delete.php. Please do add a little coding for more understanding.

[php]<?php
include ‘sql.php’;
if (isset($_POST[‘del’])){
$id = mysqli_real_escape_string($_POST[‘ID’]); // COMMENT number 2 ( I know theyre out of order but it was easier to explain this way around )
$query1 = "DELETE FROM validation WHERE ID = ‘$row[ID]’ LIMIT 1 ";
$results = mysqli_query($con , $query1);
}
?>[/php]

[php]<a href="delete.php? ID= “$rows[ID]” [/php]

You passing the ID in the URL. Which means that you are using $_GET, not $_POST. So, you are looking in the wrong area for a value. Furthermore, your $_GET variable is named ID not del.

Honestly, step back, work your way through some more tutorials, more books, and learn before you create your own project. You are missing out on a lot, because you think it will come to you when you need it.

If you want to use POST then you could something like the following:

[php]<?php
$record = filter_input(INPUT_POST, ‘delete’, FILTER_SANITIZE_SPECIAL_CHARS);

if (isset($record)) {
echo “Record Number " . $record . " was deleted
\n”;
}

$id = 1024;
?>

Untitled Document Delete [/php]

You could even throw in a little JavaScript in as well. :wink: I think it would pass markup validation, but I’m not totally sure, but I was shown this by a college instructor that I had for Web Development (Well not the exact same thing, but similar).

Sponsor our Newsletter | Privacy Policy | Terms of Service