Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql multiple delete question using checkbox

Status
Not open for further replies.

SysAdmMke

Technical User
Jan 18, 2006
34
US
Can anyone tell me why the code below will not delete the records I check? I went though the code several times and just don't understand it. Can anyone help?

Thanks
Mike




<?php
$link = mysql_connect("xxxx.org", "xxxxx", "xxxxx");
$tbl_name="signups";
mysql_select_db("ms150", $link);
// Query the database and get the count
$result = mysql_query("SELECT * from signups",$link);
$num = mysql_num_rows($result);
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="5" bgcolor="#FFFFFF"><strong><center>MS 150 Signups</Center></strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>CallSign</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>FirstName</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>LastName</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
echo $sqll;
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Callsign']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
 
Because at the time the code reaches the line:

if($delete){

it has no input.

A web script produces HTML output, then stops running completely. The browser renders the HTML and allows the user to interact with the HTML elements. If form data is submitted to a script, that script accepts the input and processes it.

I often write scripts that on the first run output produces a form that submits back to the script. On successive runs that have HTML form input submittted to it, it processes that input. Typically, a script like this is of the form:

[tt]<?php
if (isset ($_POST['some form field'])
{
//process the input
}
else
{
//produce an HTML form that includes the field looked-
//for in the if-statement above
}[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214,

I am not understanding why the checkboxes are not being detected. The way the form works is that you put checks in the boxes you want to delete and it submits them to the script. Why isn't the data available after I hit the delete button. I am a just learning MYSQL and PHP so a bit of a novice.

Thanks Mike
 
you are using the variable $count to limit the for loop, however i see nowhere in your code where you actually initialize $count. does it have valid number?
Code:
for($i=0;$i<[red]$count[/red];$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I do have it initalized, the line that says
$count=mysql_num_rows($result);

this is the result of the query which contains how many records.

 
in your iteration you are reference $checkbox[] but there is no evidence suggesting where $checkbox comes from. unless you have register globals turned on (which would be a "bad thing") you would reference either $_POST['checkbox'][] or its $_GET alternative depending on whether the form was sent in the url or via the POST method. your code suggests the latter.

i don't think that you should assume that all user agents make use of the value within the checkbox html. i believe that a number of them simply pass back a 1 if the checkbox is checked. nothing is passed if the box is unchecked.

i would store the ID of each checkbox as the variable key. i also would not iteratively run sql queries to perform your deletes. i would use the mysql IN construct and build a sensible string for it to run against.

e.g
Code:
if (isset($_POST['checkbox'])){
  $in = array();
  foreach ($_POST['checkbox'] as $key=>$val){
   //assemble an array to use later
   $in[] = "'".mysql_escape_string(trim($key)) ."'"; 
  }
  $sql = "Delete from $tbl_name where id IN (".impode(",", $in) . ")";
}

do not retrieve the whole dataset just to count the number of rows. use count() instead
Code:
$num_rows = mysql_result(mysql_query("Select count(*) from $tbl_name"), 0,0);
 
Ok thanks.. I will try that. Appreciate the help.
 
Also, be wary with check box behavior. (I feel like Im always saying this) [becuase I went through much head pounding to find this out] when the HTML is turned over to process by php the checkboxes are either "on" or null. make sure to iterate through your checkboxes and either assign off, or i changed them all to boolean values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top