Page 1 of 1

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

Posted: Sun Mar 13, 2005 11:04 am
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]

Posted: Sun Mar 13, 2005 11:15 am
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.)

Posted: Sun Mar 13, 2005 3:18 pm
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.. :?

Try this

Posted: Sun Mar 13, 2005 3:32 pm
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]