0

I wanted a row or multiple rows to be deleted when they are selected via check boxes. But it doesn't seem to delete anything even if I select.

Here's what I come up with:

<?php
$con=mysqli_connect("localhost","root","","annualdb");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM asean_japan WHERE agency='Air'");

if(isset($_POST['delete'])) {
  $checkbox = $_POST['checkbox'];

  for($i=0;$i<count((array)$checkbox);$i++) {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM asean_japan WHERE id='$del_id'";
    $result = mysqli_query($con,$sql);
  }
// if successful, refresh same page 
if($result) {
  echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
<table class='page'>
  <tr>
    <th>Select</th>
    <th>Agency</th>
    <th>FileName</th>
    <th>FileType</th>
    <th>Date Received</th>
    <th>Action</th>
  </tr>
<?php
while($row = mysqli_fetch_array($result))
{
  ?>
  <tr>
    <td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' value='<?php echo $row['id']; ?>'></td>
    <td><?php echo $row['agency']; ?></td>
    <td><?php echo $row['filename']; ?></td>
    <td><?php echo $row['filetype']; ?></td>
    <td><?php echo $row['date']; ?></td>
    <td><a target='_blank' href='../annual/indicators/" <?php echo $row['filename']; ?>"'>OPEN</a></td>
  </tr>
</table>

<?php
}
?>
<tr>
  <td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>
<?php
mysqli_close($con);
?>

Is there a problem with my checkbox or the input=delete maybe?

2
  • Did you do Debugging? i.e Are you getting anything in $_POST['checkbox']? Is your $del_id getting specific selected checkbox value? whenever you ask a question please do debug so the actual problem will uncover Commented Apr 3, 2019 at 3:04
  • Yes I tried debugging, no error appears. But no results as well. it does not delete anything Commented Apr 3, 2019 at 3:20

2 Answers 2

1

first you need to close table tag outside the while loop and then you need to check onclick your data post or not to target url click here this will help you

Sign up to request clarification or add additional context in comments.

Comments

0
    foreach($_POST['checkbox'] as $key =>$val) {
        if($val!='') {
             $sql = "DELETE FROM asean_japan WHERE id='$val'";
             $result = mysqli_query($con,$sql);  
        }
    }

4 Comments

i think you missed the form tag
new to php but I understand this code deletes row when checkbox are not empty or checked right? But still it doesn't do anything. Can't figure out why
<form name="test" id="test" method="post"><table>.......</table>SUBMIT BUTTON</form>
Please add some explanation to your code such that others can learn from it. Additionally, have a look at SQL injections - your code is highly vulnerable and should not be used in production!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.