Hi, I am trying to delete multiple rows with select buttons.
But my code does not work.
Could you help me ?
$user_query= "SELECT * FROM user";
$user_result = mysql_query($user_query);
$count=mysql_num_rows($user_result);
echo "<form name='form1' method='post' action=''>";
echo "<table>";
echo "<tr>";
echo "<th scope='col'>#</th>";
echo "<th scope='col'>username</th>";
echo "<th scope='col'>Email</th>";
echo "<th scope='col'>tel</th>";
echo "<th scope='col'>name</th>";
echo "<th scope='col'>surname</th>";
echo "<th scope='col'>type_of_user</th>";
echo "</tr>";
while($row = mysql_fetch_array($user_result))
{
echo "<tr>";
echo '<td width="20"><input name="checkbox[]" type="checkbox" username="checkbox[]" value="'.$row['username'].'"></td>';
echo '<td><b>'.$row['username'].'</b></td>';
echo '<td><a href="mailto:'.$row['e_mail'].'" title="Αποστολή Email">'.$row['e_mail'].'</a></td>';
echo '<td>'.$row['phone_number'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['surname'].'</td>';
if($row['account_type']=='admin')
echo '<td><img src="images/admin.png" width="40" height="45" title="admin"/></td>';
elseif($row['sex']=='man')
echo '<td><img src="images/user.png" width="40" height="45" title="user" /></td>';
elseif($row['sex']=='woman')
echo '<td><img src="images/woman.png" width="40" height="45" title="user" /></td>';
echo '</tr>';
}
echo '<tr><td><input name="delete" type="submit" id="delete" value="Delete"></td></tr>';
echo "</table>";
echo "</form>";
if($delete){
for($i=0;$i<$count;$i++){ // I know that here is the problem
// because I don;t have id
// but how can I correct it ?
$del_username = $checkbox[$i];
$sql = "DELETE FROM user WHERE username='$del_username'";
$result = mysql_query($sql);
}
// if successful redirect
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=ShowUsers.php\">";
}
}