【问题标题】:how to update database using php,mysql如何使用 php、mysql 更新数据库
【发布时间】:2018-02-25 07:15:36
【问题描述】:

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

这是我的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();
?>

当我运行浏览器时,它显示“记录已成功插入数据库”但数据库尚未更改。 请帮我解决我的问题。

enter image description here

【问题讨论】:

  • 您没有发布roomno 值。您永远不会在更新代码中设置 $row[roomno] 的值。
  • 好的先生那我该怎么办先生。我很困惑请指导我
  • 如何提交表单值?
  • 通过点击按钮先生

标签: php html mysql sql-update imagebutton


【解决方案1】:

我终于得到了答案。我编辑我的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();
?>

现在它工作正常:-)

【讨论】: