在此代码中,我没有任何错误消息,但它不会删除数据。顺便说一下,数据库有很多记录,所有字段的名称都是正确的。
<?php
if ($connect = mysqli_connect('localhost', 'root', 'adminpass', 'flip'))
{
$id = $_GET['id'];
$sql = "SELECT * FROM threads ORDER BY id DESC";
$query = mysqli_query($connect, $sql);
$num = mysqli_num_rows($query);
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" >
<table border="1" width="400" cellpadding="0" cellspacing="0">
<tr>
<td>#</td>
<td>id</td>
<td>subject</td>
</tr>
<?php
while ($row = mysqli_fetch_array($query))
{
?>
<tr>
<td> <input type="checkbox" name="checkbox[]" value="<?php echo $row['id'] ?>"></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['topic'] ?></td>
</tr>
<?php
}
?>
<input type="submit" name="delete" value="delete" >
<?php
if (isset($delete))
{
for ($i = 0; $i < $num; $i++)
{
$del_id = $checkbox[$i];
$sql2 = "DELETE FROM threads WHERE id='$del_id'";
$query2 = mysqli_query($connect, $sql2);
}
if ($query2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysqli_close($connect);
?>
</table>
</form>
它有什么问题?
答案 0 :(得分:0)
你可以试试这个:
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("localhost","root","adminpass","flip");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if(isset($_POST['delete'])){ /* IF DELETE IS CLICKED */
$checkbox=$_POST['checkbox'];
$hiddencounter=mysqli_real_escape_string($connect,$_POST['hiddencounter']); /* PREVENT A BIT OF SQL INJECTION */
for($i=0;$i<$hiddencounter;$i++){ /* FOR LOOP BASED ON THE NUMROWS BELOW */
if(!empty($checkbox[$i])){ /* IF THE CHECKBOX IS TICKED */
$del_id=mysqli_real_escape_string($connect,$checkbox[$i]); /* PREVENT A BIT OF SQL INJECTION */
$sql2="DELETE FROM threads WHERE id='$del_id'";
$query2=mysqli_query($connect,$sql2); /* IMPLEMENT THE DELETE QUERY */
}
} /* END OF FOR LOOP */
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
} /* END OF ISSET DELETE */
?>
<form action="" method="POST" > <?php /* YOU CAN LEAVE THE ACTION BLANK, TO SUBMIT THE FORM ON THE PAGE ITSELF */ ?>
<table border="1" width="400" cellpadding="0" cellspacing="0">
<tr>
<td>#</td>
<td>id</td>
<td>subject</td>
</tr>
<?
$sql="SELECT * FROM threads ORDER BY id DESC";
$query=mysqli_query($connect,$sql);
$num=mysqli_num_rows($query);
/* SUBMIT THE NUMBER OF ROWS QUERIED THROUGH HIDDEN INPUT */
echo "<input type='hidden' name='hiddencounter' value='$num'>";
while($row=mysqli_fetch_array($query)){ /* FETCH DATA */
?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $row['id'] ?>"></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['topic'] ?></td>
</tr>
<?
} /* END OF WHILE LOOP $QUERY */
?>
<input type="submit" name="delete" value="delete" >
<?php
mysqli_close($connect);
?>
</table>
</form>
解释您的代码无效的原因:
isset($delete)
:$delete
变量来自何处?<meta>
重定向。它会在 for循环后重定向到 delete.php ,这就是您没有看到任何错误的原因。但是,如果您删除<meta>
重定向,我很确定您会看到一些错误。答案 1 :(得分:0)
您的变量中有错误:
if (isset($delete))
应该是:
if (isset($_POST['$delete']))
答案 2 :(得分:0)
我没有看到代码中设置$delete
变量的位置。目前$delete
未设置,因此if(isset($delete))
为假。
对我而言,您似乎正在尝试使用Registered Globals。或者您忘记将$delete
设置为$_GET[]
或$_POST[]
个变量之一。
&#34;警告 自PHP 5.3.0开始,此功能已被弃用,自PHP 5.4.0起已被删除。&#34;