如何在 PHP 中更新单行?

发布于 2024-12-08 17:58:05 字数 3015 浏览 0 评论 0原文

我有 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);

    }



?>

https://i.sstatic.net/0KGLB.jpg

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);

    }



?>

https://i.sstatic.net/0KGLB.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最初的梦 2024-12-15 17:58:05

您好,我在该表单中没有看到任何表单字段。
创建一个名为“booking”的隐藏字段

  <input type="text" name="booking" value="<?php echo $bookingID; ?>" >

Hi i don't see any form fields in that form.
create a hidden field with name of "booking"

  <input type="text" name="booking" value="<?php echo $bookingID; ?>" >
悲喜皆因你 2024-12-15 17:58:05

您是否将 bookingID 传递给 php?

<input type="hidden" name="booking" id="booking">

向两个按钮添加 onclick 函数,以将所选行的 bookingid 保存到隐藏输入。

onclick=$(\"#booking\").val('.$bookingID .');

Did you pass bookingID to php?

<input type="hidden" name="booking" id="booking">

add onclick function to both bottons to save selected row's bookingid to a hidden input.

onclick=$(\"#booking\").val('.$bookingID .');
吲‖鸣 2024-12-15 17:58:05

如果预订 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.

↘人皮目录ツ 2024-12-15 17:58:05

这可能是导致您出现问题的原因。

snip
        $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'";
/snip

将其更改为:

snip
        $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='{$_POST['booking']}'";
snip

请注意,将后变量直接回显到像这样的查询字符串中是非常不安全的。我建议先检查变量,然后再将其放入其中。

无论如何,您的问题是变量 $_POST['booking'] 的值可能没有传递到查询中。它传递了字符串$_POST['booking']

This is probably causing your problem.

snip
        $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'";
/snip

Change it to this:

snip
        $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='{$_POST['booking']}'";
snip

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'].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文