954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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>&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>
preetg
Junior Poster in Training
95 posts since Jan 2010
Reputation Points: 11
Solved Threads: 4
 

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.

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

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.

webbespoke
Newbie Poster
10 posts since Jan 2010
Reputation Points: 9
Solved Threads: 1
 

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
 

ok thanx

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You