簡體   English   中英

使用下拉菜單編輯mysql數據

[英]Editing mysql data with drop downs

我有一個數據輸入頁面,其中存在一些下拉列表。 下拉列表中的選定項目存儲在我的mysql數據庫中,沒有問題。

我已經做了第二頁來編輯個人記錄。 我可以毫無問題地從下拉列表中顯示數據。 但是,我希望能夠使用相同的下拉選項來編輯結果。

我可以在edit.php頁面上添加帶有所有正確選項的下拉菜單,但是不會顯示數據庫中存儲的值。 取而代之的是,默認情況下我會得到第一選擇,而不是存儲值。

<?php
    $position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
    $position_result = mysql_query($position_sql);
        echo "<select name='position'>";
            while ($row = mysql_fetch_array($position_result)) {
        echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
        }
        echo "</select>";
?>  

我正在使用POST和GET獲取正確的記錄ID。

我的文本框如下所示:

Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">

我假設我必須構建某種if語句來顯示存儲的值?

不確定是否有幫助,但這是我如何獲取要編輯的記錄的ID:

        <?php
    $id= ($_GET["id"]);


      $sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
      $result = mysql_query($sql);
      $row = mysql_fetch_array($result);
     ?>

完整代碼:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Form Edit Data</title>
    </head>

    <body>

    <?php
$BASE_PATH = 'C:\xampp\htdocs\OGS';
include_once($BASE_PATH . "\includes\layouts\header.php");
?>

<div id="main">
    <div id="subnavigation">
        <?php include_once($BASE_PATH . "\mods\main_menu\index.html");?>
    </div>

  <div id="page"
  <br><br>
    <table border=1>
      <tr>
        <td align=center>Update Employee Information</td>
      </tr>
      <tr>
        <td>
          <table>
                  <?php 
    mysql_connect('localhost', 'root', '');
    mysql_select_db('ogs');
?>

        <?php
        $id= ($_GET["id"]);


          $sql = "SELECT * FROM people
    WHERE id='$id' LIMIT 1";
          $result = mysql_query($sql);
          $row = mysql_fetch_array($result);
         ?>

          <form method="post" action="edit_data.php">
          <input type="hidden" name="id" value="<?php echo "$row[id]"; ?>">

            <fieldset>
                <legend><b>Name</b></legend>
                    First Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
                    Last Name:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
            </fieldset>
    <br><br>
            <fieldset>
                <legend><b>Contact Information</b></legend>
                    Town:<input type="text" name="town" size="20" value="<?php echo "$row[town]"; ?>">
                    Address:<input type="text" name="address" size="40" value="<?php echo "$row[address]"; ?>">
                    Province:<input type="text" name="province" size="20" value="<?php echo "$row[province]"; ?>">
                    Postal Code:<input type="text" name="postal_code" size="40" value="<?php echo "$row[postal_code]"; ?>">
    <br><br>
                    Home Phone:<input type="text" name="home_phone" size="20" value="<?php echo "$row[home_phone]"; ?>">
                    Cell Phone:<input type="text" name="cell_phone" size="40" value="<?php echo "$row[cell_phone]"; ?>">
            </fieldset>
    <br><br>        
            <fieldset>
                <legend><b>Emergency Contact</b></legend>
                    Emergency Contact Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
                    Emergency Contact Number:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
            </fieldset>
    <br><br>        
            <fieldset>
                <legend><b>Work Information</b></legend>
                    Role:<input type="text" name="role" size="20" value="<?php echo "$row[role]"; ?>">
                    Employer:<input type="text" name="company_works_for" size="40" value="<?php echo "$row[company_works_for]"; ?>">
    <br><br>
                    Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
                    Position:
                        <?php
    $position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
    $position_result = mysql_query($position_sql);
    echo "<select name='position'>";

    // You should use PHP to get the existing value here, I have made it up here as 14
    $existing_id = '$row[id]';

    while ($row = mysql_fetch_array($position_result))
    {
        // Check if the existing id is the same as the current id we are displaying
        // If it is, set the selected attribute
        if($existing_id == $row['id'])
            echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
        else
            echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
    }
    echo "</select>";
?>  

    <br><br>
                    Is Supervisor?:
                            <input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> Yes
                            <input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> No
    <br><br>        
                    Is Active?:
                            <input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> Yes
                            <input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> No
    <br><br>
                    Start Date:<input type="text" name="start_date" size="40" value="<?php echo "$row[start_date]"; ?>">
            </fieldset>


                <input type="submit"
              name="submit value" value="Update">

          </form>




      </div>
      </div>
</body>
</html>

尼古拉斯·弗隆(Nicholas Furlong)的紀錄顯示他是健康與安全協調員。 http://prntscr.com/3o34g2

但是,當我單擊“編輯”並轉到“編輯”頁面時,它已將他列為控制器。 (這是該列中的第一個選項。) http://prntscr.com/3o36qu

還不清楚您要問什么。 但是我會嘗試

<?php
    $position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
    $position_result = mysql_query($position_sql);
    echo "<select name='position'>";

    // You should use PHP to get the existing value here, I have made it up here as 14
    $existing_id = 14;

    while ($row = mysql_fetch_array($position_result))
    {
        // Check if the existing id is the same as the current id we are displaying
        // If it is, set the selected attribute
        if($existing_id == $row['id'])
            echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
        else
            echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
    }
    echo "</select>";
?>  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM