deleting records using checkbox only then i click button twice
hi.........
my php code for deleting records from database is working succesfully but while deleting records from database using checkbox is that only records deleted when deleting button click on two time.... means clicking delete button twice delete the record...... plz chk my code nd tell whr is mistake??????????????
plz reply me within 24 hrs
<?php
$dbhost="localhost";
$dbuser="root";
$dbpass="root";
$dbname="test";
mysql_connect("$dbhost","$dbuser","$dbpass") or die('Could not connect');
mysql_select_db($dbname);
?>
<form name="form1" method="post" action="">
<table width="918" border="0">
<tr>
<td> sno</td>
<td>FirstName</td>
<td>LastName</td>
<td>Age</td>
</tr>
<?php
$query="SELECT * FROM student";
$result=mysql_query($query);
$sno=1;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
?>
<tr>
<td><? echo $sno;?></td>
<td><? echo $row['FirstName'];?></td>
<td><? echo $row['LastName'];?></td>
<td><? echo $row['Age'];?></td>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['LastName']; ?>"></td>
</tr>
<?
$sno=$sno+1;
}
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><div align="center">
<input name="delete" type="submit" id="delete" value="Delete Records">
</div></td>
</tr>
</table>
<?
if($_POST['delete']){
//print_r($_POST);
$checkbox=$_POST['checkbox'];
//exit;
for($i=0;$i<count($checkbox);$i++){
$FirstName = $checkbox[$i];
$sql = "DELETE FROM student WHERE FirstName='$FirstName'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
/*if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletemultiplerecords.php\">";
} */
}
?>
</form>
preetg
Junior Poster in Training
95 posts since Jan 2010
Reputation Points: 11
Solved Threads: 4
I don't know how it deletes anything as it is.
The query should fail.
The value for the checkbox is given as the last name here:
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['LastName']; ?>"></td>
But... the query is using the last name to match a first name here.
$FirstName = $checkbox[$i];
$sql = "DELETE FROM student WHERE FirstName='$FirstName'";
How the heck does that work unless the first and last names are the same?
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
preetg
Junior Poster in Training
95 posts since Jan 2010
Reputation Points: 11
Solved Threads: 4
ok thanx dr........this code works
preetg
Junior Poster in Training
95 posts since Jan 2010
Reputation Points: 11
Solved Threads: 4
ok thanx dr........this code works
If solved, please mark this thread solved.
Thanks
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75