Insert the value of two combos into a database table

0

I have two combobox, I can deploy them well, without any problem, the problem is that when I insert them into the database, I only add values from the first combobox, I do not know what I'm doing wrong, maybe something is missing from javascript to be able to return the value to the web form and so pass through post to upload the data to the database.

I do not know what I need to insert the value of the two comboboxes in the database table.

Connection file:

config.php

<?php
$connection = new mysqli("localhost", "root", "","sind2");
$mysqli = new mysqli("localhost", "root", "","sind2");
// Selecting Database

session_start();// Starting Session

?>

ing_carga.php

<form action="carga.php" method="post" name="combo" id="combo">
                          <table>
                            <tr>
                              Socio

  <script language="javascript">

        $(document).ready(function(){
          $("#listaNombre").change(function () {
           var texto=$("#listaNombre option:selected").html();

            $("#listaNombre option:selected").each(function () {

              num = $(this).val();

              $.post("getdatos.php", { num: num }, function(data){
                $("#listaidnum").html(data);


              });            
            });
          })
        });
   </script>
                                <div><select id="listaNombre" name="Nombre_socio" >
                                  <option>Seleccione Socio</option>
                                  <?php 
                                  $result = mysqli_query($mysqli, "SELECT Concat(Nombre,' ',Ap_Pat) as Nombre_socio, num  FROM miembros ORDER BY num DESC");
                                  while($row = $result->fetch_assoc()) { 
                                  ?>

                                  <option value="<?php echo $row['num']; ?>"><?php echo $row['Nombre_socio']; ?></option>

                                  <?php } ?>
                                 </select></div>


                                <div>Id Socio: <select name="num" id="listaidnum"></select></div>
</tr>
</table>
</form>

getdatos.php

  //llamada a Javascript

  <?php
  require('session.php');
  $num = $_POST['num'];

  $queryM = "SELECT  num FROM miembros WHERE num = '$num' ORDER BY num";
  $resultadoM = $mysqli->query($queryM);

  $html;

  while($rowM = $resultadoM->fetch_assoc())
  {
    $html.= "<option value='".$rowM['num']."'>".$rowM['num']."</option>";
  }

  echo $html;
?>

Carga.php

<?php
//including the database connection file
include_once("config.php");
include_once("ing_carga.php");

if(isset($_POST['Submit'])) { 

  $Nombre_socio = mysqli_real_escape_string($mysqli, $_POST['Nombre_socio']);

  $num =mysqli_real_escape_string($mysqli, $_POST['num']); 





  // checking empty fields
  if(empty($Nombre_socio)  ) {

    if(empty($Nombre_socio)) {
      echo "<font color='red'>Nombre esta vacio.</font><br/>";
    }



    //link to the previous page
    echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
  } else { 
    // if all the fields are filled (not empty) 

    //insert data to database 
    $result = mysqli_query($mysqli, "INSERT INTO cargas(num, Nombre_socio) VALUES('$num','$Nombre_socio')");

    //display success message
    echo "<font color='green'>Data added successfully.";
    echo "<br/><a href='ver_carga.php'>View Result</a>";
  }
}
?>

sql

create database sind2;

use sind2;
CREATE TABLE miembros (
  num int(3) NOT NULL auto_increment,
  Nombre varchar(30) NOT NULL,
  Ap_Pat varchar(30) NOT NULL,
  PRIMARY KEY  ('num')
);
create table cargas(
  id_carga int(3) not null auto_increment,
  num int(3) NOT NULL,
  Nombre_socio (60) not null,
 PRIMARY KEY ('id_carga')
);

INSERT INTO miembros (num, Nombre, Ap_Pat) values(1, "Mauricio","Concha");
INSERT INTO miembros (num, Nombre, Ap_Pat) values(2, "Tamara","Martinez");
INSERT INTO miembros (num, Nombre, Ap_Pat) values(3, "Cecilia","Loisa");
INSERT INTO miembros (num, Nombre, Ap_Pat) values(4, "Maria","Delgado");


INSERT INTO cargas (id_carga, Nombre_Socio, num) values(1, "Mauricio concha",1);
INSERT INTO cargas (id_carga, Nombre_socio, num) values(2, "Maria Delgado",4);
    
asked by Mauricio Havliczek 02.01.2019 в 15:51
source

0 answers