Jump to content

(SOLVED)Why wont this work ,help please


just me and php

Recommended Posts

This displays all tables and fields nicely , if i check box and click the delete button it acts like everything is ok no errors and it refreshes back to its own page but nothing was deleted , can someone see why??
Thanks :)
[code]
<?php
require "configdelete.php";
$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="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

</tr>
<?php
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['recno']; ?></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>
[/code]
Link to comment
Share on other sites

Hmm that didnt change anything , had same results but?

should this
[code]$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";[/code]

Be this?
[code] $sql = "DELETE recno, Username, track, HL_F, HL_MS, online, disp, PCLabel, PCValid, PC, date, display, hacked, MP, osFROM $tbl_name WHERE id='$del_id'";[/code]

With the
replace  if($delete){

With    if ($_POST['delete']){




Link to comment
Share on other sites

I think your logic is a little off. Should be something like...

[code=php:0]
if($_POST['delete']){
    foreach($_POST['checkbox'] as $del_id){
        $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
        if (mysql_query($sql))
            echo "Deleted $del_id";
        }
    }
}
[/code]
Link to comment
Share on other sites

Does it say "Deleted X", where X is a $del_id?

If not, there are 3 ways it could fail.  Either the query failed, $_POST['checkbox'] is empty or $_POST['delete'] is not true.

Try this:

[code]var_dump($_POST['delete']);
var_dump($_POST['checkbox']);[/code]

just before the if ($_POST['delete']);

Also, add an "else" case to the check for query failure:

[code]... } else {
  echo "Query $sql failed! Error was " . mysql_error();
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.