Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is the php code for searching user entered value from table and printing it.

header.php

PHP
 //html code
<form>
 <select name="select">
<option value="empty">none</option>
<option value="fiction">fiction</option>
<option value="journals">journals</option>
<option value="acadamic">acadamic</option>
</select>


            <form action="" method="POST">
 
           <input type="text" value="Enter a keyword here..." name="q" size="10" id="searchfield" title="searchfield" onFocus="clearText(this)" onBlur="clearText(this)" />
                    <input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" />


            </form></form>
// php code
<?php

if (isset($_GET['q']) && $_GET['q'] != "")
{
if($_GET['select'] == "fiction"){

  
   $result=$_GET['q'];// storing user entered value
   //echo $a;
   $sqlcommand= "SELECT title,author FROM ".USRBOOKS." WHERE fiction = '$result'";

}else if($_GET['select'] == "journals"){
$result=$_GET['q'];
	 $sqlcommand= "SELECT title,author FROM ".USRBOOKS." WHERE fiction = '$result'";

}

else if($_GET['select'] == "acadamic"){
$result=$_GET['q'];
 $sqlcommand= "SELECT title,author FROM ".USRBOOKS." WHERE fiction = '$result'";

}
 

$query = mysql_query($sqlcommand) or die($sqlcommand);
echo "hello world";
$count = mysql_num_rows($query);

if ($count>0)
{
global $searchoutput;
$searchoutput = " $count results ";
echo '<META http-equiv="refresh" content="0;URL='.INDEX_URL.'result.php">';// trying to print result in new php file
}
}

else {
 $searchoutput="0 result";
echo '<META http-equiv="refresh" content="0;URL='.INDEX_URL.'resultn.php">';

}

?>


when ever I am trying to search the value, its is printing select query.
if I entered value xyz (assume it is present in data base table in fiction column)
instead of storing resource into $query (by using $mysql_query) it is printing below value

SELECT title,author FROM books WHERE fiction = 'xyz'

please anyone solve my problem
Posted
Comments
nice_hand23 8-Feb-15 5:49am    
How does look your code to initialise your db?

btw: mysql_query() is deprecated as of PHP 5.5.0, according to php-manual[^], use mysqli_query instead.
Another task: do not use user inputs directly. Search for 'SQL injection'.

1 solution

You need to initialise your db first.
Look at: php-manual[^]
When you do it as described put your mysql_query()-call before closing the connection.

btw: put your db-code and the redirect in your 'if (isset($_GET['q']) && $_GET['q'] != "")'-clause.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900