如何只删除分配给该值的行? (不知道标题怎么写)
How to delete only row that is assigned to that value? (Don't know how to write it in title)
我正在尝试制作 post 系统。
一切都运行良好,但有一件事我无法弄清楚......
我在数据库中有一些行是我用准备好的 mysqli 提取的。好吧,当我回显行的值时,它显示了正确的回显,并且显示了所有列(这显然应该这样做),这很好。
SQL :
SELECT id,postby,downvotes,upvotes FROM posts
绑定结果如下:
bind_result($postid,$postby,$downvotes,$upvotes);
所以它正在工作。他们都被回声拉了出来。不错。
但是!当我试图从准备好的 mysqli 中删除 table 中的任何行(只有一行)时,它会删除所有行...
我的删除行代码:
DELETE FROM posts WHERE id = ?
和绑定参数如
bind_param('i',$postid);
但这会删除数据库中的所有行。我希望它只删除分配给 id 的那个。因此,如果它从数据库中拉出 20 行,它将删除数据库中的所有行...如何只删除分配给它的 id 的一行?
谢谢!
我不得不提一下,我已经尝试过 <input name='deletesomething' type='hidden' value='<php? echo $postid ?>'>
之类的东西,然后又尝试了
之类的东西
bind_param('s',$_POST['deletesomething']);
但它仍然删除了所有行。如何解决?
编辑
对不起!实际上隐藏输入的方法是可行的,但是它非常不安全,因为用户可以更改值并且它会删除不同的行...
编辑 2
我的完整代码可以理解我真正想做什么。
<?php
$selectposts = "SELECT postby,posttxt,time,upvotes,downvotes,id FROM posts ORDER BY id DESC";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime,$upvotesdb,$downvotesdb,$postid);
$stmt->store_result();
while($stmt->fetch()){
$picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$picturestmt->bind_param('s', $postby);
$picturestmt->execute();
$picturestmt->bind_result($picture);
while($picturestmt->fetch()){
if(empty($picture)){
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
}
}
echo
"<p><center><div class='postdv'>
<b>".$profpicturefromdb."
<a href='http://www.fregbind.16mb.com/".$postby.".php'><h3>".$postby."</h3></a></b><font color='#000'>
[ ".$posttime." ] </font>";
if($_SESSION['username'] == $postby){
echo"<label for='pedform'><img src='pictures/ped.png' rel='icon' width='15' height='15' class='icnimg'></label>
<label for='xdelform'><img src='pictures/xdel.png' rel='icon' width='15' height='15' class='icnimg'></label>
<div class='hidden'><form method='POST'><input id='pedform' type='submit'></form>
<form method='POST'><input type='submit' id='xdelform'></form></div>";
}
echo "<hr class='hrgray'><br>
<font color='#000'>".$posttxt."</font><p>";
$votestmt = $mysqli->prepare("SELECT upvotes, downvotes FROM posts WHERE id = ?");
$votestmt->bind_param('i',$postid);
$votestmt->execute();
$votestmt->bind_result($upvotes,$downvotes);
$votestmt->store_result();
while($votestmt->fetch()){
if(isset($_POST['voteup'])){
echo $postid;
$onevalue = "1";
$makeitplusone = $upvotes + $onevalue;
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = ? WHERE id = ?");
$addvote->bind_param('si',$makeitplusone,$postid);
$addvote->execute();
}
echo"<form method='POST'><input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
</form>
</center>
</div>";
}
}
?>
这里的代码略有不同。没什么不同,但这只需要在单击时将 upvote 的值更改为 +1,但是它将所有行更新为 +1...
编辑 3
我们正在谈论这部分:
$votestmt = $mysqli->prepare("SELECT upvotes, downvotes FROM posts WHERE id = ?");
$votestmt->bind_param('i',$postid);
$votestmt->execute();
$votestmt->bind_result($upvotes,$downvotes);
$votestmt->store_result();
while($votestmt->fetch()){
if(isset($_POST['voteup'])){
echo $postid;
$onevalue = "1";
$makeitplusone = $upvotes + $onevalue;
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = ? WHERE id = ?");
$addvote->bind_param('si',$makeitplusone,$postid);
$addvote->execute();
}
echo"<form method='POST'><input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
</form>
我不确定这段代码到底在做什么,但试试这个(第 38 和 49 行是修改)...
<?php
$selectposts = "SELECT postby,posttxt,time,upvotes,downvotes,id FROM posts ORDER BY id DESC";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime,$upvotesdb,$downvotesdb,$postid);
$stmt->store_result();
while($stmt->fetch()){
$picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$picturestmt->bind_param('s', $postby);
$picturestmt->execute();
$picturestmt->bind_result($picture);
while($picturestmt->fetch()){
if(empty($picture)){
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
}
}
echo
"<p><center><div class='postdv'>
<b>".$profpicturefromdb."
<a href='http://www.fregbind.16mb.com/".$postby.".php'><h3>".$postby."</h3></a></b><font color='#000'>
[ ".$posttime." ] </font>";
if($_SESSION['username'] == $postby){
echo"<label for='pedform'><img src='pictures/ped.png' rel='icon' width='15' height='15' class='icnimg'></label>
<label for='xdelform'><img src='pictures/xdel.png' rel='icon' width='15' height='15' class='icnimg'></label>
<div class='hidden'><form method='POST'><input id='pedform' type='submit'></form>
<form method='POST'><input type='submit' id='xdelform'></form></div>";
}
echo "<hr class='hrgray'><br>
<font color='#000'>".$posttxt."</font><p>";
if($_POST['upvoteid'] == $postid && isset($_POST['voteup'])){
echo 'This:' . $_POST['upvoteid'] . ':equals:' . $postid . '.';
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = upvotes + 1 WHERE id = ?");
$addvote->bind_param('i',$postid);
$addvote->execute();
}
echo"<form method='POST'>
<input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
<input name='upvoteid' type='hidden' value='$postid'>
</form>
</center>
</div>";
}
}
?>
这样您就可以只 运行 更新已提交的更新。之前您正在检查是否设置了一个字段,该字段将为这些记录中的每一个设置。
我正在尝试制作 post 系统。 一切都运行良好,但有一件事我无法弄清楚...... 我在数据库中有一些行是我用准备好的 mysqli 提取的。好吧,当我回显行的值时,它显示了正确的回显,并且显示了所有列(这显然应该这样做),这很好。 SQL :
SELECT id,postby,downvotes,upvotes FROM posts
绑定结果如下:
bind_result($postid,$postby,$downvotes,$upvotes);
所以它正在工作。他们都被回声拉了出来。不错。
但是!当我试图从准备好的 mysqli 中删除 table 中的任何行(只有一行)时,它会删除所有行... 我的删除行代码:
DELETE FROM posts WHERE id = ?
和绑定参数如
bind_param('i',$postid);
但这会删除数据库中的所有行。我希望它只删除分配给 id 的那个。因此,如果它从数据库中拉出 20 行,它将删除数据库中的所有行...如何只删除分配给它的 id 的一行? 谢谢!
我不得不提一下,我已经尝试过 <input name='deletesomething' type='hidden' value='<php? echo $postid ?>'>
之类的东西,然后又尝试了
bind_param('s',$_POST['deletesomething']);
但它仍然删除了所有行。如何解决?
编辑
对不起!实际上隐藏输入的方法是可行的,但是它非常不安全,因为用户可以更改值并且它会删除不同的行...
编辑 2
我的完整代码可以理解我真正想做什么。
<?php
$selectposts = "SELECT postby,posttxt,time,upvotes,downvotes,id FROM posts ORDER BY id DESC";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime,$upvotesdb,$downvotesdb,$postid);
$stmt->store_result();
while($stmt->fetch()){
$picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$picturestmt->bind_param('s', $postby);
$picturestmt->execute();
$picturestmt->bind_result($picture);
while($picturestmt->fetch()){
if(empty($picture)){
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
}
}
echo
"<p><center><div class='postdv'>
<b>".$profpicturefromdb."
<a href='http://www.fregbind.16mb.com/".$postby.".php'><h3>".$postby."</h3></a></b><font color='#000'>
[ ".$posttime." ] </font>";
if($_SESSION['username'] == $postby){
echo"<label for='pedform'><img src='pictures/ped.png' rel='icon' width='15' height='15' class='icnimg'></label>
<label for='xdelform'><img src='pictures/xdel.png' rel='icon' width='15' height='15' class='icnimg'></label>
<div class='hidden'><form method='POST'><input id='pedform' type='submit'></form>
<form method='POST'><input type='submit' id='xdelform'></form></div>";
}
echo "<hr class='hrgray'><br>
<font color='#000'>".$posttxt."</font><p>";
$votestmt = $mysqli->prepare("SELECT upvotes, downvotes FROM posts WHERE id = ?");
$votestmt->bind_param('i',$postid);
$votestmt->execute();
$votestmt->bind_result($upvotes,$downvotes);
$votestmt->store_result();
while($votestmt->fetch()){
if(isset($_POST['voteup'])){
echo $postid;
$onevalue = "1";
$makeitplusone = $upvotes + $onevalue;
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = ? WHERE id = ?");
$addvote->bind_param('si',$makeitplusone,$postid);
$addvote->execute();
}
echo"<form method='POST'><input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
</form>
</center>
</div>";
}
}
?>
这里的代码略有不同。没什么不同,但这只需要在单击时将 upvote 的值更改为 +1,但是它将所有行更新为 +1...
编辑 3
我们正在谈论这部分:
$votestmt = $mysqli->prepare("SELECT upvotes, downvotes FROM posts WHERE id = ?");
$votestmt->bind_param('i',$postid);
$votestmt->execute();
$votestmt->bind_result($upvotes,$downvotes);
$votestmt->store_result();
while($votestmt->fetch()){
if(isset($_POST['voteup'])){
echo $postid;
$onevalue = "1";
$makeitplusone = $upvotes + $onevalue;
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = ? WHERE id = ?");
$addvote->bind_param('si',$makeitplusone,$postid);
$addvote->execute();
}
echo"<form method='POST'><input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
</form>
我不确定这段代码到底在做什么,但试试这个(第 38 和 49 行是修改)...
<?php
$selectposts = "SELECT postby,posttxt,time,upvotes,downvotes,id FROM posts ORDER BY id DESC";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime,$upvotesdb,$downvotesdb,$postid);
$stmt->store_result();
while($stmt->fetch()){
$picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$picturestmt->bind_param('s', $postby);
$picturestmt->execute();
$picturestmt->bind_result($picture);
while($picturestmt->fetch()){
if(empty($picture)){
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
}
}
echo
"<p><center><div class='postdv'>
<b>".$profpicturefromdb."
<a href='http://www.fregbind.16mb.com/".$postby.".php'><h3>".$postby."</h3></a></b><font color='#000'>
[ ".$posttime." ] </font>";
if($_SESSION['username'] == $postby){
echo"<label for='pedform'><img src='pictures/ped.png' rel='icon' width='15' height='15' class='icnimg'></label>
<label for='xdelform'><img src='pictures/xdel.png' rel='icon' width='15' height='15' class='icnimg'></label>
<div class='hidden'><form method='POST'><input id='pedform' type='submit'></form>
<form method='POST'><input type='submit' id='xdelform'></form></div>";
}
echo "<hr class='hrgray'><br>
<font color='#000'>".$posttxt."</font><p>";
if($_POST['upvoteid'] == $postid && isset($_POST['voteup'])){
echo 'This:' . $_POST['upvoteid'] . ':equals:' . $postid . '.';
$addvote = $mysqli->prepare("UPDATE posts SET upvotes = upvotes + 1 WHERE id = ?");
$addvote->bind_param('i',$postid);
$addvote->execute();
}
echo"<form method='POST'>
<input type='submit' name='voteup' class='voteup' value='Up'><font color='#00CC00'> ".$upvotes." </font>
<input type='submit' name='votedown' class='votedown' value='Down'> <font color='#FF4747'>".$downvotes." </font>
<input name='upvoteid' type='hidden' value='$postid'>
</form>
</center>
</div>";
}
}
?>
这样您就可以只 运行 更新已提交的更新。之前您正在检查是否设置了一个字段,该字段将为这些记录中的每一个设置。