[英]php/mysql UPDATE where id = blah
i have a site where you can send greeting messages via php to a mysql server, and an admin login page. 我有一个网站,您可以通过PHP将问候消息发送到mysql服务器和管理员登录页面。 In the admin login page, it shows all of the messages with a status either pending, rejected or accepted with the buttons reject and accept next to each message.
在管理员登录页面中,它显示状态为待处理,拒绝或已接受状态的所有消息,其中按钮拒绝并接受每条消息旁边的消息。 Currently, whenever I hit "accept" or "reject" ALL of the messages become rejected or accepted in the database.
目前,每当我点击“接受”或“拒绝”时,所有消息都会被拒绝或在数据库中被接受。 I'd like to have the buttons call the script with a parameter which is the id of the message they're accepting/rejecting but I honestly don't know the proper syntax.
我想让按钮用一个参数调用脚本,该参数是他们接受/拒绝的消息的id,但老实说我不知道正确的语法。 Any help would be greatly appreciated.
任何帮助将不胜感激。
$query = "SELECT name, location, message, status FROM messages ORDER by status ";
if ($query_run = mysql_query($query))
{
while ($query_row = mysql_fetch_assoc($query_run))
{
$name = $query_row['name'];
$location = $query_row['location'];
$message = $query_row['message'];
$status = $query_row['status'];
echo '<form method="POST" action="login.php">';
echo 'From: '.$name.'<br>Location: '.$location.'<br>Status: '.$status.'<br>Message: '.$message.'<br><br>';
?>
<input type="submit" value="Approve" name="accept">
<input type="submit" value="Reject" name="reject"></form>
<?php
if (isset($_POST['accept']))
{
echo 'Accepted!';
$updateAccept = "UPDATE messages SET status = 'a'";
mysql_query($updateAccept);
};
if (isset($_POST['reject']))
{
echo 'Rejected!';
$updateAccept = "UPDATE messages SET status = 'r'";
mysql_query($updateAccept);
}
Bind a hidden field with each submit button like this 用这样的每个提交按钮绑定一个隐藏的字段
<form method="POST">
<input type="submit" value="Whatever" name="trigger_update" />
<input type="hidden" name="id_to_be_updated" value="<?php echo 'pass your id here'; ?>" />
</form>
<?php
if(isset($_POST['trigger_update'])) {
//Do sanitization according to your needs
mysqli_query($connection, "UPDATE tbl_name SET column_name = 'whatever' WHERE id = {$_POST['id_to_be_updated']}");
}
?>
您只需要在UPDATE语句中添加WHERE
子句:
$updateAccept = "UPDATE messages SET status = 'a' WHERE id = '$id'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.