Hi PHPFreaks!
I have a question for you. Anyone's help would be highly appreciated.......
I am writing a database search page in PHP but its not displaying any results. It connects to database and even executes the query but no results are being displayed on the webpage.
Let me explain:
This page has a form with a drop-down menu item, a text input element, and a ‘Search’ button. The dropdown menu item contains the list of search conditions e.g., CD type, CD artist,
Album name, CD price, etc. and the text input element is used to enter details of the
search condition. For example, if the ‘artist’ is selected from the drop-down list then type
in the artist’s name in the text input element e.g., ‘Beatles’. Click on the ‘Search’ button
to start the search.
But its not displaying and results after query is executed; here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<link rel="stylesheet" type="text/css" href="music.css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Waqas" />
<meta name="description" content="Online Music CD Store" />
<meta name="keywords" content="php" />
<title>Online Music CD Store ::: Home</title>
</head>
<body class="center">
<h1>Online Music CD Store</h1>
<p class="center">
Administrator <br/>
Search Items Page<br/>
</p>
<form action = "search.php" method="post">
<input type="hidden" name="posted" value=1 />
<p>
<select name="select">
<option selected="selected">Select</option>
<option value="cd_artist">CD Artist</option>
<option value="cd_type">CD Type</option>
<option value="cd_name">Album name</option>
<option value="cd_price">CD Price</option>
</select>
<input name="query" type="text" />
</p>
<p>
<input name="submit" type="submit" value="Search" /><a href="admin.html"><input name="adminhome" type="submit" value="Admin Home" /></a> </p>
</form>
<?php
require_once 'include/config.php';
$Table = $select= $text = " ";
if (isset($_POST['query']) && isset($_POST['select'])) {
//if(count($_POST)>0) {
$select = $_POST['select'];
$text = $_POST['query'];
// connect server
$DBConnect = @mysqli_connect($Host, $User, $Password)
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error() . "</p>"
. "<p>if statement with error no. catchment required here rather than this message </p>");
@mysqli_select_db($DBConnect,$DBName)
Or die("<p>The database is not available</p>");
//STEP 2 manipulate data
$srch="%".$text."%";
$SQLstring ="SELECT * FROM $Table1 WHERE $select LIKE' $srch'";
$QueryResult =@mysqli_query($DBConnect, $SQLstring)
Or die("<p> Unable to show enquiry</p>"
."<P>Error code " . mysqli_errno($DBConnect)
.":".mysqli_error($DBConnect))."</p>";
// echo $QueryResult;
//---------------------------------------------------------------
// output to table using fetchrow
//---------------------------------------------------------------
// if($QueryResult) {
echo "<hr />";
echo "<p>Search Results</p>";
echo "<hr />";
$Row = mysqli_fetch_row($QueryResult);
do {
echo "CD Type: {$Row['cd_type']}"." Artist: {$Row['cd_artist']}<br />";
echo "Album Name: {$Row['cd_name']}<br />";
echo "Price: <td>{$Row['cd_price']}<br />";
$Row = mysqli_fetch_row($QueryResult);
}while ($Row);
//----------------------------------------------------------------
//STEP 3 CLOSE
//----------------------------------------------------------------
mysqli_close($DBConnect);
} else{
echo "<p>No Data to Display, Please Try again!!!</p>";
}
// }
?>
</body>
</html>