0

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>

Edited by Ezzaral: Fixed code tags.

4
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by JRM
0

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.

Edited by happygeek: fixed formatting

0

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.

0

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?

0

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

If solved, please mark this thread solved.
Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.