在HTML中选择一个值后选择ID

我有ID,姓氏和使用查询从数据库中选择的人的名字。 因此,有一个表示例和带有变量的查询。

+----+--------+---------+
| ID | NAME   | SURNAME |
+====+========+=========+
|  1 | limone | salmone |
+----+--------+---------+
|  2 | john   | bobbino |
+----+--------+---------+

  $queryN = "SELECT `ID`,`NAME`,`SURNAME` FROM `t_people` WHERE 1";
  $sql = mysqli_query($con , $queryN);
  $people = array();
  $id = array();
  while($row = $sql -> fetch_array(MYSQLI_ASSOC))
    {
      $people[] = $row["NAME"]." ". $row["SURNAME"];//limone salmone
      $id[] = $row["ID"];//1
    }

I would like to have in output in an HTML select the name and the surname of the selected person and the ID of the person chosen when I pass in an other page. I have this code below that works pretty good, but with this solution, I have in other page only the value of $people. It is a problem, because in the database could be more than one person with same name and surname.

    <label class="control-label" for="NCPersona">Seleziona il nome della persona</label>
        <select name="NCPersona" id="validationDefaultUsername" class="form-control" required>
          <?php
          foreach($people as $value) { ?>
          <option value="<?= $value?>"><?= $value?></option>
          <?php
      } ?>
        </select>

因此,我认为最好的办法是通过页面传递该人的ID。 我该怎么办? 也许在价值阵营中使用ID?谢谢大家! :)

上面有我的代码示例

        <label for="NCPersona">Select the person's name</label>
            <select name="NCPersona" required>
              <option value="limone salmone">limone salmone</option>
              <option value="john bobbino">john bobbino</option>
            </select>
评论
  • 看透不说透
    看透不说透 回复

    您的sql请求如何工作?应该是这样的:

    "SELECT `ID`,`NAME`,`SURNAME` FROM `t_people` WHERE `id`=1"
    

    you do not have an id

    您的身份证在哪里

  • 烦恼%
    烦恼% 回复

    您要做的就是检索表数据并构建一个值数组,如下所示:

    $queryN = "SELECT * FROM `t_people`";
    $sql = mysqli_query($con , $queryN);
    $people = array();
    $id = array();
    while($row = $sql -> fetch_array(MYSQLI_ASSOC)) {
      $people[] = array(
        'id' => $row["ID"],
        'name' => $row["NAME"]." ". $row["SURNAME"],
      );
    }
    

    然后,在准备好阵列后,可以构建具有ID作为值的选择框,如下所示:

    <label class="control-label" for="NCPersona">Seleziona il nome della persona</label>
    <select name="NCPersona" id="validationDefaultUsername" class="form-control" required>
      <?php
      foreach($people as $value) { ?>
      <option value="<?= $value['id'] ?>"><?= $value['name'] ?></option>
      <?php } ?>
    </select>
    

    然后,任何特定于用户的查询都应通过提供的ID而不是名称进行查找。

    $id = 'user id here';
    $queryN = sprintf("SELECT `ID`,`NAME`,`SURNAME` FROM `t_people` WHERE %s", $id);