I tried using javascript method here is the code what I tried and I have also included the result I am get. The table is not getting populated neither the record is been deleted don't know why? I am new to PHP and can't the error. Here is the code please let me know if you can find the error or else explain me the method that I should use..
<?php
$host = 'localhost'; // Host name
$username = 'root'; // Mysql username
$password = ''; // Mysql password
$db_name = 'imail'; // Database name
$tbl_name = 'messages'; // Table name - DO NOT TOUCH
// 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);
?>
<script type="text/javascript">
function selectAll(x) {
for(var i=0,l=x.form.length; i<l; i++)
if(x.form.type == 'checkbox' && x.form.name != 'sAll')
x.form.checked=x.form.checked?false:true
}
</script>
<table width="653" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="651">
<form name="form1" method="post" action="">
<table width="694" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="57" bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td width="57" align="center" bgcolor="#FFFFFF"><strong>MsgId</strong></td>
<td width="176" align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td width="216" align="center" bgcolor="#FFFFFF"><strong>Message</strong></td>
<td width="152" align="center" bgcolor="#FFFFFF"><strong>Subject</strong></td>
</tr>
<?php while ($rows = mysql_fetch_array($result)): ?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['msgid']; ?>]" type="checkbox" id="checkbox[<? echo $rows['msgid']; ?>]" value="<? echo $rows['msgid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['msgid']; ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['username']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['message']); ?></td>
<td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['subject']); ?></td>
</tr>
<?php endwhile; ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
<input type="checkbox" name="sAll" onclick="selectAll(this)" /> (Select all)<br />
</td>
</tr>
<?php
// Check if delete button active, start this
if ( ! empty($_POST['delete'])) {
foreach ($_POST['need_delete'] as $id => $value) {
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `msgid`='.(int)$id;
mysql_query($sql);
}
header('Location: shoutcron.php'); exit();
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>