caine Posted November 29, 2006 Share Posted November 29, 2006 I want to perform searching queries once user enters searching keywords. However, there is no display returned after that. What will be the problem?[code]<html><head><title>Searching Results</title></head><body><?php $db = mysql_connect("localhost", "root", "xxxx") or die(mysql_error()); mysql_select_db("test", $db) or die(mysql_error()); $display ="<h1>Searching Results</h1>"; //perform searchif($search == $_POST['Search']){ $qry = mysql_query('SELECT * from test where TITLE, DEPARTMENT like "%'.$search.'%" ORDER by DEPARTMENT'); if(mysql_num_rows($qry)< 1) { $display = "<p>Sorry, no matching results.</p>"; } else { while ($results = mysql_fetch_array($qry)) { echo "<table width="100%" border="0">"; echo "<tr>"; echo "<th>Title</th>"; echo "<th>Department</th>"; echo "<th>Link</th>"; echo "</tr>"; $title = $results['title']; $department = $results['department']; $campus = $results['campus']; $link = $results['link']; $display .= "<tr> <td><strong>$title</strong></td> <td>$department</td> <td><a href="$link"></a></td> </tr>"; }//while $display .="</table>"; }//else $qry}//if $searchelse{ echo "No matches.";} ?></body></html>[/code]Parsing error with this part:echo "<table width="100%" border="0">"; echo "<tr>"; echo "<th>Title</th>"; echo "<th>Department</th>"; echo "<th>Link</th>"; echo "</tr>"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 29, 2006 Share Posted November 29, 2006 I think the problem is in your WHERE clause where you are trying to compare to fields simultaneoulsy. Try this"SELECT * FROM test WHERE TITLE like "%'.$search.'%" OR DEPARTMENT like "%'.$search.'%" ORDER by DEPARTMENT" if(mysql_num_rows($qry)< 1) Quote Link to comment Share on other sites More sharing options...
btherl Posted November 29, 2006 Share Posted November 29, 2006 And your parse error is due to using " in a string delimited by ". Try[code=php:0]echo "<table width=\"100%\" border=\"0\">";[/code]Notice that I've added a backslash before every double quote except the first and last. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 29, 2006 Share Posted November 29, 2006 following on from what btherl said, you dont have to use backslahes, you can simply use the ' instead. like this:[code=php:0]echo "<table width='100%' border='0'>";[/code] Quote Link to comment Share on other sites More sharing options...
caine Posted December 5, 2006 Author Share Posted December 5, 2006 I had modified the code into the following, but whatever keywords that I typed, the keywords ,$_POST['Search'] cannot be parsed into the search results page when I debug. Therefore, there's no searching results for the data in db. The error statement is : Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\xampp\htdocs\search_results_new.php on line 19. What is the problem?The form is like this:[code]<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Search</title></head><body> <table width="766" border="1"> <tr> <td width="1530"><h1>Search Bulletin.... </h1></td> </tr> <tr> <td><label> <div align="center">Keyword: <form action="search_results_new.php" method="post"> <input name="textfield" type="text" /> <input type="submit" name="submit" value="Search" /> </form> </div> </label> <label> <div align="center"></div> </label></td> </tr> <tr> <td><a href="homepg.php">Back to main menu.</a></td> </tr></table></body></html>[/code]The search results page is like this:[code]<html><head><title>Searching Results</title></head><body><?php $db = mysql_connect("localhost", "root", "xxxx") or die(mysql_error()); mysql_select_db("bulletin", $db) or die(mysql_error()); $display ="<h1>Searching Results</h1>"; //perform searchif(isset($_POST['Search'])){ $res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE` LIKE '%$_POST['Search']%' OR `DEPARTMENT` LIKE '%$_POST['Search']%' ORDER by DATE", $db); echo "res=".$res; if(mysql_num_rows($reS)>0) { //your table header goes here... echo "<table width=\"100%\" border=\"0\">"; echo "<tr>"; echo "<th>Title</th>"; echo "<th>Department</th>"; echo "<th>Link</th>"; echo "</tr>"; while($row=mysql_fetch_assoc($res)) { $title = $results['title']; $department = $results['department']; $campus = $results['campus']; $link = $results['link']; $display .= "<tr> <td><strong>$title</strong></td> <td>$department</td> <td><a href=$link></a></td> </tr>"; } $display .="</table>"; } }else{ echo "No matches.";} ?></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 5, 2006 Share Posted December 5, 2006 you cannot use $_POST[] variables in your query like this.change this:[code=php:0]$res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE` LIKE '%$_POST['Search']%' OR `DEPARTMENT` LIKE '%$_POST['Search']%' ORDER by DATE", $db);[/code]to this:[code=php:0]$res = mysql_query("SELECT * FROM `bul_data` WHERE `TITLE` LIKE '%{$_POST['Search']}%' OR `DEPARTMENT` LIKE '%{$_POST['Search']}%' ORDER by DATE", $db);[/code]and it should work just dandy. Quote Link to comment Share on other sites More sharing options...
caine Posted December 7, 2006 Author Share Posted December 7, 2006 Thanks ProjectFear, I had corrected the syntax as you said. However, there's still no data returned after entering the keywords. There's no more error statements, instead it shows this only :$query = SELECT * FROM `bul_data` WHERE `TITLE` LIKE '%Submit%' OR `DEPARTMENT` LIKE '%Submit%' ORDER by DATECould there be any other possible problems occurred from the form program? Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 7, 2006 Share Posted December 7, 2006 Don't you want to search by what the user entered into the text field? Right now you are usign the value of the submit button in your query. Try using $_POST['textfield'] Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 7, 2006 Share Posted December 7, 2006 and your echoing out your query, you dont really need to do that. remove this line:[code=php:0]echo "res=".$res;[/code]unless of course your ment to show your query. Quote Link to comment Share on other sites More sharing options...
caine Posted December 7, 2006 Author Share Posted December 7, 2006 Ok, I removed that line. Actually discovered that there's no <?php and ?> at the form program, so I added them in. Besides, I had corrected the syntax as mjdamato said. But there's still no results returned. What are other possibilities for the failure?I think I better postup latest updated program. Here they are:[code]<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Search</title></head><body><?php echo "<h1>Search Bulletin </h1>"; echo "<h3>Keywords:</h3>"; echo "<form action=\"search_results_new.php\" method=\"post\">"; echo "<input name=\"Submit\" type=\"text\">"; echo "<input type=\"Submit\" name=\"Submit\" value=\"Submit\">"; echo "</form>"; echo "<a href=\"homepg.php\">Back to main menu.</a>"; ?></body></html>[/code]The search results page :[code]<html><head><title>Searching Results</title></head><body><?php $db = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("bulletin", $db) or die(mysql_error()); $display ="<h1>Searching Results</h1>"; //perform searchif(isset($_POST[Submit])){ $query = "SELECT * FROM bul_data WHERE `TITLE` LIKE '%{$_POST[Submit]}%' OR `DEPARTMENT` LIKE '%{$_POST[Submit]}%' ORDER by DATE"; $res = mysql_query($query) or die(mysql_error()); echo "qry=".$query; if(mysql_num_rows($res)>0) { $display.= "<table width=\"100%\" border=\"0\">"; "<tr>"; "<th>Title</th>"; "<th>Department</th>"; "<th>Link</th>"; "</tr>"; while($row=mysql_fetch_assoc($res)) { $title = $results['title']; $department = $results['department']; $campus = $results['campus']; $link = $results['link']; $display .= "<tr> <td><strong>$title</strong></td> <td>$department</td> <td><a href=$link></a></td> </tr>"; } $display .="</table>"; } echo $display; }else{ echo "No matches.";} ?></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 7, 2006 Share Posted December 7, 2006 You changed the name of the text box to submit, plus you have a submit button with the name submit. Now you have created an array!Change the name of the textbox to something more relevant like "textSearch" - I would advise against ever giving a filed the name submit which is not a submit button.Then change the $_POST[Submit] in your query to the name you hcanged the text box to. Quote Link to comment Share on other sites More sharing options...
caine Posted December 10, 2006 Author Share Posted December 10, 2006 Thanks mjdamato, it works! However, my code can only works for 1 keyword search and exact keyword match. If I were to type few phrase (more than 1 keywords), then the code would not work. How should I solve this problem? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 11, 2006 Share Posted December 11, 2006 dunno, never tried but couldnt you do a loop for all your keywords, like explode it at at space then run a for loop or foreach then do each query and i dunno. try a few things. i'd try now but couldnt be bothered. Quote Link to comment Share on other sites More sharing options...
caine Posted December 11, 2006 Author Share Posted December 11, 2006 $search = explode('', $search);$search = import('%', $search); Do something like this? where should I insert these lines into? and i don't have ideas of wad other lines of programming i should do. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 11, 2006 Share Posted December 11, 2006 $searchWords = explode(" ", $_POST[Submit]);foreach ($searchWords as $word) { if ($word) { $whereParts[] = "`TITLE` LIKE '%{$word}%' OR `DEPARTMENT` LIKE '%{$word}%'" }}$whereClause = implode(" OR ", $whereParts)$query = "SELECT * FROM bul_data WHERE {$whereClause} ORDER by DATE"; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 12, 2006 Share Posted December 12, 2006 yeh thats something like i was thinking of. :) Quote Link to comment Share on other sites More sharing options...
caine Posted December 20, 2006 Author Share Posted December 20, 2006 Thousand thanks to both of you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.