how can I select what I have in my database in this php file?

1

good! my problem is that I want to put a select of 2 tables but it does not leave my PHP file. this is the code

<?php
include('conexion.php');
$paginaActual = $_POST['partida'];

$nroProductos = mysql_num_rows(mysql_query("SELECT * FROM tipo_emergencia"));
$nroLotes = 10;
$nroPaginas = ceil($nroProductos/$nroLotes);
$lista = '';
$tabla = '';

if($paginaActual > 1){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual-1).');">Anterior</a></li>';
}
for($i=1; $i<=$nroPaginas; $i++){
    if($i == $paginaActual){
        $lista = $lista.'<li class=""><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }else{
        $lista = $lista.'<li><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }
}
if($paginaActual < $nroPaginas){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual+1).');">Siguiente</a></li>';
}

if($paginaActual <= 1){
    $limit = 0;
}else{
    $limit = $nroLotes*($paginaActual-1);
}

$registro = mysql_query("SELECT * FROM tipo_emergencia LIMIT $limit, $nroLotes");


$tabla = $tabla.'<table class="table table-bordered table-inverse">
                    <tr class="bg-primary">

                        <th width="300">Emergencia</th>

                        <th width="50">Opciones</th>
                    </tr>';

while($registro2 = mysql_fetch_array($registro)){
    $tabla = $tabla.'<tr>
                        <td>'.$registro2['nombre'].'</td>

                        <td><a href="javascript:editarProducto('.$registro2['id_tipo'].');"  class="glyphicon-pencil"></a> <a href="javascript:eliminarProducto('.$registro2['id_tipo'].');" class="glyphicon glyphicon-trash"></a></td>
                      </tr>';       
}


$tabla = $tabla.'</table>';
$array = array(0 => $tabla,
               1 => $lista);
echo json_encode($array);
?>

I want the select to put this from my database

Select Tipo_emergencia.Id_Tipo, Tipo_emergencia.Nombre,  clasificacion_emergencia.nombre_clas  FROM tipo_emergencia,clasificacion_emergencia WHERE tipo_emergencia.id_clasificacion=clasificacion_emergencia.id_clasificacion; LIMIT $limit, $nroLotes

but at the moment of executing it in the browser, nothing comes out. the database is already good

In a few words I want you to show me that in the database interface. I already put it in the select but it will not let me. thanks

    
asked by Juan Lozano 29.03.2017 в 21:13
source

1 answer

1

This would be the adapted code:

<?php
include('conexion.php');
/* $_POST['partida'], debería ser validado y escapado */
$paginaActual = $_POST['partida'];
/* obtienes el número de registros de la nueva consulta */
$nroProductos = mysql_num_rows(mysql_query("
SELECT 
    Tipo_emergencia.Id_Tipo, 
    Tipo_emergencia.Nombre,
    clasificacion_emergencia.nombre_clas 
FROM 
    tipo_emergencia,
    clasificacion_emergencia 
WHERE
    tipo_emergencia.id_clasificacion=clasificacion_emergencia.id_clasificacion
"));

$nroLotes = 10;
$nroPaginas = ceil($nroProductos/$nroLotes);
$lista = '';
$tabla = '';

if($paginaActual > 1){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual-1).');">Anterior</a></li>';
}
for($i=1; $i<=$nroPaginas; $i++){
    if($i == $paginaActual){
        $lista = $lista.'<li class=""><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }else{
        $lista = $lista.'<li><a href="javascript:pagination('.$i.');">'.$i.'</a></li>';
    }
}
if($paginaActual < $nroPaginas){
    $lista = $lista.'<li><a href="javascript:pagination('.($paginaActual+1).');">Siguiente</a></li>';
}

if($paginaActual <= 1){
    $limit = 0;
}else{
    $limit = $nroLotes*($paginaActual-1);
}
/* Aquí modificamos por la consulta deseada */
$registro = mysql_query("
SELECT 
    Tipo_emergencia.Id_Tipo, 
    Tipo_emergencia.Nombre,
    clasificacion_emergencia.nombre_clas 
FROM 
    tipo_emergencia,
    clasificacion_emergencia 
WHERE
    tipo_emergencia.id_clasificacion=clasificacion_emergencia.id_clasificacion 
LIMIT 
    $limit, $nroLotes
");

/* Añades las columnas nuevas a la tabla */
$tabla .= '
    <table class="table table-bordered table-inverse">
    <tr class="bg-primary">
        <th width="300">Id_Tipo</th>
        <th width="300">Emergencia</th>
        <th width="300">Nombre</th>
        <th width="50">Opciones</th>
    </tr>';

while($registro2 = mysql_fetch_array($registro)){
    /* Aquí añades los datos que se mostrarán para cada fila */
    $tabla .='
    <tr>
        <td>'.$registro2['Id_Tipo'].'</td>
        <td>'.$registro2['Nombre'].'</td>
        <td>'.$registro2['nombre_clas'].'</td>
        <td>
            <a href="javascript:editarProducto('.$registro2['id_tipo'].');"  class="glyphicon-pencil"></a>
            <a href="javascript:eliminarProducto('.$registro2['id_tipo'].');" class="glyphicon glyphicon-trash"></a>
        </td>
    </tr>';       
}


$tabla .= '</table>';
$array = array(0 => $tabla,
               1 => $lista);
echo json_encode($array);
?>

Obviously there are things that should be changed, for example:

  • mysql_ * is obsolete, instead you should use mysqli_ *, info

  • In the first query mysql_num_rows () should be avoided, since you bring all the records and then count them and nothing else, they are not used. Instead you should create a query that returns the total number of records directly.

  • You can concatenate directly with . = instead of doing $lista = $lista.'más texto';

  • include is a language constructor and parentheses are dispensable.

  • You should validate and escape the data properly, to prevent sql injection regardless of whether you use mysql_ * or mysqli_ *, in this particular case you only receive a variable $_POST['partida']; you should make sure that it is always a number to avoid the injection and if not, avoid the execution of the query.

answered by 16.04.2017 в 18:10