a simple Mysql php search engine - doesn't work - need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ice-o
Forum Newbie
Posts: 1
Joined: Sun Mar 13, 2005 11:00 am

a simple Mysql php search engine - doesn't work - need help

Post by ice-o »

It automatically just displays the first line out of the table and that's it, i can't search, nuthing!

Code: Select all

<?php
require("config.php");

echo"
<form action=\"$_PHPSELF\" method=\"post\">
<input type=\"text\" name=\"searchterm\"><br/>
<input type=\"Submit\" value=\"Update\">
</form>";

$searchterm = $_GET['searchterm'];

$query = "SELECT content, title FROM pages WHERE content LIKE '%$searchterm%' OR title LIKE '%$searchterm%'"; 
$result = mysql_query($query);

if (!isset($_GET['submit']))
{
$row = mysql_fetch_array($result);

$content = $row['content'];
$title = $row['title'];

echo "$content";
echo "$title";

}


else {
	echo "standard";
}


?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. your form uses POST, you are looking for $_GET['submit'] which will not exist. $_POST['Submit'] will, but you shouldn't check for it, as IE at least will not submit the button if someone hit enter in the text field.
  2. $_PHPSELF doesn't exist. $PHP_SELF may.. $_SERVER['PHP_SELF'] does.
  3. your query will potentially return multiple records. A loop is required to get all the records found. Not just asking for an array (mysql_fetch_array() returns a single record, at most.)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ice-o made a new thread for this by mistake.. :roll:
ice-o wrote:

Code: Select all

<?php
require("config.php");

echo"
<form action="$PHP_SELF" method="post">
<input type="text" name="searchterm"><br/>
<input type="Submit" value="Update">
</form>";

$searchterm = $_GET['searchterm'];

$query = "SELECT content, title FROM pages WHERE content LIKE '%$searchterm%' OR title LIKE '%$searchterm%'"; 
$result = mysql_query($query);

if (isset($_POST['submit']))
{
$row = mysql_fetch_array($result);

$content = $row['content'];
$title = $row['title'];

echo "$content";
echo "$title";

}


else {
	echo "standard";
}


?>
this looks exactly like your first post.. :?
brad7451
Forum Newbie
Posts: 4
Joined: Sat Mar 12, 2005 1:03 am

Try this

Post by brad7451 »

Code: Select all

<?php
require("config.php"); 
echo"<form action=\".$_SERVER['PHP_SELF']."\" method=\"post\">
<input type=\"text\" name=\"searchterm\"><br/>
<input type=\"hidden\" name=\"posted\" value=\"true\">
<input type=\"Submit\" value=\"Update\">
</form>";

$searchterm = $_POST['searchterm']; 
$query = mysql_query("SELECT content, title FROM pages WHERE content LIKE '%$searchterm%' OR title LIKE '%$searchterm%'"); 

if(isset($_POST['posted'])) {
  while($result=mysql_fetch_array($query))  {
      $content = $result['content'];
      $title = $result['title'];
      echo $content."<BR>";
      echo $title."<BR><BR>"; 
      }
  }
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply