...

View Full Version : Search.php



Dat
03-06-2008, 01:14 AM
I have this snip of code I got from a tutorial, I've tested it on safari and it works fine but when I go onto my windows computer and try it on ie7 It just goes to the search page with no results. As if I didn't search for anything.

Code:


<?php
if(isset($_POST['submit'])){
$problem = FALSE;

if(empty($_POST['search'])){
$problem = TRUE;
echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
}
if(!$problem){
$text = escape_data($_POST['search']);
$query = "SELECT * from anime_reviews WHERE title LIKE '&#37;$text%' OR summary LIKE '%$text%' OR genres LIKE '%$text%'";
$result = @mysql_query($query);
$query2 = "SELECT id, title, summary, DATE_FORMAT(date, '%M %d, %Y') as sd FROM anime_reviews ORDER BY id DESC limit 15";
$result2 = @mysql_query($query);
if($result && $result2){
echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$view = 'anime.php?id=' . $row['id'];

echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a></li>';
}
echo "</ul><br />
<a href=\"search.php\">Search Again?</a>";
}
else
{
echo "No posts matched your query";
}
}
}
else
{
?>
<form id="search" action="search.php?search" method="post">
Keyword: <input type="text" name="search" size="30" class="text" /><br />
<input type="submit" name="submit" value="Search" />
</form>
<?php
}
?>

_Aerospace_Eng_
03-06-2008, 05:17 AM
Try this

<?php
// just in case register globals is on, someone can't pass true or false through the url making $problem to be whatever they enter
$problem = '';
if(isset($_POST['submit']) && $_POST['submit'] == 'Search')
{
// just in case register globals is on, someone can't pass true or false through the url making $problem to be whatever they enter
if(isset($_GET['problem']))
{
$_GET['problem'] = FALSE;
}
$problem = FALSE;
if($_POST['search'] == '')
{
if(isset($_GET['problem']))
{
$_GET['problem'] = TRUE;
}
$problem = TRUE;
echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
}
if(!$problem)
{
$text = escape_data($_POST['search']);
$query = "SELECT * from anime_reviews WHERE title LIKE '&#37;$text%' OR summary LIKE '%$text%' OR genres LIKE '%$text%'";
// You need some error checking on your queries. If you are getting errors then find out why you are getting them rather than supressing them
$result = mysql_query($query) or die(mysql_error());
$query2 = "SELECT id, title, summary, DATE_FORMAT(date, '%M %d, %Y') as sd FROM anime_reviews ORDER BY id DESC limit 15";
$result2 = mysql_query($query) or die(mysql_error());

// I would check for mysql_num_rows instead.
if(mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0)
{
echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$view = 'anime.php?id=' . $row['id'];
echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a></li>';
}
echo "</ul><br />
<a href=\"search.php\">Search Again?</a>";
}
else
{
echo "No posts matched your query";
}
}
}
else
{
?>
<!--IE was having issues with the form having id="search" and the input having name="search". IE treats ids and names as the same thing so it was confused -->
<form action="search.php" method="post">
Keyword: <input type="text" name="search" size="30" class="text" /><br />
<input type="submit" name="submit" value="Search" />
</form>
<?php
}
?>

Dat
03-06-2008, 05:51 AM
I've updated it with your provided coded. Still doesn't work in Ie. Works fine with safari still.
Here is were my search page is: http://www.romanceanime.com/search.php

_Aerospace_Eng_
03-06-2008, 06:07 AM
I just did a search for the word "test" and I got results. Make sure you aren't seeing a cached version. Hit CTRL + F5. Your BETA search also has a form with id="search", you need to remove that.

Dat
03-06-2008, 01:59 PM
Okay, it works ONLY if I press on the search key, I've been pressing enter and it doesn't work. How do I fix that?

_Aerospace_Eng_
03-06-2008, 03:46 PM
You didn't fix the code at all. Your BETA search has id="search" in it. Remove that please.

Dat
03-06-2008, 09:47 PM
Alright I've updated it, still doesn't work.

_Aerospace_Eng_
03-06-2008, 10:25 PM
Hmm looks like this has always been an issue in IE.

http://muffinresearch.co.uk/archives/2005/12/08/fun-with-multiple-submit-buttons/

Dat
03-07-2008, 12:03 AM
So what should I do? Make two input fields?

