用户进入我的网页,进入类别范围,它应该返回表中的所有类别,但事实并非如此。我为他们创建了一个页面,以便将记录添加到数据库中的表中并且它可以正常工作。所以现在我需要用户转到搜索页面,并能够查找添加到数据库并显示它们的类别。每当我尝试搜索类别时,一切都运行良好,但它说“没有记录返回”。
Category Search Look-Up
<form method="get" action="NameOfFileHere.php">
Please enter the beginning category:
<input type="text" name="BegCat"><br/>
Enter ending category
<input type="text" name="EndCat"><br/>
<input type="submit">
</form>
<?php
$BegCat = htmlentities($_GET["BegCat"]);
$EndCat = htmlentities($_GET["EndCat"]);
echo "Trying to return records with category between ". $BegCat ." and ". $EndCat .".<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 Info Here");
} 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 Name Here;");
} 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 >= :BegCat AND Cat <= :EndCat;";
try {
$sth = $conn->prepare($SQL);
$sth->bindParam(":BegCat", $BegCat);
$sth->bindParam(":EndCat", $EndCat);
$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='DeleteFileHere.php?ID=".$result['ID']."'>Delete Category ID ".$result['ID']."</a><br />";
echo "<a href='UpdateFileHere.php?ID=".$result['ID']."'>Update Category ID ".$result['ID']."</a><br />";
}
?>
数据库表的图片 eclipse classes