I am having a problem using the script below. I want to be able to built a database table in html (using an 'id' to select which) and then be able to delete the rows selected by the checkboxes.
The building of the table works perfect. But when selecting row's and pressing delete the screen goes to blank and nothing has been deleted, the 'test' in the if($_POST) never appears.. i cant find my error! so i hope someone can help me!!
Gerald
The code:
<?php
if ($_POST['submit']) {
$id = $_POST['id'];
$i = 0;
include("verbinding.php");
mysql_connect("$dbhost", "$username", "$dbww")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$tabellen = mysql_list_tables("$dbname") or die(mysql_error());
while ($i < mysql_num_rows($tabellen)) {
$t_name[$i] = mysql_tablename ($tabellen, $i);
if ($id == $i) {
$tbl_name = $t_name[$i];
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$fields_num = mysql_num_fields($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Table: <?php echo $t_name[$i]; ?></strong> </td>
</tr>
<tr>
<td bgcolor='#FFFFFF'>#</td>
<?php
for($k=0; $k<$fields_num; $k++) {
$field = mysql_fetch_field($result);
echo "<td bgcolor='#FFFFFF'>{$field->name}</td>";
}
while($row = mysql_fetch_row($result)) {
echo "<tr>";
?>
<td align="center" bgcolor="#FFFFFF"><form method="post" action="<?php echo $PHP_SELF;?>"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<?php
foreach($row as $cell)
echo "<td bgcolor='#FFFFFF'>$cell</td>";
echo "</tr>\n";
}
?>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
<td colspan="2" align="center" bgcolor="#FFFFFF"><input name="back" type="submit" id="back" value="Back"></td></form>
</tr>
<?php
$checkbox = $_POST[checkbox];
if($_POST['delete']){
echo "test";
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=change.php\">";
}
}
if($_POST['back']){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">";
}
}
$i++;
}
mysql_close();
}
?>
</table>
</td>
</tr>
</table>