虽然我是php的新手,但我想弄清楚使用删除操作时的错误是什么我有一个数据库表“local_admin”,元组是选区,用户名和密码,选区设置为主键。下面给出了代码,并且页面中没有填充数据表。非常感谢任何帮助。感谢名单。
<?php
session_start();
include("header.php");
$logout="You have successfully Loged out! Please log in to continue";
if(!isset($_SESSION['username']))
header("location:masteradmin.php");
else
$id=$_SESSION['username'];
if(isset($_POST['logout']))
{
session_destroy();
header("location:masteradmin.php");
}
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="voting"; // Database name
$tbl_name="local_admin"; // 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);
?>
<!--Content starts here-->
<div class="content">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete Local Admin</strong>
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Constituency</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</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['constituency']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['constituency']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></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(isset($_POST['delete']))
{
$checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE name='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletelocaladmin.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</div>
<?php include("footer.php"); ?>
答案 0 :(得分:0)
除了上面的mysql扩展建议,您还应验证输入。 偏移错误是指索引不属于数组
// load selected checkboxes or false
$checkbox = isset($_POST['checkbox'])?$_POST['checkbox']:false;
$result = false;
if(is_array($checkbox)) {
for($i=0;$i<$count;$i++){
if(isset($checkbox[$i])) { // the index may not exist
// should be using PDO
$del_id = mysql_real_escape_string($checkbox[$i]);
// you are using constituency on the html checkbox value
// but here you are using the name field?
$sql = "DELETE FROM $tbl_name WHERE name='$del_id'";
$result = mysql_query($sql);
}
}
}