Jump to content

Recommended Posts

Hello dear php members!

I have a problem with a script that searches the database for users.

After user types in the text, the results page doesn't show up for some reason.

 

My current code is:

<?
if ($searching =="yes")
{ echo "<h2>Results</h2><p>";
if ($find == "")
{ echo "<p>You forgot to enter a search term";
exit;
}
mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['first_name'];
echo " ";
echo $result['last_name'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{ echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; }
?>



<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="first_name">First Name</option>
<Option VALUE="last_name">Last Name</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

 

 

may someone please guide me what i did wrong?

Link to comment
https://forums.phpfreaks.com/topic/99616-php-search-script-problem/
Share on other sites

hmmm can't edit....

 

I looked a little deeper at your code. You should turn on your error reporting to start with for testing. Give this code a try see if kicks any errors back.

 

<?php
if ($_POST['searching'] == "yes")
{ echo "<h2>Results</h2><p>";
if ($find == "")
{ echo "<p>You forgot to enter a search term";
exit;
}
mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['first_name'];
echo " ";
echo $result['last_name'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{ echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; }
?>



<h2>Search</h2>
<form name="search" method="post" action="<?=$_SERVER['PHP_SELF']?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="first_name">First Name</option>
<Option VALUE="last_name">Last Name</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

uuuu getting somewhere :D

so now if i dont enter anything it says "You forgot to enter a search term" which is good

but then whatever else i enter it says "Sorry, but we can not find an entry to match your query" - no matter if it does exist in the database or not :/

so far thank you everyone for helping out !

try this...

<?php
if ($_POST['searching'] == "yes")
{ echo "<h2>Results</h2><p>";
$find = $_POST['find'];
$field = $_POST['field'];
if ($find == "")
{ echo "<p>You forgot to enter a search term";
exit;
}
mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['first_name'];
echo " ";
echo $result['last_name'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{ echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; }
?>



<h2>Search</h2>
<form name="search" method="post" action="<?=$_SERVER['PHP_SELF']?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="first_name">First Name</option>
<Option VALUE="last_name">Last Name</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

 

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.