samedi 26 septembre 2015

Combine multiple POSTs and adding it to table

Here is my code to add multiple types to table. I want to combine areas, location, types and add them in the table at once. I think this just wont work if(!empty($_POST['types'] && $_POST[''] && $_POST[''] ) Thanks!

if(!empty($_POST['types'])) {   
    $values = array();
    foreach($_POST['types'] as $typ_id) {
        $values[] = sprintf('(%d, %d)', $station_id, $typ_id);
    }

    $query = 'INSERT IGNORE INTO station_typ_tab
          (station_id, typ_id, area_id, location_id)
          VALUES ' .
          implode(',', $values);
          mysql_query($query, $db) or die(mysql_error($db));
   }

EDIT: here is part of code for types[] and same is for areas and location

<td>Types:<br/> <small>CTRL + click to set multiple pollutants</em></small>
   </td>
   <td>
   <?php
    $query = 'SELECT typ_id, typ FROM typ_tab ORDER BY typ ASC';
    $result = mysql_query($query, $db) or die(mysql_error($db));

    if (mysql_num_rows($result) > 0) {
        echo '<select multiple name="types[]">';
        while ($row = mysql_fetch_array($result)) {
           if (isset($station_typ[$row['typ_id']])) {
              echo '<option value="' . $row['typ_id'] . '"
              selected="selected">';
              } else {
                  echo '<option value="' . $row['typ_id'] .'">'; 
              }
              echo $row['typ'] . '</option>';
           }
           echo '</selected>';
        } else {
          echo '<p><strong>Databaza je prazdna... <a href="edit_pollutant.php">Enter database</a></strong></p>';
        }
        mysql_free_result($result);

how to combine $_POST for types, location and areas if they comes from different selecting input. something like if(!empty($_POST['types'] && $_POST['areas'] && $_POST['location']) ){ $values = array(); foreach( NEW VARIABLE as $typ_id && area_id &&location_id) { $values[] = sprintf('(%d, %d, %d, %d)', $station_id, $typ_id, area_id, location_id); if it is possible to do it like this

Aucun commentaire:

Enregistrer un commentaire