Fill table with Select mysql

0

Good afternoon, I need to fill a datatable of jquery with the data brought from a select with the filter of a parameter, select the filter from a checkbox and pressing a button displays the data in the table The query works, but when showing the data does not work, some example of how to do it? I am developing in php + mysql + mvc pattern, attached code with query

Inquiry

function filtroMed($rut)
{
    global $gbd;
    $_SESSION['con'] = $rut;
    $sql = "SELECT * FROM atencion WHERE rutMedico ='".$rut."'";
    $res=$gbd->query($sql);
    if($res)
    {
        $row=$res->fetchAll(PDO::FETCH_ASSOC);
        return $row;
    }
    else {
        return false;
    }
}

Controller

elseif($_POST['tipo']=='medico')
{
    if($medico->filtroMed($_POST['rut']))
    {
        header('Location: http://localhost/dashboard/Cesfam/Vista/mostrarEstadistica.php?med=si');
        exit;
    }
    else
    {
        header('Location: http://localhost/dashboard/Cesfam/Vista/mostrarEstadistica.php?med=no');
        exit;
    }
}
    
asked by Claudio Scabbia Sepúlveda 06.07.2017 в 20:51
source

2 answers

1

First of all, your query has security problems, try to use something like that

$sth = $dbh->prepare('
  SELECT
    *
  FROM atencion 
  WHERE
    rutMedico= :rut
  LIMIT 1
  ');

$sth->bindParam(':ruta', $ruta);

$sth->execute();

$records= $sth->fetch(PDO::FETCH_OBJ);
    
answered by 06.07.2017 в 21:18
1

I really do not understand how you are operating, you must understand that by using the header("Location: ...") you are sending the user the necessary information in the HTTP header so that your browser knows where to point, in other words the client ( browser) who redirects taking into account the information you provided through the header:

  

link

     
    

... header () is used to send raw HTTP headers.

         

"Location:" Not only sends the header to the browser, but also returns the status code (302) REDIRECT to the browser unless the status code 201 or 3xx has already been sent.

  

Knowing the above, then you can deduce that it is not equivalent to include the file, as you would with require or include

Assuming you have these two files:

- principal.php

<?php 
$miVariable = "hola mundo";
header("archivo-final.php");

- final-file.php

<?php
echo $miVariable;

You will notice that when executing the file principal.php you finally end up also executing the archivo-final.php with the following error message is:

  

Notice: Undefined variable: myVariable in final-file.php on line ...

This is because what is really happening, is not that you call your file archivo-final.php from principal.php , but you have told the user that once you have the server's response regarding the query principal.php direct to archivo-final.php , it's as if the user himself wrote the URL of archivo-final.php directly in the navigation bar ( two independent queries ). Now if the above is clear and right to think that you are trying to use the variable $datos in the file mostrarEstadistica.php , that would be your problem, it would be enough to edit the header for a require or include , as well :

elseif($_POST['tipo']=='medico')
{
    if($medico->filtroMed($_POST['rut']))
    {
        $_GET['med'] = 'si';
    }
    else
    {
        $_GET['med'] = 'si';
    }
    require('__RUTA_VISTA__/mostrarEstadistica.php');
}

Edit the code slightly, since regardless of whether the filter is fulfilled or not, you always execute the line that calls to the view and the only thing that changes is the attribute $_GET that you send to it. Therefore, it is not worthwhile to include it in the conditional.

To consider:

  • __RUTA_VISTA__ is the relative or absolute path of the directory that stores to show Statistics.php.
  • I do not think it's optimal to directly use the global variables $_POST and $_GET in fact the use $_GET is unnecessary in this case, but I try to maintain the current structure. Regarding access to global variables you can use filter_input (...) but it is another theme.
answered by 06.07.2017 в 21:33