Search bar with html/php/sql isn\'t returning any records. Arts, Electronics, Tr
ID: 3827836 • Letter: S
Question
Search bar with html/php/sql isn't returning any records. Arts, Electronics, Travel, Food and Wine are in the table. I typed each into th search bar and none came back and it said no records returned. My code is below.
Category Search Look-Up
<form method="get" action="CategorySearch.php">
Please enter the range of categories you would like:
<input type="text" name="Cat"><br/>
<input type="submit">
</form>
<?php
$Cat = htmlentities($_GET["Cat"]);
echo "Trying to return records with category". $Cat .".<br/>";
echo "Connecting to database server.<br />";
try {
//variable stores the connection -> $conn
//PDO is a php data object -> helps prevent SQL injection
//host = Database server host name
//username = name of READ ONLY user
//password = that user's password
$conn = new PDO("mysql:host=Database Login);
} catch(PDOException $e) { //this should tell us if there was a connection problem
echo "Error connecting to server: " . $e->getMessage();
die;
}
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection to server succeeded.<br />";
echo "Connecting to database Category...<br />";
try {
//if executing a query, NO USER ENTERED fields should be in query string!
$conn->query("USE Database Table;");
} catch (PDOException $e) {
echo "Error connecting to database: " . $e->getMessage();
die;
}
echo "Connection to recipes database succeeded.<br />";
//SQL statement WILL have any user-entered data, so BIND needed
$SQL = "SELECT ID, Cat, Description FROM Category";
$SQL .= " WHERE Cat;";
/*$SQL.=" ORDER BY Cat ASC;";
/* WHERE Cat BETWEEN >= :BegCat AND Cat <= :EndCat;";
*/
try {
$sth = $conn->prepare($SQL);
$sth->bindParam(":Cat", $Cat);
$sth->execute();
} catch (PDOException $e) {
echo "Error selecting category records: " . $e->getMessage();
die;
}
echo "Query executed successfully. <br />";
//are there records in the set?
if($sth->rowCount()==0) {
echo "No records returned.<br />";
die;
} else {
echo $sth->rowCount() . " records returned.<br />";
}
//$result is an array that holds the dataset
while($result = $sth->fetch()) {
//in an array, refer to column with string inside brackets ['rid']
echo "Category ID: " . $result['ID'] . "<br />";
echo "Category Name: " . $result['Cat'] . "<br />";
echo "Description: " . $result['Description'] . "<br />";
/*put lots link here*/
echo "<a href="DeleteCategory.php?ID=".$result["ID']."'>Delete Category ID ".$result['ID']."</a><br />";
echo "<a href="UpdateCategory.php?ID=".$result["ID']."'>Update Category ID ".$result['ID']."</a><br />";
}
?>
Explanation / Answer
$SQL = "SELECT ID, Cat, Description FROM Category";
$SQL .= " WHERE Cat;";
you can retype this statement in this format
$sql="SELECT ID, Cat, Description FROM Category".
" WHERE Cat;";
this format is correct and your program run properly.
I have mentioned sample program that clear the syntax of program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.