vendredi 6 février 2015

Search box does not recognise variable when false

In my search box the user can search to see if an item is in the database. The code works ok if the item name they have entered matches. The problem is when the item name they entered does not match any item names in the database.



<?php $name = $_GET['search'];
$name = strtolower($name);
$name = ucfirst($name) ;
$dbQuery = $db->prepare("select * from names WHERE Item_Name = '$name'");
$dbQuery->execute();
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)){

$Item_name = $dbRow['Item_Name']; }

if ($Item_name == $name) {
echo "Name is in database";
} else {
echo "Name is not in database";
}

?>


When $Item_name matches $name the message prints successfully. When they don't match it says 'Undefined variable: Item_name'. Im not sure why this is happening. Could someone help?





Aucun commentaire:

Enregistrer un commentaire