search form problem

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
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

search form problem

Post by kristie380 »

feyd | Please use

Code: Select all

and

Code: Select all

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


I am trying to get my PHP script to display multiple search results for a keyword that a user inputs into my search form.  So far I can only get it to display 1 record (whatever the last record entered was).  Can someone tell me what I need to add to my PHP script to get it to display more than one search record? 

Here is my PHP code:

Code: Select all

<?php
$dbHost = 'mysql';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'alumni';

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!$db_selected) { die('Error : ' . mysql_error()); }

$sql = "SELECT * FROM signup";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{
$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$email = $newArray['email'];
$url = $newArray['url'];
$update = $newArray['update'];

 }
?>

<hr width="60%" size="1" color="#006633" align="left">
<font color="#006633" size="4"><b><?php echo "$firstname $maidenname $lastname";?></b></font><p>
<font color="#006633" size="3"><?php echo "$update/n";?></font>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate 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 »

your output is outside the result set loop iteration.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

I'm sorry but I'm kind of new to this. Can you explain a little more?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

he means that because the display of the output

Code: Select all

<hr width="60%" size="1" color="#006633" align="left"> 
<font color="#006633" size="4"><b><?php echo "$firstname $maidenname $lastname";?></b></font><p> 
<font color="#006633" size="3"><?php echo "$update/n";?></font>
is not inside the loop, when the loop finishes, it will only use the last loop values for that.

to solve put the above code inside the loop so that it does it once for every record.
hiren
Forum Newbie
Posts: 3
Joined: Sun Oct 16, 2005 4:48 am

Same problem with me

Post by hiren »

I am facing same problem and I tried to search for it in so many forums.

But his is the best forum I found for php, I loved it so much.

Please solve my problem so that I can solve ur problem.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

if you are facing the same problem make sure your not doing what he did wrong by looking at my above post. if thats not it, posting your code might help.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

Ok so when I place my code inside the loop and perform a search, it pulls up all of the records in my database rather than pulling up only the records that meet my search criteria. Any suggestions? Here is my new code:

Code: Select all

<?php
$dbHost = 'mysql';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'alumni';

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!$db_selected) { die('Error : ' . mysql_error()); }

$sql = "SELECT * FROM signup";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{

$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$email = $newArray['email'];
$url = $newArray['url'];
$update = $newArray['update'];


echo "<hr width=\"60%\" color=\"#006633\" size=\"1\" align=\"left\"><font color=\"#006633\" size=\"4\"><b>$firstname $maidenname $lastname </b></font><p><font color=\"#006633\" size=\"3\">$update</font><p>";

 }
?>
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

you need the query to have the criteria in it to limit the return for example

Code: Select all

"SELECT * FROM signup WHERE username LIKE '%search%'"
the % is a wildcard thatll match anything for example

thelkjsearch
searchrgrger
efkjsearchrgkl

should all match above
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Thanks

Post by kristie380 »

Thanks! That worked!!
Post Reply