_Aerospace_Eng_
03-07-2008, 12:31 AM
I wonder if a hidden one will suffice. Something like

<input type="hidden" name="dummy">
or

<input type="text" name="dummy" style="display:none;">

Dat
03-07-2008, 01:52 AM
I've researched about this issue on google and found that there is a PHP solution but have not found the proper source to fix my problem. If there are any PHP solution please reply. ~ Thank you

_Aerospace_Eng_
03-07-2008, 06:42 AM
Where did you read that there was a solution? You can probably give the form its own name and check for submission of that though I think thats we were doing already don't know.

Dat
03-11-2008, 05:46 AM
It's much better to use: GET rather than POST :p

Dat
03-13-2008, 04:16 AM
Alright I got my search to work on ie now, and I got it work with multiple keywords but it displays double results. Example I search "and and" Stupid example but you get my point.

I think I should use preg_replace() but I don't know how to confront this with my code, I won't want to screw up anything with my nooby fingers.

If you see anything NOOBY in this code please tell me I'm learning, like the way I organize or.. whatever!


<form method="get" action="search3.php">
<input type="text" name="search" size="30" />
<input type="submit" value="Search" />
</form>
<?php
include("include/db_connect.inc.php");

function escape_data ($data) {
global $dbc; // Need the connection
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}

if(isset($_GET['search'])){
$search = trim(escape_data($_GET['search'])); // Escape and trim withspaces
$search_array = explode(' ',$search); // Seperate each keyword

if ($search == '') { // If no keyword is entered
$resultmsg = 'Search Error: Please enter a search...' ;
}
if (!isset($resultmsg)) {
foreach ($search_array as $keyword){
$query = "SELECT * FROM anime_reviews WHERE title LIKE '%$keyword%' OR genres LIKE '%$keyword%' OR summary LIKE '%$keyword%' ORDER BY title ASC" ;
$result = mysql_query ($query);

// Check number of rows and if more than 0 continue
if(mysql_num_rows($result) > 0){
while($data = mysql_fetch_array($result)){
$data['summary'] = substr($data['summary'], 0, 300);
echo '<a href="/anime.php?id=' . $data['id'] . '">' . $data['title'] . '</a> (' . $data['title_jap'] . ')<br>' . $data['summary'] . '...<br>';
}
} else {
$resultmsg = 'No entries match your search';
}
}
}
}
echo @$resultmsg;
?>

Dat
03-15-2008, 06:11 AM
Alright I got my search to work on ie now, and I got it work with multiple keywords but it displays double results. Example I search "and and" Stupid example but you get my point.

I think I should use preg_replace() but I don't know how to confront this with my code, I won't want to screw up anything with my nooby fingers.

If you see anything NOOBY in this code please tell me I'm learning, like the way I organize or.. whatever!


<form method="get" action="search3.php">
<input type="text" name="search" size="30" />
<input type="submit" value="Search" />
</form>
<?php
include("include/db_connect.inc.php");

function escape_data ($data) {
global $dbc; // Need the connection
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}

if(isset($_GET['search'])){
$search = trim(escape_data($_GET['search'])); // Escape and trim withspaces
$search_array = explode(' ',$search); // Seperate each keyword

if ($search == '') { // If no keyword is entered
$resultmsg = 'Search Error: Please enter a search...' ;
}
if (!isset($resultmsg)) {
foreach ($search_array as $keyword){
$query = "SELECT * FROM anime_reviews WHERE title LIKE '%$keyword%' OR genres LIKE '%$keyword%' OR summary LIKE '%$keyword%' ORDER BY title ASC" ;
$result = mysql_query ($query);

// Check number of rows and if more than 0 continue
if(mysql_num_rows($result) > 0){
while($data = mysql_fetch_array($result)){
$data['summary'] = substr($data['summary'], 0, 300);
echo '<a href="/anime.php?id=' . $data['id'] . '">' . $data['title'] . '</a> (' . $data['title_jap'] . ')<br>' . $data['summary'] . '...<br>';
}
} else {
$resultmsg = 'No entries match your search';
}
}
}
}
echo @$resultmsg;
?>

Really what is the best solution go about this?

Dat
03-18-2008, 05:02 AM
Actually I'm suppose to use.. array_unique arn't I? How do I use it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum