Create query with value of a Select

2

In advance, thanks for your help.

I am trying to make a form in which, when selecting an option in a combobox, take the selected value and can put together a query to insert a value into an input text. I'm stuck on how to put the value of the combobox in a variable and thus be able to arm my query. (the part of the connection to the base and how to call the cosulta I have defined, I just need to get the value to integrate it). This is my code

<?php
    $db_host="localhost";
    $db_nombre="herramienta";
    $db_usuatio="root";
    $db_contra="";
    $conexion=mysqli_connect($db_host,$db_usuatio,$db_contra,$db_nombre);
    $consulta_tec="SELECT * FROM tecnicos ";
    $consulta_sup="SELECT * FROM supervisores ";
    $consulta_pro="SELECT * FROM proyectos ";
    $consulta_suc="SELECT * FROM sucursales ";

    $list_tecnico=mysqli_query($conexion, $consulta_tec);
    $list_sup=mysqli_query($conexion, $consulta_sup);
    $list_pro=mysqli_query($conexion, $consulta_pro);
    $list_suc=mysqli_query($conexion, $consulta_suc);





 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>

</head>


<body class="oneColElsCtrHdr">



<center><div id="container">
<div id="header">
<h1>Despacho de Ingenieros</h1>
<!-- end #header --></div>
<div id="mainContent">
<form name="form1" method="POST" action="">
  <table width="200" >
    <tr> 
      <td>Fecha</td>
      <td><label>
        <input type="text" name="fecha" id="fecha">
      </label></td>
    </tr>
    <tr>
      <td>Hora de Inicio</td>
      <td><label>
        <input type="text" name="inicio" id="inicio">
      </label></td>
    </tr>
    <tr>
      <td>Hora Final</td>
      <td><label>
        <input type="text" name="fin" id="fin">
      </label></td>
    </tr>
    <tr>
      <td>Tiempo real</td>
      <td><label>
        <input type="text" name="real" id="real">
      </label></td>
    </tr>
    <tr>
      <td>Tecnico</td>
      <td><label>
              <select  name="tecnico" id="tecnico" value="" ><option 
selected="" value="">[Seleccione a un tecnico]</option>
                    <?php
            while ($fila_tec=mysqli_fetch_array($list_tecnico)){

                echo "<option value='". $fila_tec["Id"] ."'>" 
.$fila_tec["Nombre"] ."</opiton>";



            }

        ?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Supervisor</td>
      <td><label>
        <select name="supervisor" value=""><option selected="" value=""> 
[Selecciones a un supervisor]</option>
                    <?php
            while ($fila_sup=mysqli_fetch_array($list_sup)){

                echo "<option value='". $fila_sup["Id"] ."'>" 
.$fila_sup["Nombre"] ."</opiton>";


            }

        ?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Proyecto</td>
      <td><label>
              <select name="proyecto" id="proyecto" 
onchange="ShowSelected()" value=""><option selected="" value="" >[Seleccione 
un proyecto]</option>
                    <?php
            while ($fila_pro=mysqli_fetch_array($list_pro)){

                echo "<option value='". $fila_pro["Acronimo"] ."'>" 
.$fila_pro["Acronimo"] ."</opiton>";

                  }
                 ?>

        </select>

      </label></td>
    </tr>
    <tr>
        <td>Tiempo Proyecto</td> 
      <td><label>
        <input type="text" name="tiempo" id="tiempo" value="<?php echo 
$user;?>" >
      </label></td>
    </tr>

    <tr>
      <td>Sucursal</td>
      <td><label>
        <select name="sucursal" value=""><option selected="" value=""> 
  [Selecciones a un sucursal]</option>
                    <?php
            while ($fila_suc=mysqli_fetch_array($list_suc)){

                echo "<option value='". $fila_suc["SIRH"] ."'>" 
.$fila_suc["Nombre"] . "</opiton>";


            }

        ?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Cd Origen</td>
      <td><label>
        <input type="text" name="cdorigen" id="cdorigen">
      </label></td>
    </tr>
    <tr>
      <td>KM</td>
      <td><label>
        <input type="text" name="km" id="km">
      </label></td>
    </tr>
    <tr>
      <td>Division</td>
      <td><label>
        <input type="text" name="division" id="division">
      </label></td>
    </tr>
    <tr>
      <td>Zona</td>
      <td><label>
        <input type="text" name="zona" id="zona">
      </label></td>
    </tr>
    <tr>
      <td>SIRH</td>
      <td><label>
        <input type="text" name="sirh" id="sirh">
      </label></td>
    </tr>
   </table>
  <p>&nbsp;</p>
  </form>
  <h1>&nbsp;</h1>
  <h2>&nbsp;</h2>
 <p>&nbsp;</p>
</div>
 <div id="footer">
<p>Pie</p>
 </div>
</div></center>

   

It's my first attempt at php so I apologize for the obvious mistakes I made.

Greetings and thanks again!

    
asked by Ramses Cortes 06.12.2018 в 02:03
source

2 answers

0

I would do it this way ..

<?php
    $db_host="localhost";
    $db_nombre="herramienta";
    $db_usuatio="root";
    $db_contra="";
	$conexion = new mysqli($db_host,$db_usuatio,$db_contra,$db_nombre);

	$consulta_tec = $conexion->query("SELECT * FROM tecnicos");
	while($tec = $consulta_tec->fetch_assoc()){
		$lista_tecnicos = $lista_tecnicos.'<option value='. $tec['Id'] .'>'. $tec['Nombre'] .'</opiton>';
	}

	$consulta_sup = $conexion->query("SELECT * FROM supervisores");
	while($sup = $consulta_sup->fetch_assoc()){
		$lista_supervisores = $lista_supervisores.'<option value='. $sup['Id'] .'>'. $sup['Nombre'] .'</opiton>';
	}

	$consulta_pro = $conexion->query("SELECT * FROM proyectos");
	while($pro = $consulta_pro->fetch_assoc()){
		$lista_proyectos = $lista_proyectos.'<option value='. $pro['Acronimo'] .'>'. $pro['Acronimo'] .'</opiton>';
	}

	$consulta_suc = $conexion->query("SELECT * FROM sucursales");
	while($suc = $consulta_suc->fetch_assoc()){
		$lista_sucursales = $lista_sucursales.'<option value='. $suc['SIRH'] .'>'. $suc['Nombre'] .'</opiton>';
	}


 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>

</head>


<body class="oneColElsCtrHdr">



<center><div id="container">
<div id="header">
<h1>Despacho de Ingenieros</h1>
<!-- end #header --></div>
<div id="mainContent">
<form name="form1" method="POST" action="">
  <table width="200" >
    <tr> 
      <td>Fecha</td>
      <td><label>
        <input type="text" name="fecha" id="fecha">
      </label></td>
    </tr>
    <tr>
      <td>Hora de Inicio</td>
      <td><label>
        <input type="text" name="inicio" id="inicio">
      </label></td>
    </tr>
    <tr>
      <td>Hora Final</td>
      <td><label>
        <input type="text" name="fin" id="fin">
      </label></td>
    </tr>
    <tr>
      <td>Tiempo real</td>
      <td><label>
        <input type="text" name="real" id="real">
      </label></td>
    </tr>
    <tr>
      <td>Tecnico</td>
      <td><label>
              <select  name="tecnico" id="tecnico" value="" ><option 
selected="" value="">[Seleccione a un tecnico]</option>
        <?php echo $lista_tecnicos;?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Supervisor</td>
      <td><label>
        <select name="supervisor" value=""><option selected="" value=""> 
[Selecciones a un supervisor]</option>
            <?php echo $lista_supervisores;?>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Proyecto</td>
      <td><label>
              <select name="proyecto" id="proyecto" 
