使用 PHP 从 HTML 表单选择框传递多个值到 SQL 语句


Passing multiple values from HTML form select box with PHP to SQL statement

我有一个带有选择框的表单,用户可以在其中从数据库中选择预先存在的字段:

<form action='' method='POST' autocomplete='off' enctype="multipart/form-data">
  <p><strong>Title and Description:</strong></p>
  <select name='uidtitle' class='chosen-select'>
  <option value="0" ></option>
    <?php 
      $result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
      while ($row = $result->fetch_array(MYSQLI_ASSOC)){
        echo "<option value='"1'">" . $row['title'] ." - " . $row['description'] . "</option>";
        $uid = $row['uid'];
        $title = $row['title'];
        $desc = $row['description'];
      }
    ?>
    </select>
...

如何将这三个值(分别(发送到 SQL 的回发?

if (isset($_POST['submitted'])) { 
  //Get params for prepared statements
  $startDatec= date("Y-m-d H:i:s", strtotime($_POST['startEventDate']));
  $endDatec= date("Y-m-d H:i:s", strtotime($_POST['endEventDate']));
  $startTimec=$_POST['startTime'];
  $endTimec=$_POST['endTime'];
  $recurc=$_POST['recurrence'];
  $finalc= date("Y-m-d H:i:s", strtotime($_POST['finalDate']));
  ...
我不知道

为什么你需要发回所有三个值。 数据库键的存在是为了能够在给定单个字段的情况下识别记录中的所有字段,在这种情况下,我假设uid. 仅传递该字段将允许您在执行所需的操作之前选择回发中的其他字段。

但是,可以使用隐藏的表单字段,尽管我不提倡这种方法。

<select name='uidtitle' class='chosen-select'>
  <option value="0" ></option>
    <?php 
      $result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
      $cacheArray = array(); // Used to store the information to be used below
      while ($row = $result->fetch_array(MYSQLI_ASSOC)){
        echo "<option value='"" . $row['uid'] . "'">" . $row['title'] ." - " . $row['description'] . "</option>";
          $cacheArray[] = $row;
      }
    ?>
    </select>
<?php
    foreach($cacheArray as $k => $v) {
        echo '<input type = "hidden" name = "title-' . $v['uid'] . '" value = "' . $v['title'] . '">';
        echo '<input type = "hidden" name = "description' . $v['uid'] . '" value = "' . $v['description'] . '">';
    }
?>

隐藏的表单字段将出现在tblFacilityHrs表中的所有记录中。 通过将uid附加到名称中,可以使名称变得不同。 您可以通过以下方式确定回发中感兴趣的字段:

$_POST['title-'.$_POST['uidtitle']]
$_POST['description-'.$_POST['uidtitle']]