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>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</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>

I won't even consider reading your code and helping you out, until YOU consider how you formulate your topic.
I'm not blaming your English, although understanding what your problem is wasn't exactly easy. But you're actually demanding an answer. You should tell us what you tried, and if you suspect anything specific.

Don't put a deadline on free help.

Oh and use code tags to make your code more reader-friendly.

I agree with Excizted, you really shouldn't put a deadline on free help.

You should find that you are experiencing your problem because you are running your 'delete' PHP code AFTER you are populating your HTML table. If you moved your PHP code (line 56 to line 82) to the top of the page (just after line 8) then your problem should be solved.

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?

ok thanx

ok thanx dr........this code works

ok thanx dr........this code works

If solved, please mark this thread solved.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.