简体   繁体   中英

how to update database using php,mysql

hi i am developing hotel website. I have a page which is show all available and booked room in a page using php and mysql.the booked room display as red background color with Exclamation button. if i click that exclamation marked button room status should be change from booked to available. his same function should be done in available room portion. available room should be display with green background color with tick button. if click tick button available status should be change from available to booked.

here is my html code

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

my php code is

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

when i run my browser it show "Records inserted successfully into database" but database not yet to change. please help me o resolve my problem.

enter image description here

finally i got the answer. I edit my html page as below

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

and my php page

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

now its working fine :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM