"Select" with data from different tables

0

I need help. I have to create with PHP , the options of a select with the data coming from different tables MySql , and once selected, insert them in another table.

I have this query SQL :

$consulta =  "SELECT idCausa,
    Deudor_idDeudor,
    Cliente_idCliente,
    TiposCausas_idTiposCausas,
    Tribunal_idTribunal,
    SubMateria,
    Rol,
    RutPerJurid,
    ClienteAsunto,
    Abogado,
    Procurador,
    Estado,
    Fecha
    FROM causa WHERE idCausa = " . $idCausa;

and the JOIN are these:

$consulta = "SELECT c.idCausa, 
    DATE_FORMAT(c.Fecha, '%d.%m.%Y') 'Fecha',
    d.nombre 'Deudor', 
    cc.nombre 'Cliente',
    tc.Descripcion 'TiposCausas', 
    tb.DescTribunal 'Juzgado', 
    sm.Descripcion 'SubMateria', 
    c.Rol, c.RutPerJurid, 
    c.ClienteAsunto,
    u.nombres 'Abogado',
    pc.Procurador,
    ec.estado ,
    c.Documento
         FROM causa c
         INNER JOIN usuario u ON u.rut = c.abogado
         LEFT JOIN deudor d ON c.Deudor_idDeudor= d.idDeudor
         LEFT JOIN cliente cc ON c.Cliente_idCliente = cc.idCliente
         LEFT JOIN tiposcausas tc ON tc.idTiposCausas =  c.TiposCausas_idTiposCausas
         LEFT JOIN submateria sm ON sm.idSubTiposCausas = c.SubMateria
         LEFT JOIN tribunal tb ON tb.idTribunal = c.Tribunal_idTribunal
         LEFT JOIN procuradores pc ON pc.idProcurador = c.procurador
         LEFT JOIN estadoscausas ec ON ec.idestado = c.estado
         ORDER BY c.idCausa desc;

I tried the following and it did not work:

<select>
    <option value="0">Selección:</option>

<?php

    $query = $mysqli -> query ( "SELECT * FROM datos" );

    while ( $valores = mysqli_fetch_array( $query )) {

        echo '<option value="' . $valores[ idcausa ]. ' ">'</option>';

    }
?>

</select>

Any idea how I can do it? I need to do it urgently and my ideas run out.

update I did what I was told and the option still does not show data the code was something like this:

<?php
 
 
include("conexion.php");

header("Content-Type: text/html;charset=ISO-8859-1");
session_start();
if(isset($_SESSION['nombreusu']))
{
?>

<html lang="en">
<div class="form-group">
				<div class="form-row">
				  <div class="col-md-6">
				<label for="Deudor_idDeudor">Id Deudor</label>
				<select>
    			<option value="0" >Selección:</option>

				<?php

   			 $resultado = $mysqli -> query ( "SELECT * FROM causa" );

  		  while ( $valores = $resultado->fetch_array(MYSQLI_ASSOC)) {
		echo '<option value="' . $valores[ 'idcausa' ] . ' ">' . $valores[ 'idcausa' ] . '</option>';
   								 }
					?>

				</select>
		 </div>
	</div>
					
    
asked by Tondrax 06.11.2017 в 08:31
source

2 answers

1

I'll let you point out some things:

  • You are using object-oriented style and style by procedures in the same code.

    Here you use the object-oriented style:

    $query = $mysqli -> query ( "SELECT * FROM datos" );

    Here you use the style by procedures:

    while ( $valores = mysqli_fetch_array( $query )) {

  • The mixture of styles is a practice not recommended by the PHP Manual , for obvious reasons.

      

    Mix styles

         

    It is possible to change between styles at any time. I dont know   recommends mixing the two styles for reasons of clarity and style of   code.

    You can use the object-oriented style ... it's clearer and it's more modern . If you want more details about this you can check the answer to this question: Difference between new mysqli and mysqli_connect

  • If you want to read the values using the names of the columns within while then you must indicate that you want an associative array in the results. The reading is done like this: $valores["idcausa"] , indicating in quotes " the name of the column, which is missing in your code.
  • You lack the value that must be shown outside the option . Note that the option has the following syntax: <option value="valor">Valor que se muestra</option> , your code does not indicate anything in Valor que se muestra . I will assume in the code proposal that you want to show a column of the table in the database called causa .
  • EDITED CODE

    This is a complete proposal, which controls the entire flow of the program, has a variable $arrError dedicated to collecting any errors that might occur. That way the code always says something.

    Let's see:

    <?php
    include("conexion.php");
    header("Content-Type: text/html;charset=ISO-8859-1");
    session_start();
    if(isset($_SESSION['nombreusu']))
    {
    ?>
    
    <html lang="en">
    
    <?php
            if ($mysqli){    
                $resultado = $mysqli -> query ( "SELECT * FROM causa" );
                 if ($resultado->num_rows > 0){
                    $strHTML='<div class="form-group">';
                    $strHTML.='<div class="form-row">';
                    $strHTML.='<div class="col-md-6">';
                        $strHTML.='<label for="Deudor_idDeudor">Id Deudor</label>';
                        $strHTML.='<select>';
                            $strHTML.='<option value="0" >Selección:</option>';
                    while ( $valores = $resultado->fetch_array(MYSQLI_ASSOC)) {
                            $strHTML.='<option value="' . $valores[ 'idcausa' ] . ' ">' . $valores[ 'idcausa' ] . '</option>';
                    }
                        $strHTML.='</select>';
                    $strHTML.='</div>';
                    $strHTML.='</div>';
                    $strHTML.='</div>';
                    echo $strHTML;
    
                }else{
    
                    $arrError=array("mensaje"=>"No se encontraron registros");      
                }
    
            }else{
    
                $arrError=array("mensaje"=>"La conexión a la base de datos no se puedo establecer. Revise conexion.php");   
            }
    
    }else{  
    
        $arrError=array("mensaje"=>"No hay datos en nombreusu");
    
    }
    
    /*Verificamos si hubo errores*/
        if ($arrError){
    
            echo $arrError["mensaje"];
        }
    ?>
    </html>
    
        
    answered by 06.11.2017 в 12:38
    0

    Assuming that your query is correct and you have the data when composing the option you are not printing a visible content, your options would be displayed empty ... Also you do not use the key of the array as a string so you would not get the data. It should be:

        echo '<option value="' . $valores[ 'idcausa' ] . ' ">' 
    . $valores[ 'idcausa' ] . '</option>';
    
        
    answered by 06.11.2017 в 09:18