delete data from database using checkbox
Posted: Mon Jan 22, 2007 4:25 am
hi everyone,
i have to delete the data from the database.but there is one problem, when i checked any checkbox,all data are deleted from database.here is my code.please help me.
i have to delete the data from the database.but there is one problem, when i checked any checkbox,all data are deleted from database.here is my code.please help me.
Code: Select all
<?php
$con = mysqli_connect("localhost", "root", "admin","paras");
if (!$con)
{
die('Could not connect: ' . mysqli_errno($con));
}
$sql="select Name, Email, Address, Country, City, Zip, Phone, Mobile, Message,f_id from feedback";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_errno($con));
}
$result= mysqli_query($con, $sql);
$thispage = $_SERVER['PHP_SELF'];
echo "<form method='post'action= $thispage>";
echo "<table border = '1' align='center'>
<br><br>
<tr><th>S.No.</th><th><input type = 'Submit' name = 'Submit' value = 'Delete' ></th>
<th>Name</th> <th>Email</th><th>Address</th><th>Country</th><th>City</th><th>Zip Code</th><th>Phone</th><th>Mobile</th><th>Message</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['f_id'] . "</td>";
echo "<td><input type = 'checkbox' name = 'chk' value = '1' ></td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['Zip'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Message'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
// echo $thispage;
if( $_POST['Submit'] = 'Delete')
{
$con1 = mysqli_connect("localhost", "root", "admin","paras");
if (!$con1)
{
die('Could not connect: ' . mysqli_errno($con1));
}
$sql1="select f_id from feedback";
if (!mysqli_query($con1, $sql1))
{
die('Error: ' . mysqli_errno($con1));
}
$n = mysqli_num_rows($result1);
// echo $n;
for ($j = 0; $j<=$n; $j++)
{
$c[$j] = $_POST['chk'];
// echo $c[$j];
if ($result1 = mysqli_query($con1, $sql1))
{
// $i = $j;
while ($row1 = mysqli_fetch_array($result1))
{
$a[$j] = $row1[0];
// echo $a[$j];
// echo $row1[1];
// echo $a[0];
if ($c[$j] == 1)
{
echo "<input type= 'text' name = 't' value = ''>";
echo "<br>";
//echo $row1[$i];
$d = "delete from feedback where f_id = '$a[$j]'";
mysqli_query($con1, $d);
}
}
// $i++;
}
}
}
echo "</form>";
mysqli_close($con1);
?>