如何使用 php、mysql 更新数据库

how to update database using php,mysql

您好,我正在开发酒店网站。我有一个页面,该页面使用 php 和 mysql.the 预订房间显示为红色背景颜色并带有感叹号按钮。如果我点击那个带有感叹号的按钮,房间状态应该从已预订变为可用。他的相同功能应该在可用房间部分完成。可用房间应显示为绿色背景颜色并带有勾号按钮。如果单击勾选按钮,可用状态应从可用更改为已预订。

这是我的 html 代码

<?php while($row = mysql_fetch_array($result)):;?>
                <div class="w3-small">
                  <?php if ($row['status'] == 'available'){ 
                        echo"<div class='w3-container w3-green w3-text-white w3-padding-small'>
                            <div class='w3-left'><i class='fa fa-users w3-tiny'></i></div>
                            <div class='w3-right'>".
                            "<h3 name='$row[0]'>".$row['roomno']."</h3>".
                            "</div>".
                          "<div class='w3-clear'></div>";
                        echo "<form action='editstatus.php' method='POST'><button class='buttonmark buttoncan' name='available'><i class='fa fa-exclamation-triangle'></i></button></form></div>";
                        }
                        elseif ($row['status']== 'booked') {
                          echo"<div class='w3-container w3-red w3-text-white w3-padding-small'>
                            <div class='w3-left'><i class='fa fa-users w3-tiny'></i></div>
                            <div class='w3-right'>".
                            "<h3 name='$row[0]'>".$row['roomno']."</h3>".
                            "</div>".
                          "<div class='w3-clear'></div>";
                        echo "<form action='editstatus.php' method='POST'><button class='buttonmark buttonok' name='booked'><i class='fa fa-check-square-o'></i></button></form></div>";
                        }
                      ?>
                        <br> <br>
                </div>
              <?php endwhile;?> 

我的php代码是

<?php
ob_start(); //After ob_start this output is saved in output buffer, so you can later decide what to do with it. 
include("../database/connection.php");

    $sql1="UPDATE acroom SET status='$_POST[booked]' WHERE roomno='$row[roomno]'";
    // If result matched $myusername and $mypassword, table row must be 1 row
        if(mysql_query($sql1)){
            header("Location:sample.php");
            echo "Records inserted successfully into database.";

        } else{

            echo "ERROR: Could not able to execute $sql. " . mysql_error($cn);

        }

ob_end_flush();
?>

当我 运行 我的浏览器显示 "Records inserted successfully into database" 但数据库尚未更改。 请帮助我解决我的问题。

enter image description here

终于找到答案了。我编辑 html 页面如下

<?php while($row = mysql_fetch_array($result)):;?>
                <div class="w3-small">
                  <?php if ($row['status'] == 'available'){ 
                        echo"<div class='w3-container w3-green w3-text-white w3-padding-small'>
                            <div class='w3-left'><i class='fa fa-users w3-tiny'></i></div>
                            <div class='w3-right'>".
                            "<h3>".$row['roomno']."</h3>".
                            "</div>"."<div class='w3-clear'></div>
                            <a href='editstatus.php?d=$row[roomno]' class='buttonmark buttoncan'><i class='fa fa-exclamation-triangle'></i></a>

                          </div>";
                        }
                        elseif ($row['status']== 'booked') {
                          echo"<div class='w3-container w3-red w3-text-white w3-padding-small'>
                            <div class='w3-left'><i class='fa fa-users w3-tiny'></i></div>
                            <div class='w3-right'>".
                            "<h3>".$row['roomno']."</h3>".
                            "</div>".
                          "<div class='w3-clear'></div>".
                          "<a class='buttonmark buttonok' href='editstatus.php?e=$row[roomno]'><i class='fa fa-check-square-o'></i></a></div>";
                        }
                      ?>
                        <br> <br>
                </div>
              <?php endwhile;?>

和我的 php 页面

<?php
ob_start(); //After ob_start this output is saved in output buffer, so you can later decide what to do with it. 
include("../database/connection.php");

if( isset($_GET['d']) )
{
    $roomno = $_GET['d'];
    $sql= "UPDATE acroom SET status='booked' WHERE roomno='$roomno'";
    $res= mysql_query($sql) or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=sample.php'>";
}

if( isset($_GET['e']) )
{
    $roomno = $_GET['e'];
    $sql= "UPDATE acroom SET status='available' WHERE roomno='$roomno'";
    $res= mysql_query($sql) or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=sample.php'>";
}
ob_end_flush();
?>

现在工作正常:-)