Hello frends,
I'm trying to create a webpage in which i have records with checkboxes at the end. When i check the boxes and click on delete button, the selected records
should be deleted.
I'm able to create the checkboxes in every row, but not able to write the delete
code.
These r my codes:
<?php
include('dbconntry.php');
$sql="SELECT * FROM properties";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Found the following entries in the database:";
echo "<br />";
echo "<br />";
$result = mysql_query("SELECT * FROM properties");
echo "<table border='1'>
<tr>
<th>House ID</th>
<th>Address</th>
<th>Location</th>
<th>Rent</th>
<th>Deposit</th>
<th>Square Feet</th>
<th>Available</th>
<th>Bed Room</th>
<th>Bath</th>
<th>Family Room</th>
<th>Fireplace</th>
<th>Type</th>
<th>Terms</th>
<th>Carpetting</th>
<th>Garage</th>
<th>Yard</th>
<th>Pet</th>
<th>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['house_id'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['area'] . "</td>";
echo "<td>" . $row['rent'] . "</td>";
echo "<td>" . $row['deposit'] . "</td>";
echo "<td>" . $row['sqft'] . "</td>";
echo "<td>" . $row['available'] . "</td>";
echo "<td>" . $row['br'] . "</td>";
echo "<td>" . $row['bath'] . "</td>";
echo "<td>" . $row['familyrm'] . "</td>";
echo "<td>" . $row['fireplace'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['terms'] . "</td>";
echo "<td>" . $row['carpetting'] . "</td>";
echo "<td>" . $row['garage'] . "</td>";
echo "<td>" . $row['yard'] . "</td>";
echo "<td>" . $row['pet'] . "</td>";
echo "<td align=center><input type = \"checkbox\" name = \"delete[]" . $house_id . "\" id = \"delete[]" . $house_id . "\" value = \"delete\" ></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<br>";
$num=mysql_numrows($result);
echo "Total number of entries: $num";
if(!$result)
{
echo "No data.";
}
?>
<html>
<head><title>Web Database Sample Index</title>
</head>
<body>
<h3>Click on the links below to Alter/Modify the Database</h3>
<p></p>
<ul>
<li><a href="indextry.php">Home</a>
<form action="delete.php" method="post">
<input type="submit" name="delete" id="delete" value="Delete">
</form>
</body>
</html>
This code helps create the checkboxes in every row.
The delete.php code which i'm using is as follows:
<?php
include('dbconntry.php');
$query = "SELECT * FROM properties";
$row= mysql_query($query);
$num_rows = mysql_num_rows( $row);
echo "$num_rows";
echo "<br>";
if($delete){
for($i=0;$i<$num_rows;$i++){
$del_id = $delete[$i];
$sql = "DELETE FROM properties WHERE house_id='$del_id'";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Selected Records Deleted";
echo "<br />";
header("Location: indextry.php");
exit;
mysql_close();
?>
Plz guys help me in resolving this. I've to complete this project soon.
Thanks.