Delete the selected checkbox row from database

vaneetagoswami

Well-known member
Registered
Joined
Mar 6, 2014
Messages
140
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

Well-known member
Registered
Joined
Mar 11, 2014
Messages
239
Points
18
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
Replies
15
Views
9,536
Replies
6
Views
5,002
Replies
6
Views
3,490
Newer Threads
Replies
3
Views
3,724
Replies
5
Views
7,574
Replies
5
Views
5,852
Latest Threads
Replies
0
Views
234
Replies
1
Views
167
Replies
1
Views
150
Replies
2
Views
732
Replies
1
Views
483
Recommended Threads
Replies
5
Views
3,732
Replies
3
Views
4,000
Replies
12
Views
8,809

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top