onchange="ShowSelected()" value=""><option selected="" value="" >[Seleccione 
un proyecto]</option>
            <?php echo $lista_proyectos;?>

        </select>

      </label></td>
    </tr>
    <tr>
        <td>Tiempo Proyecto</td> 
      <td><label>
        <input type="text" name="tiempo" id="tiempo" value="<?php echo 
$user;?>" >
      </label></td>
    </tr>

    <tr>
      <td>Sucursal</td>
      <td><label>
        <select name="sucursal" value=""><option selected="" value=""> 
  [Selecciones a un sucursal]</option>
         <?php echo $lista_sucursales;?>

    
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Cd Origen</td>
      <td><label>
        <input type="text" name="cdorigen" id="cdorigen">
      </label></td>
    </tr>
    <tr>
      <td>KM</td>
      <td><label>
        <input type="text" name="km" id="km">
      </label></td>
    </tr>
    <tr>
      <td>Division</td>
      <td><label>
        <input type="text" name="division" id="division">
      </label></td>
    </tr>
    <tr>
      <td>Zona</td>
      <td><label>
        <input type="text" name="zona" id="zona">
      </label></td>
    </tr>
    <tr>
      <td>SIRH</td>
      <td><label>
        <input type="text" name="sirh" id="sirh">
      </label></td>
    </tr>
   </table>
  <p>&nbsp;</p>
  </form>
  <h1>&nbsp;</h1>
  <h2>&nbsp;</h2>
 <p>&nbsp;</p>
</div>
 <div id="footer">
<p>Pie</p>
 </div>
</div></center>
    
answered by 06.12.2018 в 04:09
0

you capture the selection with $_POST[''] When you send the form ... then you play with those variables ... something like this:

<?php
    $db_host="localhost";
    $db_nombre="herramienta";
    $db_usuatio="root";
    $db_contra="";
	$conexion = new mysqli($db_host,$db_usuatio,$db_contra,$db_nombre);
	
	$tecnico_seleccionado = $_POST['tecnico'];
	$supervisor_seleccionado = $_POST['supervisor'];
	$proyecto_seleccionado = $_POST['proyecto'];
	$sucursal_seleccionado = $_POST['sucursal'];
	
	$consulta_tec = $conexion->query("SELECT * FROM tecnicos");
	while($tec = $consulta_tec->fetch_assoc()){
		$lista_tecnicos = $lista_tecnicos.'<option value='. $tec['Id'] .'>'. $tec['Nombre'] .'</opiton>';
		if($tec['Id'] == $tecnico_seleccionado){$nombre_tecnico_select = $tec['Nombre'];}
	}

	$consulta_sup = $conexion->query("SELECT * FROM supervisores");
	while($sup = $consulta_sup->fetch_assoc()){
		$lista_supervisores = $lista_supervisores.'<option value='. $sup['Id'] .'>'. $sup['Nombre'] .'</opiton>';
		if($sup['Id'] == $supervisor_seleccionado){$nombre_supervisor_select = $sup['Nombre'];}
	}

	$consulta_pro = $conexion->query("SELECT * FROM proyectos");
	while($pro = $consulta_pro->fetch_assoc()){
		$lista_proyectos = $lista_proyectos.'<option value='. $pro['Acronimo'] .'>'. $pro['Acronimo'] .'</opiton>';
		if($pro['Acronimo'] == $proyecto_seleccionado){$nombre_proyecto_select = $pro['Nombre'];}
	}

	$consulta_suc = $conexion->query("SELECT * FROM sucursales");
	while($suc = $consulta_suc->fetch_assoc()){
		$lista_sucursales = $lista_sucursales.'<option value='. $suc['SIRH'] .'>'. $suc['Nombre'] .'</opiton>';
		if($suc['SIRH'] == $sucursal_seleccionado){$nombre_sucursal_select = $suc['Nombre'];}
	}


 ?>
    
answered by 07.12.2018 в 00:37