<?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>
$checkbox = $_POST[checkbox]; //Problem #1 if($_POST['delete']){ //Problem #2 echo "test"; //Nice Debugging But This Should Fix It So We'll Remove It for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; //Problem #3 $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; //And to save a line We'll condense this $result = mysql_query($sql);
$checkbox = $_POST['checkbox[]'];
$_POST['checkbox[]'] holds the value.
if($_POST['delete']=="Delete"){
$_POST['delete'] with the value "Delete".$checkbox will contain the id that you seek in this line.
$result = mysql_query("DELETE FROM {$tbl_name} WHERE id='".$del_id."'");
if($_POST['delete']) { $checkbox = $_POST[checkbox]; for($i=0;$i<$count;$i++){ mysql_query("DELETE FROM $tbl_name WHERE id='".mysql_real_escape_string($del_id)."'");
mysql_query("DELETE FROM $tbl_name WHERE id='".mysql_real_escape_string($del_id)."'");
if($_POST['delete']=="Delete") { $checkbox = $_POST['checkbox[]']; for($i=0;$i<$count;$i++) { $del_id = $checkbox[$i]; $result = mysql_query("DELETE FROM $tbl_name WHERE id='.$del_id'") or die(mysql_error()); } if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=change.php\">"; }

$result = mysql_query("DELETE FROM $tbl_name WHERE id='".mysql_real_escape_string($del_id)."'") or die("Query Error!");
mysql_real_escape_string() is so that malicious users can't perform a MySQL Injection attack on your script (Looking at it now it seems pretty vulnerable). And the reason for the quotes is basic PHP. When you put a function result in a string you have to put a quote that started the string (In you case the ", double quote, from "DELETE) to tell PHP execute this and then a period, the function, and another period and quote. In your case you also need to include the MySQL ' and ' (single) quotes so that MySQL determines that $del_id .$_POST[] variables are not arrays, they are strings (or integers, etc.). So in this case you will need to change the code that creates the checkboxes. I also determined that their are problems way beyond the scope of this post. For one, you add a meta tag to a page that has content. (In PHP you can't add a header after you start the page (meaning after you type the body tag). This whole script needs a serious overhaul, and I'd be happy to do so, but I need to know the structure of the database you are using, what you want to accomplish it, and other relevant details. If you could provide me with those I will be able to make the fixes to this script to make it better.
header("Location: admin.php");
I apoligize for suggesting this, but I noticed that you use 'echo "<meta http-equiv=\"refresh\" content=\"0;URL=change.php\">";' and 'echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">";'. There is a better method of redirecting users it also has less over head I think on both sides of processing the web page/there is less typing involved which makes development easier. I thought I could pass this on for future reference for you.
As example:
php Syntax (Toggle Plain Text)
header("Location: admin.php");
By the way have a great day.![]()
header() function (which only works before there is any text outputted) will work. This also made me realize that not only is this script incorrect in PHP, but also is not a valid HTML document (There is no HTML, HEAD, or BODY tags!).
<!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <table border="1"> <thead> <tr> <th>blah</th> <th>blah</th> </tr> </thead> <tbody> <tr> <td>blah</td> <td>blah</td> </tr> <tr> <td>blah</td> <td>blah</td> </tr> </tbody> </table> <?php header("Location: index.php"); ?> </body> </html>
The header() function sends a raw HTTP header to a client.
It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):
PHP Syntax (Toggle Plain Text)
<html> <?php // This results in an error. // The output above is before the header() call header('Location: http://www.example.com/'); ?>
<!-- verbinding.php --> <?php $dbhost = "***"; $db = "***"; $dbname = "***"; $username = "***"; $dbww = "***"; ?>
<!-- admin.html --> <HTML> <HEAD> <TITLE>admin.html</TITLE> </HEAD> <BODY> Welkom op de admin. <br><br> A.u.b. inloggen: <form method=post action=admin.php> Username: <input type="text" name="name"> Wachtwoord: <input type"password" name="password"> <input type="submit" name="submit"> </form> </BODY> </HTML>
<!-- admin.php --> <HTML> <HEAD> <TITLE>Admin.php</TITLE> </HEAD> <?php include("verbinding.php"); if ($_POST['submit']) { $name = $_POST['name']; $password = $_POST['password']; if (($name == $username) && ($password == $dbww)) { mysql_connect("$dbhost","$username","$dbww"); echo "kies een tabel.<br>"; show(); ?> <BODY> <form action=change.php method=post> <input type="text" name="id"> <input type="submit" name="submit"> </form> <?php } else { ?> Foute username en wachtwoord combinatie ingevuld, <a href="admin.html">probeer opnieuw</a> // wrong password/user combination <?php } } function show() { $tabellen = mysql_list_tables("$dbname"); $i=0; while ($i < mysql_num_rows($tabellen)){ $t_name[$i] = mysql_tablename ($tabellen, $i); echo $i, $t_name[$i]. "<br>"; $i++; } }?> </BODY> </HTML>
<!-- change.php --> <?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("cannot list tables"); 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); ?> <HTML> <HEAD> <TITLE>Change.php</TITLE> </HEAD> <BODY> <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> <?php echo "<td bgcolor='#FFFFFF'>#</td>"; 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 if($_POST['delete']=="Delete") { $checkbox = $_POST['checkbox[]']; for($i=0;$i<$count;$i++) { $del_id = $checkbox[$i]; $result = mysql_query("DELETE FROM $tbl_name WHERE id='".mysql_real_escape_string($del_id)."'") or die("Query Error!"); } if($result){ //Here a code to reload change.php } } if($_POST['back']=="Back"){ //Here a code to reload admin.php } } $i++; } mysql_close(); } ?> </table> </td> </tr> </table> </BODY> </HTML>

if($_POST['delete']=="Delete") { $checkbox = $_POST['checkbox']; if($checkbox) { foreach($checkbox as $box) { $result = mysql_query("DELETE FROM $tbl_name WHERE id='".mysql_real_escape_string($box)."'") or die("Query Error!"); } } if($result){ //Here a code to reload change.php } }
| DaniWeb Message | |
| Cancel Changes | |