0

I can't seem to delete row in database by id in php I think the the id is not passed to the $_POST['delete'] however, the popup "Your data is deleted" is displayed, but the data is not deleted. So I'm not sure where is the error in this code.

I also try to delete the data by its id for example: Delete book where no='4'; and the code seems to run fine because the data is deleted in the database.

<html>

<script>
  function confirmDelete() {
    return confirm('Are you sure?');
  }
</script>


<!DOCTYPE html>

<head>
  <form action="test.php" method="POST">

    <br><br><br>

    <table bordercolor="#FFCC66" align="center" bgcolor="#FFFFFF">
      <tr>
        <th>No</th>
        <th>Title</th>
        <th>Author</th>
        <th>Year</th>
        <th>Donor's Name</th>
        <th>Call Number</th>
        <th>Date Received</th>
        <th>Handled By</th>
        <th></th>
        <th></th>
      </tr>

      <?php
  include ('config.php');

  $view=mysqli_query($conn,"SELECT * FROM book");
  
  
?>
        <?php while($v=mysqli_fetch_array($view)){ ?>
        <tr>

          <td>
            <?php echo $v["no"];?>
          </td>
          <td>
            <?php echo $v["title"];?>
          </td>
          <td>
            <?php echo $v["author"];?>
          </td>
          <td>
            <?php echo $v["year"];?>
          </td>
          <td>
            <?php echo $v["donorname"];?>
          </td>
          <td>
            <?php echo $v["callnum"];?>
          </td>
          <td>
            <?php echo $v["datereceived"];?>
          </td>
          <td>
            <?php echo $v["handledby"];?>
          </td>
          <td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
        </tr>
        <?php
    } ?>

          </tr>
    </table>
    <br><br>

  </form>
  </body>

</html>

<?php

if(isset($_POST['delete']))
{
    
    include('config.php');
    
  $no =$v["no"];
  $d=mysqli_query($conn,"DELETE FROM `book` WHERE no='$no'");
  if ($d)
  {

    echo "<script type='text/javascript'> alert('Your data is deleted!'); </script>";
    echo"<meta http-equiv='Refresh' content='0' >"; 
  }
  else
  {
    echo "<script type='text/javascript'> alert('Your data cannot delete!'); </script>";
  }
     mysqli_close($conn);

}
  
?>

0

3 Answers 3

1

Change the submit element to

<td>
  <input type="submit" name="delete" value="<?php echo $v['no'];?>" onclick="return confirmDelete('Are you sure?');" />
</td>

and

$no = $_POST["delete"];
Sign up to request clarification or add additional context in comments.

Comments

0

Another solution si to add a hidden input with your value.

<td>
    <?php echo $v["no"];?>
    <input type="hidden" value="<?php echo $v["no"];?>" />
</td>

In your php you will find the value in $_POST['no']

This solution is better to pass multiple arguments in POST like a captcha or a confirmation (checkbox).

Comments

0

logic is not correct, while you press the delete button, all the data will be passed along with submitting because your tag is outside of the loop.

As my opinion, you should use ajax like functionality here, or follow this method.

<?php while($v=mysqli_fetch_array($view)){ ?>

<form action="test.php" method="POST">
   <tr>

      <td>
        <?php echo $v["no"];?>
        <input type="hidden" value="<?php echo $v["no"];?>" name="no" >
      </td>
      <td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
    </tr>
</form>
<?php } ?>

and in your post call use $no = $_POST['no']; instead of $no =$v["no"];

Comments

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.