Notice: Undefined variable: row in C: \ xampp / ... / ... index.php on line

0

I am trying to make an autocomplete form using an input (text) ... I used this way but I do not know if it will be fine ... I leave the code: (I have a patient table (id, code_pac, name, surname_paterno )

CONNECT.PHP

<?php
$hostname_strcn = "localhost";
$database_strcn = "basededatos";
$username_strcn = "usuario";
$password_strcn = "";
mysqli_connect($hostname_strcn, $username_strcn, $password_strcn) or 
die(mysqli_error());
mysqli_select_db($database_strcn) or die(mysqli_error());
?>

INDEX.PHP

<?php 
if (isset($_GET['action'])) { 

include_once('connect.php');

$strsql = "SELECT * FROM paciente WHERE codigo_pac=".$_GET['codigo_pac'];
$rs = mysqli_query($strsql) or die(mysqli_error());
$row = mysqli_fetch_assoc($rs);
$total_rows = mysqli_num_rows($rs);    

} 

?>

<div>
    <div class="form-group">
            <label for="first-name">Ingrese el Código del Paciente</label>
            <input type="text" class="form-control" id="codigo_pac" value="<?php echo($row['codigo_pac']);?>" name ="codigo_pac">
            <input type="hidden" id="action" name="action" value="sent">
            <input type="submit" id="btn_submit" value="Enviar">
        </div>

        </div>
<div class="form-group">
            <label for="first-name">Nombres</label>
            <input type="text" class="form-control" id="nom" name ="nom" value="<?php echo($row['nombre']); ?>">
        </div>
        <div class="form-group">
            <label for="last-name">Apellido Paterno</label>
            <input type="text" class="form-control" id="apat" name ="apat" value="<?php echo($row['apellido_paterno']); ?>">
        </div>

AT THE TIME OF TESTING ... I get "Notice: Undefined variable: row in C: \ xampp \ htdocs \ cnsbol \ patientsearch.php on line" ... both in the code input and the two inputs of ( first and last name) ... I would appreciate your help please ... (THE IDEA IS TO MAKE A SELF-COMPLETED FORM) ... thanks

    
asked by KVN20 26.05.2018 в 08:13
source

1 answer

2

The first thing is that, you are not using the mysqli_select_db() function since you do not have a dynamic DB change, then you can pass the mysqli_connect() as the fourth value the base.

Second, you are opening a connection but you are never storing it to use it. That's why you do not get a connection error but mysqli_query() requires a connection and a query to be successful since you are using the procedural mode and not object-oriented connection. View mysqli_query () for more information.

<?php
$hostname_strcn = "localhost";
$database_strcn = "basededatos";
$username_strcn = "usuario";
$password_strcn = "";
$conn = mysqli_connect($hostname_strcn, $username_strcn, $password_strcn, $database_strcn ) or 
die(mysqli_error());
?>

Then your code would be like this (I did a couple of validations also take into account the comments about the SQLInjection)

<?php 
if (isset($_GET['action'])) { 

include_once('connect.php');

//Consulta
$strsql = "SELECT * FROM paciente WHERE codigo_pac=".$_GET['codigo_pac'];

//Validar si la consulta está bien hecha al igual que la conexión
if( $rs = mysqli_query($conn, $strsql)){
    //Validar que la consulta haya retornado valores
    if( mysqli_num_rows($rs) > 0){
      //Sacar información
      $total_rows = mysqli_num_rows($rs);
      $row = mysqli_fetch_assoc($rs);
    }

    else{
      echo 'La consulta no retornó resultados';
      //Liberar memoria
      mysqli_free_result($rs);
    }
}

else{
  echo 'La consulta está mal formada o no se abrió la conexión';
}
//Cerrar conexión
mysqli_close($conn);

}

That it serves you

    
answered by 26.05.2018 / 12:49
source