Please bear with me, as I am new at PHP. I want my users to be able to retrieve data from my table when they enter a search term (e.g. author’s name). However, I am having two problems with this:
When I type a particular author’s name and click “submit”, I get a listing of all the authors I have put in the table, instead of only the listing which relate to the name I typed in the search field.
Also, for each author, there is a hyperlink to show detailed information about the author’s books. But, when I click on the link, I get the following error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lhsnycne/public_html/library/novels2/detail.php on line 22
Here are the scripts I am using:
Filename: Form
<html>
<head>
<title>Search by Last Name</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div style="border:1px maroon dotted; float:left;">
<form action="search02.php" method="post" name="form">
Enter Last Name to Search: <input type="text" name="search_lname" size="17"><br>
<input type="submit" name="submit" value="submit">
</form>
</div>
</body>
</html>
=============================================
Filename: Search02.php
<?php
include("connection.inc");
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$rs = mysql_select_db("mydatabase",$conn) or die("error connecting to the database");
#using the wildcard (%) character in the last half of the statement allows users to search with only the first few letters of the name
#another wildcard could be placed between the first ' and " to allow total matching searching for letters between the first and last letters
$sql = "SELECT id,fname,lname FROM novels2 WHERE lname LIKE '" . $search_lname ."%';";
echo ($sql) . "<br>";
$rs = mysql_query($sql, $conn);
$total_results = mysql_num_rows($rs);
?>
<h3>Here are the results...</h3>
<?php
if ($total_results > 1)
{
echo ("<p>You have found $total_results Records</p>");
}
if ($total_results == 0)
{
echo ("<p>There are no records matching that name</p>");
}
if ($total_results == 1)
{
echo ("<p>You have found $total_results Record</p>");
}
?>
<?php
$names=1;
while ($row = mysql_fetch_array($rs))
{
echo ("<p>$names. " . $row["fname"] . " " . $row["lname"] . "<br>");
$names=$names+1;
echo ("<a href='detail.php?id=" . $row["id"] . "'> See Details</a></p>");
}
?>
</body>
</html>
============================================
Filename: Detail.php
<?php
include("connection.inc");
?>
<html>
<head>
<title>Detail Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$rs = @mysql_select_db("mydatabase",$conn) or die("error connecting to the database");
#using the wildcard (%) character in the last half of the statement allows users to search with only the first few letters of the name
#another wildcard is placed between the first ' and " to allow total matching searching
$sql = "SELECT * FROM novels2 WHERE id = " .$id ;
$rs = mysql_query($sql, $conn);
?>
<h3>Here are the details....</h3>
<?php
$row = mysql_fetch_array($rs);
echo("<p>Name: " . $row["fname"] . " " . $row["lname"] . "<br>");
echo("Title: " . $row["title"] . "<br>");
echo("Genre: " . $row["genre"] . "<br>");
echo("Information: " . $row["info"] . "<br>");
?>
<a href=<?php echo ($row["url_1"]);?> target="_blank"><?php echo ($row["url_1_description"] . "<br>"); ?></a>
<?php
echo("Year Published: " .$row["year"] . "<br>");
echo("Date Added: " .$row["dte"] . "</p>");
?>
</body>
</html>
Thank you in advance.