Jump to content

Search result turns nothing with space


3raser

Recommended Posts

http://minecraftservers.comyr.com/search.php

 

<?php
session_start();
include("includes/mysql.php");
include("includes/config.php");

?>

<title><?php echo $title; ?></title>

<?php
echo "<a href='../index.php'>Home</a> | <a href='start_upload.php'>Upload</a> | Search & Browse | <a href='/news'>News & Announcements</a> | <a href='logout.php'>Logout</a><hr>";

$search = $_POST['search'];

if(!$search)
{
	echo "<form action='search.php' method='POST'>Search: <input type='text' name='search'><input type='submit' value='Search'></form>";
}
else
{

$query = mysql_query("SELECT * FROM mods WHERE name LIKE '%$search%' OR description LIKE '%$search%'");
while ($row = mysql_fetch_assoc($query))
{
$x++;
	echo "#". $x .": ". $row['name'] .".[VIEW]<br/>";
}
if($x=="0")
{
	echo "No results found.";
}
else {
	echo "<br/>".$x." results.";
}
}
include("includes/footer.php");
?>

 

Whenever you type in something without spaces, it works perfectly. When you do add a space, it basically goes blank. :/

Link to comment
Share on other sites

you will need to cleanse the search data, to make sure php doesn't interpret the spaces as anything other than a space. I can't think of the functions of the top of my head, but a simple google search should find something you need. It will convert spaces into &nbsp (I believe).

 

So look into cleansing of input text.

 

Denno

Link to comment
Share on other sites

you will need to cleanse the search data, to make sure php doesn't interpret the spaces as anything other than a space. I can't think of the functions of the top of my head, but a simple google search should find something you need. It will convert spaces into &nbsp (I believe).

 

So look into cleansing of input text.

 

Denno

 

Doesn't that nbsp function add a space? If so, how do this help? The code already has a space when someone types in a space manually.

Link to comment
Share on other sites

no, do not replace spaces with the html entity   unless you expect   to actually be in the data.

 

$search = (isset($_POST['search']))?$_POST['search']:false;

if(!$search) {
    echo "<form action='search.php' method='POST'>Search: <input type='text' name='search'><input type='submit' value='Search'></form>";
} else {
      if (get_magic_quotes_gpc()) {
         $search = stripslashes($search);
      }
      $search = mysql_real_escape_string($search);
      $sql = "SELECT * FROM mods WHERE name LIKE '%$search%' OR description LIKE '%$search%'";
      $query = mysql_query($sql) or die(mysql_error());

      // Now, loop over results.

}

 

updated to replace $result with $query

Link to comment
Share on other sites

no, do not replace spaces with the html entity   unless you expect   to actually be in the data.

 

$search = (isset($_POST['search']))?$_POST['search']:false;

if(!$search) {
    echo "<form action='search.php' method='POST'>Search: <input type='text' name='search'><input type='submit' value='Search'></form>";
} else {
      if (get_magic_quotes_gpc()) {
         $search = stripslashes($search);
      }
      $search = mysql_real_escape_string($search);
      $sql = "SELECT * FROM mods WHERE name LIKE '%$search%' OR description LIKE '%$search%'";
      $query = mysql_query($sql) or die(mysql_error());

      // Now, loop over results.

}

 

updated to replace $result with $query

 

Still does the same glitch, and I updated my code accordingly.

 

<?php
session_start();
include("includes/mysql.php");
include("includes/config.php");

?>

<title><?php echo $title; ?></title>

<?php
echo "<a href='../index.php'>Home</a> | <a href='start_upload.php'>Upload</a> | Search & Browse | <a href='/news'>News & Announcements</a> | <a href='logout.php'>Logout</a><hr>";

$search = (isset($_POST['search']))?$_POST['search']:false;

if(!$search) {
    echo "<form action='search.php' method='POST'>Search: <input type='text' name='search'><input type='submit' value='Search'></form>";
} else {
      if (get_magic_quotes_gpc()) {
         $search = stripslashes($search);
      }
     $search = mysql_real_escape_string($search);
 $sql = "SELECT * FROM mods WHERE name LIKE '%$search%' OR description LIKE '%$search%'";
     $query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($query))
{
$x++;
	echo "#". $x .": ". $row['name'] .".[VIEW]<br/>";
}
if(!$x)
{
	echo "No results found.";
}
else 
{
	echo "<br/>".$x." results.";
}

}
include("includes/footer.php");
?>

Link to comment
Share on other sites

Doesn't that nbsp function add a space? If so, how do this help? The code already has a space when someone types in a space manually.

 

All I said was this is what I think the cleansing functions will do. Read more carefully and don't assume things.

 

Denno

 

How about don't be an ass?

 

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.