简体   繁体   中英

Query MySQL with array of values from select box

Using a simple PHP form I am trying to generate a report only displaying the values selected in a dynamically populated select box.

   <div id="container">
      <div>
        <form action="report.php" method="post">
            <label>Select Devices: </label>
            <select name="devices[]" multiple>
                <?php
                    require 'config/db.php';
                    $pdo = Database::connect();
                    $sql = 'SELECT alarm_device_description FROM alarm_device';
                    foreach($pdo->query($sql) as $row) {
                        echo '<option>'. $row['alarm_device_description'] . '</option>';
                    }
                    Database::disconnect();
                ?>
            </select>
            <button type="submit">Get Report</button>
        </form>
      </div>
    </div>

Then I have a php file:

<div id="container">
  <div>
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Alarm Device Description</th>
        </tr>
      </thead>
      <tbody>
        <?php
          require 'config/db.php';
          $pdo = Database::connect();

          var_dump($_POST);
          echo '<hr />';

          $devices = $_POST['devices'];
          echo $devices . '<hr />';

          $sql = 'SELECT date, alarm_device_description
                  FROM alarm_log
                  NATURAL JOIN alarm_device
                  ORDER BY date ASC';

          print_r($_POST['devices']);
          echo '<hr />';

          foreach ($pdo->query($sql) as $row) {
            echo '<tr>';
            echo '<td>'. $row['date'] . '</td>';
            echo '<td>'. $row['alarm_device_description'] . '</td>';
            echo '</tr>';
          }
          Database::disconnect();
        ?>
      </tbody>
    </table>
  </div>
</div>

The report by default is supposed to generate all the alarm_devices, and that works fine. However, I need to be able to select for example: iPhone 5, and Nexus and only display those results.

I have tried to pull in the value as an array which in the POST tests on my reports.php page show - it does seem to grab the selected values.

The $devices = $_POST['devices'] only produces an empty array however.

I need to figure out how to modify my query to post the selected values, and by default all of the values.

Thanks in advance and I hope that makes sense. I am a front-end developer and not very well versed in PHP/MySQL

<option> should have a value attribute which will be passed to server:

For example:

<select name="select_name">
    <option value="2">Text 2</option>
</select>

On server side after you select Text 2 option, it will be:

echo $_POST['select_name'];    // 2

In case with multiple :

<select name="select_name" multiple>
    <option value="2">Text 2</option>
    <option value="3">Text 3</option>
</select>

On server side after you select both options it will be:

print_r $_POST['select_name'];    // array(2,3);
print_r $_POST['select_name'];    // array(2); if you select one option

As you select alarm_device_description field - you can try:

foreach($pdo->query($sql) as $row) {
    echo '<option value="' . $row['alarm_device_description'] . '">'. $row['alarm_device_description'] . '</option>';
}

You are missing the value attribute while generating the select box.

change

echo '<option>'. $row['alarm_device_description'] . '</option>';

to

echo '<option value="' . $row['alarm_device_description'] . '">' . $row['alarm_device_description'] . '</option>';

This way your $_POST['devices'] array would be poplulated with items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM