Problem when using variable in PDO query

0

I have a web in which I want to pass a variable from one page to another but I only get to recognize the database ID, when I change the parameter it does not show me anything. I pass the code that works for me and also the code that gives me problems.

This code works:

<a href="visualizar-informe-alta.php?id=<?php echo htmlentities($result->id);?>"><img src="images/ver-paciente.png" alt="Ver Informe" height="25" width="25"   title="Visualizar Informe"></a>

<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])=="")
    {   
    header("Location: index.php"); 
    }
    else{

$id=intval($_GET['id']);

?>


<?php 

$sql = "SELECT 'id', 'fecha_alta', 'cip_paciente', 'nombre', 'apellidos', 'edad', 'sexo', 'servicio', 'modalidad', 'up_que_consulta', 'cod_grupo_patologia', 'cod_cie9', 'dolor', 'fuerza', 'movilidad', 'dependencia', 'otros_parametros', 'motivo_alta' FROM 'informe_de_alta' WHERE id=:id";
$query = $dbh->prepare($sql);
$query->bindParam(':id',$id,PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
foreach($results as $result)

?>

This code is the one that does not work for me:

<a href="visualizar-informe-alta.php?cip_paciente=<?php echo htmlentities($result->cip_paciente);?>"><img src="images/ver-paciente.png" alt="Ver Informe" height="25" width="25"   title="Visualizar Informe"></a>

<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])=="")
    {   
    header("Location: index.php"); 
    }
    else{

$cip_paciente=intval($_GET['cip_paciente']);

?>


<?php 

$sql = "SELECT 'id', 'fecha_alta', 'cip_paciente', 'nombre', 'apellidos', 'edad', 'sexo', 'servicio', 'modalidad', 'up_que_consulta', 'cod_grupo_patologia', 'cod_cie9', 'dolor', 'fuerza', 'movilidad', 'dependencia', 'otros_parametros', 'motivo_alta' FROM 'informe_de_alta' WHERE cip_paciente=:cip_paciente";
$query = $dbh->prepare($sql);
$query->bindParam(':cip_paciente',$cip_paciente,PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
foreach($results as $result)

?>

What could it be?

    
asked by Yandry Hernández 05.03.2018 в 15:07
source

1 answer

-1

in the part of the query where you are doing the WHERE but not send them with the two points pq do not send them with the question, since you are sending parameters, it would be something more or less like this

$sql = "SELECT 'id', 'fecha_alta', 'cip_paciente', 'nombre', 'apellidos', 'edad', 'sexo', 'servicio', 'modalidad', 'up_que_consulta', 'cod_grupo_patologia', 'cod_cie9', 'dolor', 'fuerza', 'movilidad', 'dependencia', 'otros_parametros', 'motivo_alta' FROM 'informe_de_alta' WHERE id=?";

This would be the first query the second would be like this

$sql = "SELECT 'id', 'fecha_alta', 'cip_paciente', 'nombre', 'apellidos', 'edad', 'sexo', 'servicio', 'modalidad', 'up_que_consulta', 'cod_grupo_patologia', 'cod_cie9', 'dolor', 'fuerza', 'movilidad', 'dependencia', 'otros_parametros', 'motivo_alta' FROM 'informe_de_alta' WHERE cip_paciente=?";

It is advisable that you do an echo before sending them to the view to see if it brings you the result.

    
answered by 05.03.2018 в 15:21