如何在 PHP 中更新单行?
我有 2 个按钮,批准和拒绝更新预订状态。 当我试图将值传递给approve_booking.php时我迷失了。 不知道如何根据 bookingID 更新单行。需要编码方面的帮助!
index.phpapprove_booking.php
<head>
<script src="jquery-latest.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#myTable").tablesorter({widgets: ['zebra']});
});
$(document).ready(function()
{
$("#myTable").tablesorter();
}
);
$(document).ready(function()
{
$("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} );
}
);
</script>
<link href="style.css" rel="stylesheet" type="text/css">
<link href="stylelogin.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="post" action="approve_booking.php" >
<?php
$db = new mysqli("localhost", "root", "", "eventdb");
$query = "SELECT customer.companyName, customer.contactName,eventinfo.eventTitle,boothAlias,date, testbook.bstatus, testbook.username, bookingID from eventinfo, testbook, customer where testbook.username=customer.username AND testbook.eventID=eventinfo.eventID";
$result = $db->query($query, MYSQLI_STORE_RESULT);
$o = '<table id="myTable" class="tablesorter" width="660px"><thead><tr><th>Company Name</th><th>Contact Name</th><th>Event</th><th>Booth</th><th>Date</th><th>Status</th></tr></thead><tbody>';
while(list($companyName, $contactName, $eventTitle, $boothAlias, $date, $bstatus,$bookingID) = $result->fetch_row()) {
if($bstatus==0){
$status="Pending";
}else if($bstatus==1){
$status="Successful";
}else{
$status="Reject";
}
$o .= '<tr><td width="120px">'.$companyName.'</td><td width="120px">'.$contactName.'</td><td width="180px">'.$eventTitle.'</td><td width="70px">'.$boothAlias.'</td><td width="170px">'.$date.'</td><td width="70">'.$status.'</td><td width="100"><input width="100px" name="APPROVED" type="submit" id="APPROVED" value="Approve"> <input width="100px" name="REJECT" type="submit" id="REJECT" value="Reject"></td></tr>';
}
$o .= '</tbody></table>';
echo $o;
?>
</form>
</body>
<?php
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("eventdb") or die (mysql_error());
if(isset($_POST['APPROVED']))
{
$query2 = "UPDATE testbook SET bstatus ='0' WHERE bookingID='$_POST[booking]'";
$result2 = @mysql_query($query2);
}
if (isset($_POST['REJECT']))
{
$query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'";
$result3 = @mysql_query($query3);
}
?>
I have 2 buttons, approve and reject to update booking status.
I am lost when trying to pass value to approve_booking.php.
Have no idea how to update single row according to bookingID. Need help on coding!!!
index.php
<head>
<script src="jquery-latest.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#myTable").tablesorter({widgets: ['zebra']});
});
$(document).ready(function()
{
$("#myTable").tablesorter();
}
);
$(document).ready(function()
{
$("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} );
}
);
</script>
<link href="style.css" rel="stylesheet" type="text/css">
<link href="stylelogin.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="post" action="approve_booking.php" >
<?php
$db = new mysqli("localhost", "root", "", "eventdb");
$query = "SELECT customer.companyName, customer.contactName,eventinfo.eventTitle,boothAlias,date, testbook.bstatus, testbook.username, bookingID from eventinfo, testbook, customer where testbook.username=customer.username AND testbook.eventID=eventinfo.eventID";
$result = $db->query($query, MYSQLI_STORE_RESULT);
$o = '<table id="myTable" class="tablesorter" width="660px"><thead><tr><th>Company Name</th><th>Contact Name</th><th>Event</th><th>Booth</th><th>Date</th><th>Status</th></tr></thead><tbody>';
while(list($companyName, $contactName, $eventTitle, $boothAlias, $date, $bstatus,$bookingID) = $result->fetch_row()) {
if($bstatus==0){
$status="Pending";
}else if($bstatus==1){
$status="Successful";
}else{
$status="Reject";
}
$o .= '<tr><td width="120px">'.$companyName.'</td><td width="120px">'.$contactName.'</td><td width="180px">'.$eventTitle.'</td><td width="70px">'.$boothAlias.'</td><td width="170px">'.$date.'</td><td width="70">'.$status.'</td><td width="100"><input width="100px" name="APPROVED" type="submit" id="APPROVED" value="Approve"> <input width="100px" name="REJECT" type="submit" id="REJECT" value="Reject"></td></tr>';
}
$o .= '</tbody></table>';
echo $o;
?>
</form>
</body>
approve_booking.php
<?php
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("eventdb") or die (mysql_error());
if(isset($_POST['APPROVED']))
{
$query2 = "UPDATE testbook SET bstatus ='0' WHERE bookingID='$_POST[booking]'";
$result2 = @mysql_query($query2);
}
if (isset($_POST['REJECT']))
{
$query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'";
$result3 = @mysql_query($query3);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,我在该表单中没有看到任何表单字段。
创建一个名为“booking”的隐藏字段
Hi i don't see any form fields in that form.
create a hidden field with name of "booking"
您是否将 bookingID 传递给 php?
向两个按钮添加
onclick
函数,以将所选行的 bookingid 保存到隐藏输入。Did you pass bookingID to php?
add
onclick
function to both bottons to save selected row's bookingid to a hidden input.如果预订 ID 是您的自动增量/主键,则它仅更新单行,但如果不是,则在 WHERE 条件下使用您的主键,以便它只能查找并更新 1 行。
If booking id is your auto increament / primary key so it update only single row but if its not then use your primary key in WHERE condition so it can find and update only 1 row.
这可能是导致您出现问题的原因。
将其更改为:
请注意,将后变量直接回显到像这样的查询字符串中是非常不安全的。我建议先检查变量,然后再将其放入其中。
无论如何,您的问题是变量
$_POST['booking']
的值可能没有传递到查询中。它传递了字符串$_POST['booking']
。This is probably causing your problem.
Change it to this:
Note that echoing post variables directly into query strings like these is highly unsecure. I suggest checking the variable first before putting it in there.
Anyway, your problem is the value of the variable
$_POST['booking']
probably wasn't passed into the query. It passed the string$_POST['booking']
.