php从mysql删除行

Please look why this code dosen't work, I didn't remove the lines. Connection to server is ok. I can add new rows, but when I press DELETE on selected rows - noting happends. I think that something is wrong in 'for'..

Thank you in advance!

    <?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="test_mysql"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<table width="400" align="center" 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>
<td align="center" bgcolor="#FFFFFF"><strong>Imię</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nazwisko</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Wiek</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Płeć</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['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name_client']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname_client']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['old_client']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['sex_client']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php

// 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=login_success.php\">";
}
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>

To make things work, just replace the code below with the ones in lines 59-69.

if(isset($_POST) && isset($_POST['checkbox']))
{
    $toDelete = $_POST['checkbox'];
    for($i=0; $i<count($toDelete); $i++)
    {
        $del_id = $toDelete[$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=login_success.php\">";
}

Besides, you're better to reconsider your code:

  • I believe the '' in the "delete" query are redundant,
  • mysql_connect() is deprecated,
  • short opening tags (<?) are discouraged,
  • You can delete all the selected items in one SQL query, instead of doing it repeatedly: $idsToDelete = implode(',', $_POST['checkbox']); $sql = "DELETE FROM $tbl_name WHERE id IN ($idsToDelete);";