Help with the select in a combobox

1

I'm using a select to save data from a database, but when selecting several of select only saves me the last one selected from the combobox box and I want to logically save all the selected.

This is the code I have

<?php
    require ('../php/conexion2.php');
    $id_clasificacion = $_GET['estado_id'];

    echo 'tipo de emergencia: <select multiple="multiple" size="5" name="tipo"  onChange="getPersonal(this.value) ;"     >';
    $query = "SELECT id_tipo, nombre FROM tipo_emergencia WHERE id_clasificacion= '$id_clasificacion' ORDER BY nombre";

    if($resultado=$mysqli->query($query)){
        while($row = $resultado->fetch_assoc()){
    ?>
    <option value="<?php echo $row['nombre']; ?>"><?php echo $row['nombre']; ?></option>
    <?php
        }
    }
    echo '</select>';
?>
    
asked by Juan Lozano 12.04.2017 в 23:27
source

2 answers

1

You must insert using insert into :

First code:

<HTML>
  <BODY class="con">
    <!-- CONTENIDO DE LA PAGINA -->
    <h2>Formulario de inscripción:</h2>
    <br><hr>
    <div class="account-box">
      <form name="registro" id="registro" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
        <!-- lo pego aqui? yes -->
        <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php }?>
        <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php }?>

        <span>Nombres: </span>
        <input type="text" name="nombre" value="" placeholder="Digita tus nombres"><br><br><br>
        <span>Apellidos: </span>
        <input type="text" name="apellido" value="" placeholder="Digita tus apellidos"><br><br><br>
        <span>Teléfono: </span>
        <input type="text" name="telefono" value="" placeholder="Digita tu teléfono"><br><br><br>
        <span>Dirección: </span>
        <input type="text" name="direcccion" value="" placeholder="Digita tu direccción"><br><br><br>
        <span>Correo electrónico: </span>
        <input type="text" name="correo" value="" placeholder="Digita tu correo"><br><br><br>
        <button type="submit" name="boton">Registrarse</button>
      </form>
    </div>
  </body>
</HTML>

Second code:

</HEAD>
  <?php require('connect.php');
  //If the values are posted, insert them into the database.
  //if (
  //  isset($_POST['nombre'])
  //  && isset($_POST['apellido'])
  //  && isset($_POST['telefono'])
  //  && isset($_POST['direccion'])
  // && isset($_POST['correo'])){

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $smsg = "POST enviado.";
    if( isset($_POST['boton']) ) {
      echo "pasó por aquí ";
      // console.log("¿Entró al if?");
      $_nombre = $_POST['nombre'];
      $_apellido = $_POST['apellido'];
      $_telefono = $_POST['telefono'];
      $_direccion = $_POST['direccion'];
      $_correo = $_POST['correo'];

      $query = "INSERT INTO 'usuarios' (nombre,apellido,telefono,direccion,correo) VALUES ('$_nombre', '$_apellido', '$_telefono', '$_direccion', '$_correo' )";
      $result = mysqli_query($connection, $query);

      //print("Conexion+$connection");
      //print("Consulta+$query");

      if($result){
        $smsg = "Usuario creado correctamente.";
      }else{
        $fmsg = "Usuario no registrado";
      }
    }
  }
}
?>
    
answered by 15.04.2017 в 07:59
0

The solution I propose for this problem is:

In the html:

 <form id="form1" name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
    <select name="selector[]" size="5" multiple="multiple"> <!-- crea un arreglo para ser enviado por metodo de post o de get -->
            <option value="1">uno</option>
            <option value="1">dos</option>
            <option value="1">tres</option>
          </select>
</form>

In the PHP script:

    <?php
        require ('../php/conexion2.php');

        $opciones = "";
        $esPrimero = 0;
        foreach ($_GET['selector'] as $opcionesSeleccionadas){
          echo $opcionesSeleccionadas."\n";
           if ($esPrimero == 0){
            $opciones =  $opciones . $opcionesSeleccionadas;
            $esPrimero = 1;
           }
           else{
            $opciones =  $opciones . ", " . $opcionesSeleccionadas;
           }

        }
    $query = "SELECT id_tipo, nombre FROM tipo_emergencia WHERE id_clasificacion IN('$opciones') ORDER BY nombre";

  // ETC ETC 
?>
    
answered by 15.04.2017 в 08:21