[固定] 我解决了我的问题。根据{{3}}的建议,我访问了Fred -ii-。我看到他使用的复选框似乎比按钮更有用,因为你可以一次取出多个物体。他还将对象的id附加到按钮上,就像this Q&A on SO建议的那样。
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?php echo row['id']; ?>'/>
</form>
这是固定代码。我现在可以单独删除这些框。谢谢你提出的所有建议。我现在也在使用mysqli。
$query = mysqli_query($connect, "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysqli_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b><a href="">'.$date2.'</a></div>';
echo '<div class="news-body">'.$post2.'</div>';
if (isset($_SESSION['username']) && ($_SESSION['level'] >= 3 || $_SESSION['group'] == 'Admin')) {
?>
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button[]' value="<?php echo $row['id']; ?>"/>
</form>
<?php
}
echo '<br>';
}
if(isset($_POST['delete_button'])) {
$boxid = $_POST['delete_button'];
for($i=0;$i<count($boxid);$i++){
$del_id = $boxid[$i];
mysqli_query($connect, "DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$del_id'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
}
}
[问题] 所以目前我一直在为一个网站工作,仅仅是为了我的学习目的而谷歌到目前为止一直都是很好的帮助。虽然,我似乎无法解决这个问题。我有一个“新闻提要”
它不允许我发布图像,但它是我能给出的唯一视觉想法。除非您想访问我的主页http://i.stack.imgur.com/oieXJ.png,否则新闻Feed就在那里,当然没有删除按钮。
我想为每个帖子添加一个“删除”按钮,以便我可以在需要时单独删除它们。我正在使用当前代码的问题。它从数据库中删除所有这些,但我只想删除我点击删除的“帖子”。这是我的来源:
$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysql_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
$boxid = $row['id'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b><a href="">'.$date2.'</a></div>';
echo '<div class="news-body">'.$post2.'</div>';
if ($_SESSION['level'] > 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') {
?>
<form action="" method='post'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='Delete' />
</form>
<?php
if(isset($_POST['delete_button'])) {
$con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
header('refresh:1; url=/');
mysql_close($con);
}
}
echo '<br>';
}
我是PHP HTML CSS MYSQL的新手。我对所有建议和评论持开放态度。任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
首先,您要选择多行:
mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5")
然后你要为每一行抓取一个数组
while ($row = mysql_fetch_array($query)) {
然后为该行设置boxid,然后在该行的循环内运行删除查询,其中WHERE子句查找该boxid。
因此,给定此代码流程,您将始终删除表中的所有行。
mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'")
现在,真正的问题是,如何解决这个问题?
在你的select语句中,只需添加一个where子句,它只返回带有boxid的行或你实际需要的任何id,这样就只返回一行(或者确切地说是要删除的行)
"SELECT FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$someSortOfID'"
此外,不推荐使用mysql_ *,因此please see the following documentation关于如何“升级”新标准mysqli。
答案 1 :(得分:0)
我建议你不要使用折旧的 mysql _ * 功能!!!!!
您需要做的是将输入值更改为行ID :
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?echo$boxid;?>' />
</form>
然后在while循环之外移动 if(isset($ _ POST ['delete_button']))中的整个代码,整个代码将是:
<?php
$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysql_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
$boxid = $row['id'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b><a href="">'.$date2.'</a></div>';
echo '<div class="news-body">'.$post2.'</div>';
if ($_SESSION['level'] > 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') {
?>
<form action="" method='post'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?echo$boxid;?>' />
</form>
<?php
}
echo '<br>';
}
if(isset($_POST['delete_button'])) {
$boxid = $_POST['delete_button'];
$con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
header('Location: http://'.$_SERVER['HTTP_HOST']);
mysql_close($con);
}
?>