简体   繁体   English

使用下拉菜单编辑mysql数据

[英]Editing mysql data with drop downs

I have a data entry page where a few drop downs exist. 我有一个数据输入页面,其中存在一些下拉列表。 The selected item in the drop down stores in my mysql database with no issue. 下拉列表中的选定项目存储在我的mysql数据库中,没有问题。

I've made a 2nd page to edit individual records. 我已经做了第二页来编辑个人记录。 I can display the data from the drop down in a text box with no problem. 我可以毫无问题地从下拉列表中显示数据。 However, I want to be able to edit the result with the same drop down choices. 但是,我希望能够使用相同的下拉选项来编辑结果。

I can add the drop down menu on my edit.php page with all the correct options, but the stored value from the database does not appear. 我可以在edit.php页面上添加带有所有正确选项的下拉菜单,但是不会显示数据库中存储的值。 Instead I get the first choice by default and not the stored value. 取而代之的是,默认情况下我会得到第一选择,而不是存储值。

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

I'm using POST and GET to get the correct record ID. 我正在使用POST和GET获取正确的记录ID。

My text boxes, which work fine are as follows: 我的文本框如下所示:

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

I'm assuming I have to build some sort of if statement to display the stored value? 我假设我必须构建某种if语句来显示存储的值?

not sure if this helps, but this is my how I grab the ID for the record I want to edit: 不确定是否有帮助,但这是我如何获取要编辑的记录的ID:

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


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

Entire Code: 完整代码:

    <!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>

The record for Nicholas Furlong shows him as a Health & Safety Coordinator. 尼古拉斯·弗隆(Nicholas Furlong)的纪录显示他是健康与安全协调员。 http://prntscr.com/3o34g2 http://prntscr.com/3o34g2

But when I click edit, and go to my edit page, it has him listed as controller. 但是,当我单击“编辑”并转到“编辑”页面时,它已将他列为控制器。 (This is the first option in the column.) http://prntscr.com/3o36qu (这是该列中的第一个选项。) http://prntscr.com/3o36qu

Its not entirely clear what you are asking. 还不清楚您要问什么。 But I will try 但是我会尝试

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