使用下拉菜单中的值更新mysql表中的字段

时间:2015-02-27 23:51:44

标签: php

我目前在使用下拉列表中的值编辑mysql数据库表中的字段时遇到问题。我希望能够在mysql数据库上修改和更新的字段是从我的网页的下拉列表中选择的类型和时间。每当我改变'类型'从下方菜单中,它可以更新数据库,但会对“时间”产生影响。数据库中的字段将值更新为00:00:00。

这是我的代码的主要部分:

$id = $_GET['id'];

if(isset($_POST['update'])){

$type =  mysqli_real_escape_string($mysqli, $_POST['testtype']);


  $time = mysqli_real_escape_string($mysqli, $_POST['timeslot']);

     $query = "UPDATE bookings1 SET Type = '$type', Timeslot = '$time' WHERE booking_id = '$id'";

     $sqlquery = $mysqli->query($query);

}

$sqlquery = "SELECT * FROM bookings1 WHERE booking_id = '$id'";

$results = $mysqli->query($sqlquery);

if ($results->num_rows > 0) {

 while ($row = $results->fetch_assoc()){

       echo '<form method = "post"  action = "">';

          echo'Type:  <select id =  name ="testtype" > 

           <option value =""> '. $row["Type"].'  </option>

        <option value = "classIV"  > Class IV - Cars,light-vans,Motorcaravans (£35.00) </option>
        <option value = "classV" > Class V - Private Passenger vehicles & Minibuses (£44.99) </option>
        <option value = "classVI" > Class VI - Goods vehicles (£54.99) </option>
        <option value = "TaxiMOT" > Taxi MOT - Taxi vehicles (£35.00) </option>
        </select></br></br>';

      echo'Time:  <select id = "timeslot"  name ="timeslot" > 

        <option value =""> '. $row["Timeslot"].'  </option>

        <option value = "08:30" > 08:30 </option>

        <option value = "09:15" > 09:15 </option>

        <option value = "10:00" > 10:00 </option>

        <option value = "10:45" > 10:45 </option>

        <option value = "11:30" > 11:30 </option>

        <option value = "12:15" > 12:15 </option>

        <option value = "13:00" > 13:00 </option>

        <option value = "13:45" > 13:45 </option>

        <option value = "14:30" > 14:30 </option>

        <option value = "15:15" > 15:15 </option>

        <option value = "16:00" > 16:00 </option>

        <option value = "16:45" > 16:45 </option>

        <option value = "17:30" > 17:30 </option> 

        </select>

       echo '<input type = "submit" = name = "update" value = "update" />';

        echo '</form>';

1 个答案:

答案 0 :(得分:0)

尝试根据您输入的数据库列格式化日期/时间字段:

  1. 如果您的MySQL列是DATE类型:

    $ date = date(&#39; Ym-d&#39;,strtotime(str_replace(&#39; - &#39;,&#39; /&#39;,$ date))); < / p>

  2. 如果您的MySQL列是DATETIME类型:

    $ date = date(&#39; Ymd H:i:s&#39;,strtotime(str_replace(&#39; - &#39;,&#39; /&#39;,$ date)) );

  3. convert php date to mysql format