Problems obtaining the ID of a record within a dropdown

0

Good day.

I am elaborating two forms in which in the first there will be a dropdown to show data from a MySQL table and when selecting one of the records, in the second form your information will appear in a textbox and a dropdown to be updated if it is necessary.

At the moment I am having two problems, one at the moment of wanting to update I get an error of type "Undefined Index" and it is not sending to the procedure the ID.

To be more exact, this is the error that appears:

  

Notice: Undefined index: cmbID in ... \ connectionBD_UpdateEmployee.php on line 15   Error: call sp_UpdateEmployee ('', 'Maria', 'Salas', 'Martinez', 'Salinas', '6631231234', '[email protected]', '4')   Incorrect integer value: '' for column 'EmployeeID' at row 1 **

And the code that I have at this moment is this:

<!doctype HTML>
<html>
  <head>
    <title>Actualizar un Registro de Empleado</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
            <?php
                require_once("conexionBD_ActualizarEmpleado.php");
                $sql="SELECT empleado.Empleado_ID, empleado.Nombre, empleado.Segundo_Nombre, empleado.Apellido_Paterno, empleado.Apellido_Materno, empleado.Telefono, empleado.Correo_Electronico, empleado.Puesto_Puesto_ID, puesto.Nombre_Puesto FROM empleado inner join puesto on empleado.Puesto_Puesto_ID = puesto.Puesto_ID";
        $sql2="SELECT Puesto_ID, Nombre_Puesto FROM puesto";
                $result2 = $conn->query($sql2);
                $result = $conn->query($sql);
            ?>
        </script>
    <link rel="stylesheet" href="Estilos_Submenu.css">
    <script type='text/javascript'>
      $(document).ready(function(){
      $('#nombre').val($('#selectdata option:selected').data('nombre'));
      $('#segnombre').val($('#selectdata option:selected').data('segnombre'));
      $('#apaterno').val($('#selectdata option:selected').data('apaterno'));
      $('#amaterno').val($('#selectdata option:selected').data('amaterno'));
      $('#tel').val($('#selectdata option:selected').data('tel'));
      $('#email').val($('#selectdata option:selected').data('email'));
      $('#puesto').val($('#selectdata option:selected').data('puesto'));
      $(function(){
          $('#selectdata').change(function(){
              $('#nombre').val($('#selectdata option:selected').data('nombre'));
              $('#segnombre').val($('#selectdata option:selected').data('segnombre'));
              $('#apaterno').val($('#selectdata option:selected').data('apaterno'));
              $('#amaterno').val($('#selectdata option:selected').data('amaterno'));
              $('#tel').val($('#selectdata option:selected').data('tel'));
              $('#email').val($('#selectdata option:selected').data('email'));
              $('#puesto').val($('#selectdata option:selected').data('puesto'));
          });
      });
      });
  </script>
  </head>
  <body>
    <h1>Actualizar Registro de Empleado</h1>
    <form class="form1" action="" method="POST">
        Empleado: <select name="cmbID" id="selectdata">
          <?php
            if($result->num_rows>0){
              while($row=$result->fetch_assoc()){
                echo '<option value=" '.$row['Empleado_ID'].'" data-nombre="'.$row['Nombre'].'" data-segnombre="'.$row['Segundo_Nombre'].'" data-apaterno="'.$row['Apellido_Paterno'].'" data-amaterno="'.$row['Apellido_Materno'].'" data-apaterno="'.$row['Apellido_Paterno'].'" data-tel="'.$row['Telefono'].'" data-email="'.$row['Correo_Electronico'].'" data-puesto="'.$row['Puesto_Puesto_ID'].'">'.$row['Empleado_ID'].'. '.$row['Nombre'].' '.$row['Segundo_Nombre'].' '.$row['Apellido_Paterno'].' '.$row['Apellido_Materno'].'</option>';
              }
            }
            $conn->close();
          ?></select><br /><br />
    </form>
    <br />
    <form class="form2" action="conexionBD_ActualizarEmpleado.php" method="POST">
            Nombre: <input type="text" name="txtNombre" maxlenght="45" id="nombre"/></br></br>
            Segundo Nombre: <input type="text" name="txtSegundoNom" maxlenght="45" id="segnombre"/></br></br>
            Apellido Paterno: <input type="text" name="txtApellidoPa" maxlenght="45" id="apaterno"></br></br>
            Apellido Materno: <input type="text" name="txtApellidoMa" maxlenght="45" id="amaterno"></br></br>
            Telefono: <input type="text" name="txtTelefono" maxlenght="45" id="tel"/></br></br>
            Correo Electronico: <input type="text" name="txtEmail" maxlenght="45" id="email"/></br></br>
            Puesto: <select name="cmbPuesto" id="puesto">
        <?php
                if($result2->num_rows > 0){
                    while($row=$result2->fetch_assoc()){
                        echo '<option value=" '.$row['Puesto_ID'].' "> '.$row['Nombre_Puesto'].' </option>';
                    }
                }
                $conn->close();
            ?>
      </select>
      <br/><br/>
      <input type="submit" name="btnActualiza" value="Actualizar Datos">
    </form>
  </body>
