Jump to content

[SOLVED] Displaying Results on Same Page in PHP & MySQL


waqaslone

Recommended Posts

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>

 

 

Link to comment
Share on other sites

First step would be to remove the error suppressors "@" symbols.

They might be hiding something.

 

Error-reporting is on?

 

The printr($_POST)  and echo the $text search value to confirm what you are searching for.

Could also check the num_rows after the Query and echo that.

Link to comment
Share on other sites

@jlhaslip:

yes error reporting is on, i did echo the values of "$text" and "$select". They are printing absolutely fine what i want. I did put the following code after the query execution code.

 

$NumRows =mysqli_num_rows($QueryResult);
			$NumFields =mysqli_num_fields($QueryResult);
			if ($NumRows !=0 && $NumFields !=0)
				echo "<p> your query returned " .	$NumRows." rows and ". $NumFields . "fields .</p>";		

 

output:

 

your query returned 1 rows and 6fields .

 

 

@BobCatM:

well there are actually two tables "Table1" & "Table2", so Table1 in the query is fine.

 

 

Link to comment
Share on other sites

oh i got this problem solved: the problem was in query there was a blank space in quotes after "LIKE" keyword causing not to display the results:

 

$SQLstring ="SELECT * FROM $Table1 WHERE $select LIKE' $srch'";

 

Thanks everyone for their help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.