0

I have a while loop displaying my database columns and rows in the table.

I want to make a button which can delete specific MySQL row but unfortunately, I can't select which of all rows have to be deleted simply because I use "while loop" to display my database content in HTML.

    <?php
    $db=mysqli_connect("localhost","root","root","done");
    if(isset($_POST['deletesubmit'])){
    $deleteId=$_POST['deleteId'];
    $query3="DELETE FROM dones WHERE  id=deleteId";
    $result3=mysqli_query($db,$query);
    }
    ?>


   <?php

    $query="SELECT * FROM dones where id>3";
    $result1=mysqli_query($db,$query);

   while($row=mysqli_fetch_array($result1))
   {

   echo '<div class="accordion" id="accordionExample">';
   echo '<div class="card" style="margin-left:10px; margin-right:10px;">';
   echo '<div class="card-header">';
     echo '<h5 class="mb-0">';
     echo '<form action="" method="POST" enctype="multipart/form-data" 
   style="height:430px;">';
      echo  '<button class="btn btn-link" style="text-decoration:none; font- 
   family:Times New Roman, Times, serif; font-size:18px; " type="button" 
   data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" 
   aria-controls="collapseOne">';
      echo   $row['title'];

       echo '</button>';
      echo '</h5>';
    echo '</div>';

   echo '<div id="collapseOne" class="collapse show" aria- 
   labelledby="headingOne" data-parent="#accordionExample">';
   echo  '<div class="card-body">';

   // Content Of new banner


   echo '<h5 style="text-align:left; margin-left:20px; margin-top:20px;"> 
   <small>Title</small></h5>';
   echo '<input style="width:290px; margin-left:158px; height:30px; margin- 
   top:-30px;" name="title" type="text" class="form-control" 
   placeholder="Title">';
   echo ' <br>';
   echo ' <div class="custom-file" style="height:10px; width:290px; margin- 
   right:1050px; top-padding:20px; float:right;">';
   echo ' <input type="file" name="uploadfile" class="custom-file-input" 
   id="validatedCustomFile">';
   echo ' <label class="custom-file-label" for="validatedCustomFile">Choose 
   file...</label>';
   echo ' </div>';
   echo ' <h5 style="text-align:left; margin-left:20px;"><small>File</small> 
   </h5>';
   echo ' <br>';
   echo ' <h5 style="text-align:left; margin-left:20px;"><small>Banner 
   Image</small></h5>';
   echo ' <div style="height:130px; width:290px; border-width:1px; border- 
   style:solid; float:right; margin-right:1050px; margin-top:-15px;">';


     $sql="SELECT image FROM dones where status=1";
     $result=mysqli_query($db,$sql);
     $data = mysqli_fetch_array($result);
     echo '<img src= "images/'.$data['image'].'" style="height:130px; 
     width:290px;">';

     echo '</div>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';  
     echo '<h5 style="text-align:left; margin-left:20px;"> 
     <small>Status</small></h5>';
     echo '<select name="status" id="" style="width:290px; float:right; 
     margin-right:1050px; margin-top:-25px;">';
     echo '<option value="1" name="enable">Enable</option>';
     echo '<option value="0" name="disable">Disable</option>';
     echo '</select>';
     echo '<br>';
     echo '<br>';
     echo '<button type="submit" name="tbsubmit"  style="margin-left:250px;" 
     class="btn btn-primary">Save Changes</button>';  
     echo  '<a name="deleteId" href="?deleteId=$row[keyID]"></a>';
     echo '<button type="submit" name="deletesubmit"  style="margin- 
     left:250px;"  class="btn btn-primary">Delete Banner</button>'; 
     echo '</div>';

     echo '</form>';
     echo '</div>';



    echo  '</div>';
   echo '</div>';
   echo '</div>';
   echo '</div>';

    }    

   ?>

So any idea to solve the problem.

4
  • This could help you: Delete MySQL row from PHP while loop Commented Oct 4, 2018 at 6:26
  • Dear you are using query string to pass row id echo '<a name="deleteId" href="?deleteId=$row[keyID]"></a>'; so dony use submit buttion <a> tag is sufficent to pass Id. i am recommend use seprate page delete.php. echo '<a name="deleteId" href="delete.php?deleteId=$row[keyID]"></a>'; <?php $db=mysqli_connect("localhost","root","root","done"); if(isset($_GET['deleteId'])){ $deleteId=$_GET['deleteId']; $query3="DELETE FROM dones WHERE id='".mysqli_real_escape_string($deleteId,$db)."'"; $result3=mysqli_query($db,$query); } ?> Commented Oct 4, 2018 at 6:35
  • What changes I must bring in this page without creating another page Commented Oct 4, 2018 at 7:26
  • you also made here mistake $query3="DELETE FROM dones WHERE id=deleteId"; instead of $query3="DELETE FROM dones WHERE id=" .$deleteId ."; and I didn't read your full code. Commented Oct 8, 2018 at 12:21

1 Answer 1

0
```
<?php
include 'config.php';//connection
?>

<?php
$sql = "select * from tablename";
$result = mysqli_query($con,$sql);
$num=mysqli_num_rows($result);
if($num > 0)
{ 
while($row = mysqli_fetch_array($result))
{ 
$id=$row[0];
$title=$row[1];
$info=$row[2];
?>


<tr>
<td><?php echo $title;?></td>
<td><?php echo $info; ?></td>
<td><form action="#" method="post">
<input type="text" name="id[]" value="<?php echo $id; ?>" hidden="true">
<button type="submit" name="dlt">Delete</button>
</form></td>
</tr>



<?php
}
}
if(isset($_POST['dlt']))
{
foreach ($_POST['id'] as $key => $value)
{
$id=$value;
}
$query = "delete from tablename where id='".$id."'";
if(mysqli_query($con,$query))
{
echo "<script>
alert('row Deleted..');
</script>";
}
}
?>  

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

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.