-4

i'm trying to check if a value inserted into a taxtbox is different from a value listed on a database.

for now i have this code:

<?php
include('connection.php');
if($_POST)
{
$q=$_POST['search'];
$sql_res=mysql_query("select name, surname, test, from table where name like '%$q%' or surname like '%$q%' order by field (test,1,0)");
while($row=mysql_fetch_array($sql_res))
{
$name=$row['name'];
$surname=$row['surname'];
$test=$row['test'];

$b_name='<strong>'.$q.'</strong>';
$b_surname='<strong>'.$q.'</strong>';
$final_name = str_ireplace($q, $b_name, $surname);
$final_surname = str_ireplace($q, $b_name, $surname);
                
$display = '<div> HTML code here</div>';
    
?>

<?php echo $display?>

    
<?php

}
}
    if($final_name != $name){$display = "no";}

?>

and using this code, if the value exist everything works fine, otherwise i have this error:

Notice: Undefined variable: name in C:\xampp\htdocs\A\search.php on line 44

Notice: Undefined variable: final_name in C:\xampp\htdocs\A\search.php on line 44

any help?

EDIT:

Ok, i think that maybe the problem is the live search.. i mean, with your help i can show a message if the $q != $name but the problem is that this message is always visible at the bottom of the results, and as long as there's no more match, the results disappears and the message is above everything.

I think that maybe just because the search is live, the textbox value is never equal to the database one.

Is it correct?

Community
  • 1
  • 1
  • Variables name, final_name and display are outside the while loop, where they were defined. – user1587985 May 29 '14 at 07:31
  • `$name` only exists if your query didn't fail. `$final_name` is not defined anywhere in the code excerpt posted. – bcmcfc May 29 '14 at 07:32
  • Please be aware that the mysql extension (supplying the mysql_ functions) has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 29 '14 at 12:20

1 Answers1

0

try

$check = mysql_num_rows($sql_res);
if($check > 0) {
  // your while loop and other code
}

Note :- mysql_* has been deprecated use mysqli_* or PDO

or try to check empty() or isset() for check values exist

$name=(!empty($row['name']) ? $row['name'] : '');
// and also for other vars same process
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44