deleting rows using checkboxes!!!!
Moderator: General Moderators
deleting rows using checkboxes!!!!
Hi everyone,
I am a student and have just started with PHP. I have a php page that displays data from MySql in an HTML table. Along with every record that gets displayed, i have included a checkbox and a single delete button at the end. What i want is that when a checkbox is selected and the delete button is clicked, the entire row of the page should get deleted along with the data in mysql. I have the following 'display_notes.php' page that displays the mysql data and a separate 'delete_notes.php' page that does the deletion. I am not achieving what i want. Please help....
********* display_notes.php *****************************
<?php
include "header.php";
?>
<div id="content">
<h2>NOTES</h2>
<form method="POST" action="delete_notes.php">
<?php
$sql="SELECT date, note FROM notes ORDER BY N_ID DESC";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
if ($count == 0) {
echo "<br><br><strong>No notes found in database</strong>";
}
else {
echo '<table style="width: 400px" align="center" border="1px">';
echo '<tr>';
echo '<td style="width: 100px" align="center"><b>Date</b></td>';
echo '<td style="width: 300px" align="center"><b>Note</b></td>';
echo '<td style="width: 100px "align="center"><b>Delete</b></td>';
echo '</tr>';
while ($row = mysql_fetch_array($result))
{
$N_ID = $row["N_ID"];
$date = $row["date"];
$note = $row["note"];
echo '<tr>';
echo '<td style="width: 100px" align="center">'.$date.'</td>';
echo '<td style="width: 300px" align="center">'.$note.'</td>';
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=N_ID></td>';
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
?>
<input type="submit" name="delete" id="delete" value="Delete">
</form>
</div>
<?php
include "footer.php";
?>
*********************** delete_notes.php ************
<?php
include "header.php";
?>
<?php
$query = "SELECT * FROM notes order by N_ID";
$row= mysql_query($query);
$num_rows = mysql_num_rows($row);
if(isset($_POST["delete"]))
{
for($i=0;$i<$num_rows;$i++)
{
$delete_records = $cb[$i];
$sql = "DELETE FROM notes WHERE N_ID=$delete_records";
$result = mysql_query($sql);
}
}
?>
<div id="content">
<br><br><br>
<p>The Record has been deleted from the database. <br><br><br>
<a href="display_notes.php">CLICK HERE</a> to view Notes!</p>
</div>
<?php
include "footer.php";
?>
I am a student and have just started with PHP. I have a php page that displays data from MySql in an HTML table. Along with every record that gets displayed, i have included a checkbox and a single delete button at the end. What i want is that when a checkbox is selected and the delete button is clicked, the entire row of the page should get deleted along with the data in mysql. I have the following 'display_notes.php' page that displays the mysql data and a separate 'delete_notes.php' page that does the deletion. I am not achieving what i want. Please help....
********* display_notes.php *****************************
<?php
include "header.php";
?>
<div id="content">
<h2>NOTES</h2>
<form method="POST" action="delete_notes.php">
<?php
$sql="SELECT date, note FROM notes ORDER BY N_ID DESC";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
if ($count == 0) {
echo "<br><br><strong>No notes found in database</strong>";
}
else {
echo '<table style="width: 400px" align="center" border="1px">';
echo '<tr>';
echo '<td style="width: 100px" align="center"><b>Date</b></td>';
echo '<td style="width: 300px" align="center"><b>Note</b></td>';
echo '<td style="width: 100px "align="center"><b>Delete</b></td>';
echo '</tr>';
while ($row = mysql_fetch_array($result))
{
$N_ID = $row["N_ID"];
$date = $row["date"];
$note = $row["note"];
echo '<tr>';
echo '<td style="width: 100px" align="center">'.$date.'</td>';
echo '<td style="width: 300px" align="center">'.$note.'</td>';
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=N_ID></td>';
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
?>
<input type="submit" name="delete" id="delete" value="Delete">
</form>
</div>
<?php
include "footer.php";
?>
*********************** delete_notes.php ************
<?php
include "header.php";
?>
<?php
$query = "SELECT * FROM notes order by N_ID";
$row= mysql_query($query);
$num_rows = mysql_num_rows($row);
if(isset($_POST["delete"]))
{
for($i=0;$i<$num_rows;$i++)
{
$delete_records = $cb[$i];
$sql = "DELETE FROM notes WHERE N_ID=$delete_records";
$result = mysql_query($sql);
}
}
?>
<div id="content">
<br><br><br>
<p>The Record has been deleted from the database. <br><br><br>
<a href="display_notes.php">CLICK HERE</a> to view Notes!</p>
</div>
<?php
include "footer.php";
?>
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: deleting rows using checkboxes!!!!
You haven't retrieved your checkbox array from the form. You have simply created an empty variable called cb.
You need to get $_POST['cb']:
Now you have the array.
You need to get $_POST['cb']:
Code: Select all
$cb=$_POST['cb'];
Re: deleting rows using checkboxes!!!!
It seems to me that when you have the for loop going almost through all the records.
You can do this, with the previous suggestion of puttin $_post['cb'] into $cb first.
Granted its a simplified version. but in the for loop you specify the number of fields you chose with your checkboxes that means how many array cells there are, for example: 2, it would go through twice with $cb[0] contaning first value and $cb[1] containing another value.
Hope that helped.
You can do this, with the previous suggestion of puttin $_post['cb'] into $cb first.
Code: Select all
<?php
$cb = $_POST['cb'];
for($i = 0; $i < sizeof($cb); $i++)
{
$sql = "DELETE FROM notes WHERE N_ID = " . $cb[$i];
$query = mysql_query($sql);
}
echo "Records have been deleted";
?>Hope that helped.
Re: deleting rows using checkboxes!!!!
First correction:
In display_notes.php: Change
To
You had forgotten to include the $ sign, and since a single quoted string does not parse PHP variables, you have to break the string and concatenate it with the variable.
Second correction: Your delete_notes.php logic is a little wrong, plus as aceconcepts pointed out you aren't initialising $cb to anything.
Heres an alternative:
delete_notes.php
The extended if condition is to ensure that delete is not pressed without any checkboxes checked.
Now, once a user does submit a form with the checkboxes you will have an array $_POST['cb'] which will contain all the values of the checkboxes that were checked (in your case, the NID).
Then we set up a Query string, and keep adding to the $clauses array all the conditions for what to delete. Then we join all the conditions with an OR and append it to the Query string. Thats your Query.
eg. if the user checks IDs 2,4,5 the query will look like:
The (int) typecast is to prevent malicious strings being sent via the form.
In display_notes.php: Change
Code: Select all
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=N_ID></td>';Code: Select all
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=' . $N_ID . '></td>';Second correction: Your delete_notes.php logic is a little wrong, plus as aceconcepts pointed out you aren't initialising $cb to anything.
Heres an alternative:
delete_notes.php
Code: Select all
<?php
include "header.php";
?>
<?php
if( isset( $_POST["delete"] ) && ( count($_POST['cb']) > 0 ) )
{
$sql = "DELETE FROM notes WHERE";
$clauses = array();
foreach($_POST['cb'] as $id)
{
$clauses[] = " NID = " . (int)$id;
}
$sql = $sql . implode(' OR ' , $clauses);
mysql_query($sql);
}
?>
<div id="content">
<br><br><br>
<p>The Record has been deleted from the database. <br><br><br>
<a href="display_notes.php">CLICK HERE</a> to view Notes!</p>
</div>
<?php
include "footer.php";
?>Now, once a user does submit a form with the checkboxes you will have an array $_POST['cb'] which will contain all the values of the checkboxes that were checked (in your case, the NID).
Then we set up a Query string, and keep adding to the $clauses array all the conditions for what to delete. Then we join all the conditions with an OR and append it to the Query string. Thats your Query.
eg. if the user checks IDs 2,4,5 the query will look like:
Code: Select all
DELETE FROM notes WHERE NID = 2 OR NID = 4 OR NID = 5Re: deleting rows using checkboxes!!!!
Thanks to everyone for the suggestions ...i will be working on it now...and will come back if there is further problem in it. thanks a lot once again.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: deleting rows using checkboxes!!!!
Why use a loop 
Code: Select all
$cb = isset($_POST['cb']) ? $_POST['cb'] : array();
if (count($cb)) {
$cb = array_map('intval', $cb);
$sql = 'DELETE FROM foo WHERE id IN ('. implode(', ', $cb).')';
mysql_query....
}Re: deleting rows using checkboxes!!!!
JCart: Ooh, I didn't know the IN keyword, I always used id=1 OR id=5 OR id=9 OR ... Learn something new everyday 
Re: deleting rows using checkboxes!!!!
Hi Verminox,
I made the following changes to my code, but i am not getting the desired output. I am getting the same output again and the records donot get deleted either from the page or from mysql. I hope the changes i made to my previous code is correct. If i am wrong, please give me the correct codes for the two pages.
***********************This is the delete_notes.php page**********
<?php
include "header.php";
?>
<?php
if( isset( $_POST["delete"] ) && ( count($_POST['cb']) > 0 ) )
{
$sql = "DELETE FROM notes WHERE";
$clauses = array();
foreach($_POST['cb'] as $id)
{
$clauses[] = " NID = " . (int)$id;
}
$sql = $sql . implode(' OR ' , $clauses);
mysql_query($sql);
}
?><div id="content">
<br><br><br>
<p>The Record has been deleted from the database. <br><br><br>
<a href="display_notes.php">CLICK HERE</a> to view Notes!</p>
</div>
<?php
include "footer.php";
?>
***********And this is the display_notes.php*****************
<?php
include "header.php";
?>
<div id="content">
<h2>NOTES</h2>
<form method="POST" action="delete_notes.php">
<?php
$sql="SELECT date, note FROM notes ORDER BY N_ID DESC";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
if ($count == 0) {
echo "<br><br><strong>No notes found in database</strong>";
}
else {
echo '<table style="width: 400px" align="center" border="1px">';
echo '<tr>';
echo '<td style="width: 100px" align="center"><b>Date</b></td>';
echo '<td style="width: 300px" align="center"><b>Note</b></td>';
echo '<td style="width: 100px "align="center"><b>Delete</b></td>';
echo '</tr>';
while ($row = mysql_fetch_array($result))
{
$N_ID = $row["N_ID"];
$date = $row["date"];
$note = $row["note"];
echo '<tr>';
echo '<td style="width: 100px" align="center">'.$date.'</td>';
echo '<td style="width: 300px" align="center">'.$note.'</td>';
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=' . $N_ID . '></td>';
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
?>
<input type="submit" name="delete" id="delete" value="Delete">
</form>
</div>
<?php
include "footer.php";
?>
I made the following changes to my code, but i am not getting the desired output. I am getting the same output again and the records donot get deleted either from the page or from mysql. I hope the changes i made to my previous code is correct. If i am wrong, please give me the correct codes for the two pages.
***********************This is the delete_notes.php page**********
<?php
include "header.php";
?>
<?php
if( isset( $_POST["delete"] ) && ( count($_POST['cb']) > 0 ) )
{
$sql = "DELETE FROM notes WHERE";
$clauses = array();
foreach($_POST['cb'] as $id)
{
$clauses[] = " NID = " . (int)$id;
}
$sql = $sql . implode(' OR ' , $clauses);
mysql_query($sql);
}
?><div id="content">
<br><br><br>
<p>The Record has been deleted from the database. <br><br><br>
<a href="display_notes.php">CLICK HERE</a> to view Notes!</p>
</div>
<?php
include "footer.php";
?>
***********And this is the display_notes.php*****************
<?php
include "header.php";
?>
<div id="content">
<h2>NOTES</h2>
<form method="POST" action="delete_notes.php">
<?php
$sql="SELECT date, note FROM notes ORDER BY N_ID DESC";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
if ($count == 0) {
echo "<br><br><strong>No notes found in database</strong>";
}
else {
echo '<table style="width: 400px" align="center" border="1px">';
echo '<tr>';
echo '<td style="width: 100px" align="center"><b>Date</b></td>';
echo '<td style="width: 300px" align="center"><b>Note</b></td>';
echo '<td style="width: 100px "align="center"><b>Delete</b></td>';
echo '</tr>';
while ($row = mysql_fetch_array($result))
{
$N_ID = $row["N_ID"];
$date = $row["date"];
$note = $row["note"];
echo '<tr>';
echo '<td style="width: 100px" align="center">'.$date.'</td>';
echo '<td style="width: 300px" align="center">'.$note.'</td>';
echo '<td style="width: 100px" align="center"><input type=checkbox name=cb[] value=' . $N_ID . '></td>';
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
?>
<input type="submit" name="delete" id="delete" value="Delete">
</form>
</div>
<?php
include "footer.php";
?>
Re: deleting rows using checkboxes!!!!
Hi everyone,
I am not able to get my php checkbox deletion script correct. Please help cz i am in desperate need of these....
I am not able to get my php checkbox deletion script correct. Please help cz i am in desperate need of these....
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: deleting rows using checkboxes!!!!
What does cz mean? Condition Zero? Cold Zoo? Just joking, but pleas stop using AOL speak. Also, please use code tags when posting code, and lastly please stop formatting your entire posts in color. Now.. onto your problem...pritam79 wrote:Please help cz i am in desperate need of these....
Have you checked your queries aren't returning errors, i.e.
Code: Select all
mysql_query($sql) or die(mysql_error())Have you tried my solution I posted earlier?