deleting rows using checkboxes!!!!
Posted: Thu May 01, 2008 11:56 pm
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";
?>