Forum Moderators: coopster
If I declare $search ='dvd'; the query returns the results for dvd correctly
So the query code is fine and db attach is good
I cant seem to get the info from the form to the page to $search ?
Any ideas. I need this code to search a new FAQ CMS added or another script
Same page
<p style="float:center; padding: 5px;">
<form method="post" action="searchnew.php">
<input name="search" size="25"/>
<input value="Search" name="search" type="submit" class="button"/>
<small style="text-align:right;">
<a href="search.php">Advanced Search</a>
</small>
</form>
</p>
Query is
if ($search) // perform search only if a string was entered.
{
$lResInsert=mysql_connect("$set_mysql_host","$set_mysql_user","$set_mysql_pass");
$lResSelect=mysql_select_db("$set_mysql_base",$lResInsert);
//$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%'";
$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%'";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
How do I return the value of the post from the browser?
If I hit refresh it tells me I have a postdata value so I know it has the search query in the post?
Sorry I am stumped?
if ($_POST) {
// all your databse stuff
}
else {
// display your form
}
<edit>
Just noticed that you are using if ($search), do you have globals on?
[edited by: PHP_Chimp at 9:55 am (utc) on April 8, 2008]
have a search form that I want to use for the query
Form on the same page
<p style="float:center; padding: 5px;">
<form method="post" action="<?php print($_SERVER['PHP_SELF'] ); ?>">
<input name="search" size="25"/>
<input value="Search" name="search" type="submit" class="button"/>
<small style="text-align:right;">
<a href="search.php">Advanced Search</a>
</small>
</form>
<?
if ($_POST) {
// all your databse stuff
}
else {
// display your form
}
?>
<br />
</p>
//query info
<?php
//Secure the DB base access connection
include "admin/config/db_inc.php";
//Secure End
//$search ='classifieds';
echo "$search";
echo "<br />";
if ($search) // perform search only if a string was entered.
{
$lResInsert=mysql_connect("$set_mysql_host","$set_mysql_user","$set_mysql_pass");
$lResSelect=mysql_select_db("$set_mysql_base",$lResInsert);
//$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%'";
$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%'";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
//Fill out some vars first
$q = $row['q_id'];
$question = $row['question'];
$answer = $row['answer'];
//Rewrite to capitals
$question = ucwords(strtolower($question));
$answer = ucwords(strtolower($answer));
$search = ucwords(strtolower($search));
// Load the Side Menu
echo "<table border=0' cellspacing='0' class='welcome' style='clear:both; background: #F2F2F2; border-right: 1px dotted #666666;'>";
echo "<tr>";
echo "</tr>";
echo "<td>";
//session_start();
if (session_is_registered("valid_user"))
{
}
else
{
include "admin/templates/login.tpl";
}
// Google Adverts Start
include "admin/templates/adverts.tpl";
echo "</td>";
// Google Adverts Start
echo "</tr>";
echo "</table>";
echo "</td>";
echo "<td>";
// Display the adverts
echo "<div class='containertable' cellspacing='0'>";
echo "<table border='0' cellspacing='0' class='box' style='clear:both;'>";
echo "<tr>";
echo "<th width='40%'>Search Results the term $search</th>";
echo "</tr>";
//start alt columns
echo "<tr bgcolor=\"$bgcolor\">";
if ($numrows == 0) {
echo "<p> No items to shows</p>";
}
else {
$row = 0; // set count of array to be 0 for first run
for($i = 0; $i < count($row); ) {
while($row=mysql_fetch_assoc($result)){
$bgcolor = ($i % 2) ? '#EFEFEF' : '#F8F8F8';
//print results....
echo "<td bgcolor=\"$bgcolor\" style='clear:both; border-bottom: 1px dotted #666666; padding-left: 2 !important;' >";
echo "<h3><a href='http://$url/faq/faq.php?q_id=".$row['q_id']." '>" . substr($row['question'],0,100) . " ...</a></h3><br /><br /> " .substr($row['answer'],0,200) ."" ;
echo '<br/><br />';
echo "</td>";
echo "</tr>";
// alt mod
$i++;
}
//alt mod end
}
}
}
echo "</table>";
echo "</div>";
?>
If you change that part to
if ($_POST['search']) {
if ([url=http://uk2.php.net/manual/en/function.array-key-exists.php]array_key_exists[/url]('search', $_POST)) {
Also avoid using the short tags <?, as they are not always supported. Stick to <?php.
$search = $_POST["search"];
Returns the query ok with this search form
<p style="float:center; padding: 5px;">
<form method="post" action="searchnew.php">
<input type=text name='search' size="25"/>
<input value="Search" type=submit class="button"/>
<small style="text-align:right;">
<a href="search.php">Advanced Search</a>
</small>
</form>
<br />
</p>
So you have got it to work :)
I need to santize the input do you know how I can do this?
You could use array_walk [uk2.php.net] to apply mysqli_real_escape_sting to all of the $_POST array.
$sanitized = array_walk($_POST, 'mysqli_real_escape_string');
if ($sanitized) {
// inputs ok to use
}
else {
// inputs still nasty
}
$search = mysqli_real_escape_string($_POST['search']);
$another = mysqli_real_escape_string($_POST['another']);
I have just used the filter [owasp.org...]
added the filter to the search page
require('sanitize.inc.php');
$var=100.50;
$search = sanitize($var,HTML);
Next question is
$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%'";
I know but cant find how to do more than one search using pipes as some times the search seems to be very lean and not return any results unless it was a single word.
I think I can do LIKE '%$search%' ¦¦ next search ¦¦ another search ¦¦ etc
Thanks for all the help so far you have made pointed me in the right direction each time.
So WHERE answer LIKE 'a%' would match apple, avocado, anaconda, etc.
So as you have %'s at either end of the search string that should provide good coverage.
You can also use the AND or OR statements
WHERE answer LIKE '%$search%' OR 'test';
ok this seems to work
I need to have the results be from both answer and question at the moment its only answer
$query = "SELECT question, answer, q_id FROM faq_questions WHERE answer LIKE '%$search%' or '$search%' '$search%'";
So
SELECT question, answer, q_id FROM faq_questions WHERE answer
selects deom question and answer from the fa_question table but then its where answer
Do I need to join to get two tables?