Warning "Undefined variable: row in ..."

1

I have tried to follow a tutorial and adapt it to the need to autocomplete a form by means of an input (with Send button).

I get the error:

  

Notice: Undefined variable: row in C: \ xampp ... \ patientsearch.php on line 61.

It also shows me the error of "Unknown column '...' in 'where clause." I show you the code (2 files):

patientsearch.php

 <?php 
if (isset($_GET['action'])) { 
include_once('connections/connect.php');
$strsql = "SELECT codigo_pac, nombre, apellido_paterno FROM paciente WHERE codigo_pac=".$_GET['codigo_pac'];
$rs = mysqli_query($conn,$strsql) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($rs);
$total_rows = mysqli_num_rows($rs);    

} 
?>


<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="pacientebusqueda.css">
<link href="css/jqueryui.css" type="text/css" rel="stylesheet"/>
<title>Advanced Search Form</title>




</head>

<body >
 <form>

<div class="form-group"  >
            <label>Ingrese el Código del Paciente</label>

            <input type="text" id="codigo_pac" value="<?php echo($row['codigo_pac']); ?>"" name ="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>Nombres</label>
            <input type="text" class="form-control" id="nombre" name ="nombre" value="<?php echo($row['nombre']); ?>">
        </div>
        <div class="form-group">
            <label>Apellido Paterno</label>
            <input type="text" class="form-control" id="apat" name ="apat" value="<?php echo($row['apellido_paterno']); ?>">
        </div>
</div>

<?php include_once('connections/connect.php');?>
<?php
$strsql = "SELECT codigo_pac,nombre,apellido_paterno FROM paciente ";
$rs = mysqli_query($conn,$strsql) or die(mysql_error($conn));
$row = mysqli_fetch_assoc($rs);
$total_rows = mysqli_num_rows($rs);
//print_r($row);
?>
<table width="800" border="0" cellspacing="0" cellpadding="2">
<tr>
    <td>Codigo</td>
    <td>Nombre</td>
    <td>Apellido Paterno</td>
  </tr>

 <?php if ($total_rows > 0) {
    do {
 ?>
 <tr>
    <td><?php echo($row['codigo_pac']); ?></td>
    <td><?php echo($row['nombre']); ?></td>
    <td><?php echo($row['apellido_paterno']); ?></td>
 </tr>
 <?php
    } while ( $row = mysqli_fetch_assoc($rs) );
    mysqli_free_result($rs);
 } else {
 ?>
<tr>
    <td colspan="3">No data found.</td>
</tr>

 <?php } ?>
 </table>

connect.php

<?php
$hostname_strcn = "localhost";
$database_strcn = "...";
$username_strcn = "root";
$password_strcn = "*****";
$conn = mysqli_connect($hostname_strcn, $username_strcn, $password_strcn) or 
die(mysqli_error($conn));
mysqli_select_db($conn, $database_strcn) or die(mysqli_error($conn));
?>

My database consists of a "patient" table (id, code_pac, name, parent_name).

    
asked by KVN20 29.05.2018 в 06:16
source

2 answers

1

Let's go in parts.

The message "Notice: Undefined variable: row in C: \ xampp ... \ patientsearch.php on line 61." it appears because you are using a variable called $row when it is not defined.

When does that happen? If we see your code you have:

<?php
if (isset($_GET['action'])) {
    include_once('connections/connect.php');
    $strsql = "
      SELECT
        codigo_pac,
        nombre,
        apellido_paterno
      FROM paciente
      WHERE
        codigo_pac = '" . mysqli_real_escape_string($conn, $_GET['codigo_pac']) . "'
    ";
    $rs = mysqli_query($conn,$strsql) or die(mysqli_error($conn));
    $row = mysqli_fetch_assoc($rs);
    $total_rows = mysqli_num_rows($rs);
}

You define the value of $row only if you have sent the parameter action for GET , but when you load the page without sending it you use this variable here:

<input type="text" id="codigo_pac" value="<?php echo($row['codigo_pac']); ?>"" name ="codigo_pac" name ="codigo_pac">

Regardless of whether the HTML tag is malformed by not closing the quotes correctly or by not using htmlspecialchars , $row may not exist and you have not taken it into account.

Now let's go for the message of the non-existent column (due to not having correctly formed the label).

You only have a WHERE in your code, so this is the only plausible explanation for the problem:

When you submit the form, the form fields are not well assembled because they contain a double quote and because there is an unnecessary space between the name of the attribute and its content:

<input type="text" id="codigo_pac" value="<?php echo($row['codigo_pac']); ?>"" name ="codigo_pac" name ="codigo_pac">

It should be:

<input type="text" id="codigo_pac"
  value="<?= htmlspecialchars(isset($row['codigo_pac'])?$row['codigo_pac']:'') ?>"
  name="codigo_pac" />

Notice that I have changed name ="codigo_pac" by name="codigo_pac" , I have removed the duplicate, and I have used htmlspecialchars() to show your content in an HTML document.

In addition, isset($row['codigo_pac'])?$row['codigo_pac']:'' allows you to generate an empty string if you have not been sent by GET the parameter codigo_pac (and thus avoid the above warning message).

The message is because you are concatenating the string in the SQL:

"WHERE codigo_pac=".$_GET['codigo_pac']

And since you did not mount the HTML well, this field is not sent as GET , and you must have also released a warning message saying that the index does not exist and the query remains:

WHERE codigo_pac=

Generally I should give you the following error message:

  

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    
answered by 29.05.2018 / 09:31
source
0

I think your problem is in this line

<input type="text" id="codigo_pac" value="<?php echo($row['codigo_pac']); ?>"" name ="codigo_pac" name ="codigo_pac">

Notice that after closing the php you have two double quotes. That makes "name = ......" is no longer interpreted as html attributes, but as a literal text in php.

    
answered by 29.05.2018 в 09:12