根据用户通过下拉列表选择的内容更新数据库字段

时间:2018-01-28 14:29:16

标签: php html sql

因此,在我的网站上,管理员可以将作业分配给用户,该作业将自动具有以下状态:等待被接受。我现在在作业页面上创建了一个下拉框,以便用户可以使用该下拉框拒绝或接受作业。但是我不确定根据用户选择的内容更新数据库字段所需的编码。

我的目标是:用户选择状态,然后单击更改按钮以更新作业的状态。

当前代码:

   <?php
        if(!isset($_SESSION)) 
        { 
            session_start(); 
        } 
    ?>



    <!DOCTYPE html>
    <html>
        <head>
           <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Latest compiled and minified CSS -->

            <title>Tyre Hire</title>
            <link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
        <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        </head>
        <body>
    <div class="container">
        <div class="row">
        <div class="col-xs-12">

            <header> 
                <img class="img-responsive" src ="images/Logo.png" alt ="logo"/>
            </header>

        </div>
        </div>

                <?php

                require_once ("config.inc.php");

    try
        {
        $conn = new PDO(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD);
        }

    catch(PDOException $exception)
        {
        echo "Oh no, there was a problem" . $exception->getMessage();
        }

                if(!isset($_SESSION["username"]))
    {
        //user tried to access the page without logging in
        header( "Location: add.php" );

    }

        $login = $_SESSION['user_ID'];

        $query = "SELECT * FROM login WHERE user_ID = :user_ID"; 
    $term = $conn->prepare($query);
    $term->bindValue(':user_ID', $login);
    $term->execute();

    $login = $term->fetch(PDO::FETCH_OBJ);      


    $status = "Waiting to be accepted";
    $status2 = "Accept Job";
    $status3 = "Decline Job";

    if(isset($_SESSION["username"]))
    {
        echo "Welcome, you are now logged in as <b>".$_SESSION['username']."</b> <img class='clientView' src='images/loginIcon.png' alt='client'>"; }
        else { 
        echo "You are currently not logged in";
        };

        ?>
        <div class="row">
                <div class="col-xs-12">
        <br>
            <nav class="navbar navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>                        
          </button>
          <a class="navbar-brand" href="main.php">Tyre Hire</a>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav">

                    <li><a href="main.php"> Home <span class="glyphicon glyphicon-print"></span></a></li>
                   <li><a href="search.php"> Search <span class="glyphicon glyphicon-print"></span></a></li>
                    <li><?php echo "<a href='all-jobs.php?user_id=" . $login->user_ID . "&occupation=". $login->occupation ."''> Current Jobs <span class='glyphicon glyphicon-print'></span></a>";?> </li>
                     <li><a href="interest.php"> Register Interest <span class="glyphicon glyphicon-print"></span></a></li>
                   <li><a href="logout.php"> Logout <span class="glyphicon glyphicon-print"></span></a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="account.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
            <li><a href="add.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
          </ul>
        </div>
      </div>
    </nav>
        </div>
        </div>
            <div class="row">
        <div class="col-xs-12">
            <div class="right">
                <h2>Current Jobs</h2>
                <p>One of the best sites to find the best qualified and skilled drivers in the UK.</p>


                            <form class="form-horizontal formApply"  action="" method="POST">
            <h2><u>Job Details </u></h2>

        <div class="form-group">

    <?php

        echo "<h3>Job role :".$worker->jobTitle."</h3>";

    echo "<ul>";
    echo "<h3> About the role </h3>";
    echo "<li><b>Company Name:</b>".$worker->company."</li>";

    echo "<li><b>Job Description:</b>".$worker->jobDescription."</li>";
    echo "<h3>shift pattern</h3>";
    echo "<li><b>Start Time: </b>".$worker-> startTime."</li>";
    echo "<li><b>End Time: </b>".$worker-> endTime."</li>";
    echo "<br />";
    echo "<li><b>start Date:</b>".$worker->startDate."</li>";
    echo "<li><b>Job Expiry Date:</b>".$worker->expiry."</li>";
    echo "<b><h4>Current Status: </b>".$worker->status."</h4>";
    echo "</ul>";




    ?>
     <div class="form-group">
     <label class="control-label col-sm-2" for="search"> Status</label>
        <div class="col-sm-3">

            <select class="form-control" name="status" > 
            <option value="<? echo $status;?>"><?php echo $status;?></option>
            <option value="<? echo $status2;?>"><?php echo $status2;?></option>
            <option value="<? echo $status3;?>"><?php echo $status3;?></option>
            </select> 

            <input type="hidden" value="<?php echo $status; ?>" name="status">

    </div>

     <div class="form-group">
 <!--<label class="control-label col-sm-2" for="search"></label>      -->
    <div class="col-sm-3">

    <!--    <select class="form-control" name="status" > 
        <option value="<? echo $job;?>"><?php echo $job;?></option>
        </select>  -->

        <input type="hidden" value="<?php echo $job; ?>" name="job_id">

</div>
</div>

    </div>
    <input class = "buttonA" type="submit" name="Apply" value="Change status"/> 
            </div>          







    </form>

            </div>  


            </div>
            </div>




            <div class="row">
             <div class="col-xs-12">
            <div id="BottomArea">
            <h4> Recruitment Agencies </h4> 
            <div class="row">
             <div class="col-xs-3">
            <img class="FxPic img-responsive" src="images/fxpic.png" alt="fx-logo">
            </div>
             <div class="col-xs-3">
            <img class="driverhire img-responsive"  src="images/driverhire.jpg" alt="driverhire">
            </div>
             <div class="col-xs-3">
            <img class="logi img-responsive" src="images/logi.png" alt="logi-logo">
            </div>
             <div class="col-xs-3">
            <img class="pebble img-responsive" src="images/noel.png" alt="pebbles">
            </div>
            </div>

            </div>
            </div>

            </div>

            <div class="row">
             <div class="col-xs-12">
            <footer>
            <img id="logoFoot" src ="images/Logo.png" alt ="logo"/>
                <p><b>Address: 5A twickenham road, london SJ5 7AE</b></p>
            </footer>
            </div>
            </div>

            <script src=myjs.js></script>
        </div>
        </body>
    </html>

在阅读更新手册后尝试了它,没有错误,但点击按钮后它会给出一个空白页面。:

   <?php
include ("db_fncs.php");
try{
       $conn = new PDO(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD);

}
catch (PDOException $exception) 
{
echo "Oh no, there was a problem" . $exception->getMessage();
}


$status = $_POST["status"];
$job = $_POST['job_id'];
print_r($_POST);

$query = "UPDATE jobs SET status = :status WHERE job_id = :job_id AND status = :status";


    // Prepare statement
    $stmt = $conn->prepare($query);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':job_id', $job);
    // execute the query
    $stmt->execute();
    //$stmt->commit();
 echo $stmt->rowCount() . " records UPDATED successfully";
?>

1 个答案:

答案 0 :(得分:2)

UPDATE的基本语法是:

UPDATE <my_table>
SET <update_column_name> = <some_value>
WHERE <identifying_column_name> = <some_value>

在你的情况下,它可能看起来像:

UPDATE jobs
SET status = <new_job_status>
WHERE userid = <user_id>
AND jobid = <job_id>