Hi .
I need help with the code below. I 'm using Php 4.0.1 and running on windows 2000.
search2.php is where the user will key in author name, title, or type and the results will be displayed in results.php
The problem is that I have added in two items in the mysql database and when i key in part of the title e.g. 'Professional', I do not see any result and get the ' no matches available ' message.
//file: search2.php
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<h2>Online Library -Search</h2>
<form action="results.php" method=GET>
Query:<input name="query" type="text"/><br/>
Series:<select name="series">
<?php
//open connection
$conn = mysql_connect('localhost','jon','secret') or die(mysql_error());
mysql_select_db('library',$conn) or die(mysql_error());
//query database
$sql = "SELECT series_ID,book_series FROM series";
$result = mysql_query($sql,$conn);
if($result && (mysql_num_rows($result)>0)) {
while($row = mysql_fetch_assoc($result)) {
$option = sprintf('<option value = "%d">%s</option>',$row['series_ID'],$row['book_series']);
echo("$option\n");
}
} else {
echo("<option>No Series available</option>\n");
}
mysql_close($conn);
?>
The code below is suppose to display the results of the search when the user keys in either title, series or author name and display the title, author and price of the book.
The tables in the library database have been filled with the appropriate data. I've already run the sql statement in $sql in mysql command monitor and the query displayed the result i expected.
//Destination: results.php
<html>
<head>
<title> Online Library Results</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<h2> Online Library-Results</h2>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
</tr>
<?php
$conn = mysql_connect('localhost','jon','secret') or die(mysql_error());
mysql_select_db('library',$conn) or die(mysql_error());
$query = addslashes($HTTP_GET_VARS['query']);
$series = $HTTP_GET_VARS['series'];
$type= $HTTP_GET_VARS['type'];
//query database
$sql ="SELECT book_title,auth_name,price".
"FROM title, details,author,authortitle,series".
"WHERE author.auth_ID = authortitle.auth_ID AND ".
"authortitle.ISBN = title.ISBN AND title.ISBN = details.ISBN ".
"AND details.series_ID = series.series_ID";
//add search terms to query
if(!empty($series)) {
$sql .=" AND series.series_ID = $series";
}
if(!empty($query)&& !empty($type)) {
if($type =='ISBN') {
$sql .=" AND details.ISBN = '$query'";
}
elseif($type =='author') {
$sql .= "AND author.auth_name LIKE '%query%'";
}
elseif($type =='title') {
$sql .= "AND title.book_title LIKE '%query%'";
}
}
$result = mysql_query($sql,$conn);
//print the <option> rows for <select> widget
if($result &&(mysql_num_rows($result)> 0)) {
while($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><u><?php echo(htmlspecialchars($row['book_title'])); ?></u></td>
<td><?php echo(htmlspecialchars($row['auth_name'])); ?></td>
<td><?php echo(htmlspecialchars($row['price'])); ?></u></td>
</tr>
<?php
}
}else {
echo("<tr><td colspan=\"3\">No matches found.</td></tr>\n");
}
mysql_close($conn);
?>
</table>
<br>
<a href="search2.php">Search Again</a>
</body>
</html>
Thanks for taking the time to help me out.