</html>

And this is the code with the BD information and the procedure call:

    <?php
    $servername="127.0.0.1";
    $username="root";
    $password="password";
    $dbname="proyecto_hotel";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if($conn->connect_error){
        die("La conexion fallo".$conn->connect_error);
    }


    $sql="call sp_ActualizarEmpleado('".$_POST['cmbID']."', '".$_POST['txtNombre']."', '".$_POST['txtSegundoNom']."', '".$_POST['txtApellidoPa']."', '".$_POST['txtApellidoMa']."', '".$_POST['txtTelefono']."', '".$_POST['txtEmail']."', '".$_POST['cmbPuesto']."')";

    if($conn->query($sql) === TRUE){
        echo "New record updated";
    }
    else{
        echo "Error: ".$sql."</br>".$conn->error;
    }
?>

Is there any way in which I can get the ID of the first form (where I send that data is in the variable 'cmbID' which is the name of the dropdown) to use it in the procedure or something that is not considered in my code ?

    
asked by Space_Shift 04.12.2018 в 23:42
source

1 answer

0

That's because the submit you are sending from form 2, which does not have the select cmbID, my suggestion is that you create in form 2 an input of type hidden, to which you assign the value of select cmbID when this change.

First remember to add Jquery to the Head tag of the html

All Javascript code should look something like this

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript">

    $('#selectdata').on('change', function() {
      $("#hiddendata").val(this.value);
    });

</script>
</head>

And the form2 something like that

 <form class="form2" action="conexionBD_ActualizarEmpleado.php" method="POST">
            Nombre: <input type="text" name="txtNombre" maxlenght="45" id="nombre"/></br></br>
            Segundo Nombre: <input type="text" name="txtSegundoNom" maxlenght="45" id="segnombre"/></br></br>
            Apellido Paterno: <input type="text" name="txtApellidoPa" maxlenght="45" id="apaterno"></br></br>
            Apellido Materno: <input type="text" name="txtApellidoMa" maxlenght="45" id="amaterno"></br></br>
            Telefono: <input type="text" name="txtTelefono" maxlenght="45" id="tel"/></br></br>
            Correo Electronico: <input type="text" name="txtEmail" maxlenght="45" id="email"/></br></br>
            Puesto: <select name="cmbPuesto" id="puesto">
        <?php
                if($result2->num_rows > 0){
                    while($row=$result2->fetch_assoc()){
                        echo '<option value=" '.$row['Puesto_ID'].' "> '.$row['Nombre_Puesto'].' </option>';
                    }
                }
                $conn->close();
            ?>
      </select>
      <br/><br/>
       <input type="hidden" id="hiddendata" name="cmbID" value="">
      <input type="submit" name="btnActualiza" value="Actualizar Datos">
    </form>
    
answered by 04.12.2018 / 23:59
source