Delete the selected checkbox row from database

vaneetagoswami

New member
Joined
Mar 6, 2014
Messages
370
Points
0
Hey I write this code for delete the selected check box values from the database. Yeah I know there are some example already here. But when I take look on them and tried to do as per they done in their question. still doesn't get the success. Can you guys help me with this query. When I click on the on the delete button the page is refresh and message display that records are delete successfully but after that not see any records delete from database

Code:
<html>
	<head>
		<title>
		</title>
		<meta name="" content="">
	</head>
	<body bgcolor="#05fa8c">
		<form action="a.php" method="POST">
			<table style="margin-top: 252px; font-size: larger; font-style: oblique;" align="center" bgcolor="#05fa8c">
				<?php
					include("config.php");
					
					  if (isset($_POST['delete'])) {
					     foreach($_POST['checkbox'] as $del_id){
					         $del_id = (int)$del_id;
					         $sql = 'DELETE FROM `book` WHERE `id` = '.$del_id;
					         mysqli_query($con, $sql);
					         echo "Records delete successfully";
					     }
					 }
						//$id = $_POST['id'];
					
						$sbook = "SELECT * FROM `book`";
						$rnq   = mysqli_query($con,$sbook);
						$count = mysqli_num_rows($rnq);

						if(!$rnq)
						{
							print ("Error: %s\n ". mysqli_error($con));
						}

						echo "<table border=2 table style=margin-top: 252px; font-size: larger; font-style: oblique; bgcolor=#05fa8c>
						<tr>
						<td width=10% height= 10%><b>Book Name:</b></td>
						<td width=10% height= 10%><b>Author Name:</b></td>
						<td width=10% height= 10%><b>Publisher Name:</b></td>
						<td width=10% height= 10%><b>Description</b></td>
						<td width=3% height= 3% align='center'><b>#</b></td>
						</tr>";

						while($row = mysqli_fetch_array($rnq))
						{
							$rid   = $row['id'];
							$bname = $row['bname'];
							$aname = $row['aname'];
							$pname = $row['pname'];
							$desc  = $row['description'];

							echo "<tr>
							<td width=10% height= 10%><b>$bname</b></td>
							<td width=10% height= 10%><b>$aname</b></td>
							<td width=10% height= 10%><b>$pname</b></td>
							<td width=10% height= 10%><b>$desc</b></td>";?>
							<td align='center'><input type="checkbox" name="checkbox[<? echo $row['id']; ?>]" id="checkbox[<? echo $row['id']; ?>]" value="<? echo $row['id']; ?>"></td>
					<?php 	} 
					echo "</table>";
					echo "<input type='submit' name='delete' id='delete' value='Delete' >";
	?>
		</form>
	</body>
</html>
Help me guys.. I tried some different solution but still stuck with this problem.
 

Dopani

Active member
Joined
Mar 11, 2014
Messages
324
Points
28
You should covert your checkbox value to an array

for example

Code:
<input name="checkbox[]" type="checkbox" value="<? echo $row['id']; ?>">
After that

run this querry to delete selected records
Code:
$sql = "DELETE FROM book WHERE id in ";
$sql.= "('".implode("','",array_values($_POST['checkbox']))."')";
Don't use single id as you did above, it consumes more your server resource to run your queries :D

Good luck!
 
Older threads
Latest threads
Replies
0
Views
28
Replies
0
Views
30
Replies
1
Views
39
Replies
3
Views
94
Recommended threads
Replies
1
Views
3,784
Replies
3
Views
1,581
Replies
4
Views
2,437
Top