Jump to content

Archived

This topic is now archived and is closed to further replies.

Lyricsride

Very simple search script not working. I'm Baffled. **[SOLVED]**

Recommended Posts

Hey guys, so i'm using a VTC to learn some php [url=http://www.vtc.com/products/php.htm]http://www.vtc.com/products/php.htm[/url] but the demonstration he gave in the video, doesn't work on my machine. I'm thinking maybe there's a problem with my php.ini settings, or the application in the video is changed in the new versions of php. Here's the code that doesn't seem to run right.

"searchbyname.html"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>A Simple Search Script</title>
</head>
<body>
<h2> Search for a user</h2>
<br />
<form action="resultsbyname.php" method="post">
  Please enter the name, or part of the name, of the user you are seeking: <br />
  <input name="name" type="text" />
  <br />
  <input type="submit" value="search" />
</form>
</body>
</html>
---------------------------------------------------------------------------

"resultsbyname.php"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>
<br />

<?php
echo $name;
?>

<br />
<a href="searchbyname.html">Go back to the search page</a>
</body>
</html>
---------------------------------------------------------------------------

The two pages of code about seem to be exactly what he has. Any ideas? Thanks.

Lyricsride

Share this post


Link to post
Share on other sites
sorry, i guess i should have said what it meant to happen. The first page should pass the inputed user text to the second page, and display it ...

Share this post


Link to post
Share on other sites
unless your register globals is set to on (usually set off by default on most servers), you'll need to put this (in php) before you use the variable names:

foreach($_POST as $key=>$value) {
$$key = $value;
}

Share this post


Link to post
Share on other sites
Thanks for the quick reply! Worked great. I know using globals is not reccomended so I will certainly want to use this method then. Would you mind maybe explaining quickly how the code exactly works? Either way thanks for the quick reply, appreciate it.

Share this post


Link to post
Share on other sites
Well I'm still a bit of a newb myself, and the reason I knew the answer to that is because I've scratched my head over the same problem before :P

But basically when you use a post method like that, in the following page, you can just type
$_POST[$variable_name]  to call each variable.

This foreach loop just goes through every item posted and assigns it to the appropriate variable.

Glad I could help! :)

Share this post


Link to post
Share on other sites
Ah ok, that seems to make sense right enough.
For a "GET" form method, (instead of post) would you simply modify it to say
[b]$_GET[$variable_YOURVARIABLE][/b] i suppose?

Also, the
$_POST[$variable_[b]name[/b]] part of the code is what calls the specific variable being passed right? Well, thanks for the help bud!  ;)

Share this post


Link to post
Share on other sites
Arg. Update:

I thought the problem i was having before, was the cause of another one in a slightly more complicated situation. Turns out it's not. The 'second' problem i'm having is this.

"searchbyname.html" [still same page as before]
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>A Simple Search Script</title>
</head>
<body>
<h2> Search for a user</h2>


<form action="resultsbyname.php" method="post">
  Please enter the name, or part of the name, of the user you are seeking:

  <input name="name" type="text" />


  <input type="submit" value="search" />
</form>
</body>
</html>
---------------------------------------------------------------------------


"resultsbyname.php"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>



<?php
foreach($_POST as $key=>$value) {
$$key = $value;
}

echo $name;
$db =      mysql_connect("localhost",root,314314);
  mysql_select_db("prototyping",$db);
$query =      "SELECT users.usr_fname
          FROM users
  WHERE name LIKE '%".$name."%'";
$result = mysql_query($query);

[color=red][b]while($record = mysql_fetch_assoc($result)){[/b][/color]
while(list($fieldname, $fieldvalue) = each($record)){
echo $fieldname.": <b>".$fieldvalue."</b><br />";
}
echo"<br />";
}
?>



<a href="searchbyname.html">Go back to the search page[/url] 
</body>
</html>
---------------------------------------------------------------------------

When i type in a search of some sort, the "echo $name;" works fine and repeats what i typed, but the search throws this error:

[b]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\server\Apache2.2\htdocs\learn_php_test\resultsbyname.php on line 22[/b]

Line 22 is the [b]bolded [/b][color=red]red[/color] code. At first, I thought that this error was becasue $query failed to get $name which i though might casue $result to screw up, thus causing line 22 to fail since it used $result. But there's another problem apparently...

users.usr_fname is a varchar(50) cell, it currently only contains one record. When i ran a search "%jas%" in myPHPadmin this was the Query it used:

SELECT *
FROM `prototyping`.`users`
WHERE `usr_id` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_alias` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_fname` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_lname` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_joindate` LIKE '%%jas%%'
OR `usr_email` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_bio` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_location` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
LIMIT 0 , 30

---------

So that's about all the info i can think of giving... hopefully i'm not testing your knowledge -too- much =0

Share this post


Link to post
Share on other sites
Change
[code]
$result = mysql_query($query);
[/code]

to

[code]
$result = mysql_query($query) or die(mysql_error());
[/code]

and tell us what the error is.

Share this post


Link to post
Share on other sites
:D Thanks for the reply -both- of you haha.
Ok, so this is what it said when i search using "searchbyname.php" page (source code above):

[b]Unknown column 'name' in 'where clause'[/b]

Here is the page source again. I think the error is that my SQL syntax is searching my database for a column titled "name" instead of using the search string passed from the search in the previous page. That's the only thing that seems to make sense based on the error message...  ???
------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>
<br />

<?php
$hostname_logon = "localhost"; 
$database_logon = "prototyping"; 
$username_logon = "root"; 
$password_logon = "314314";

//open database connection
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die("Unabale to connect to the database");

//select database
mysql_select_db($database_logon,$connections) or die ("Unable to select database!");

//specify how many results to display per page, loaded into a variable
$limit = 10;

//allows passing of variables from page-to-page (without using Global Variables On in PHP.ini)
foreach($_POST as $key=>$value) {
$$key = $value;
}

//tests to see if directly-above code is working right, it should output the search string typed in the previous page
echo $name;

$query = "SELECT users.usr_fname
FROM users
WHERE name LIKE '%".$name."%'";
$result = mysql_query($query) or die(mysql_error());
while($record = mysql_fetch_assoc($result)){
while(list($fieldname, $fieldvalue) = each($record)){
echo $fieldname.": <b>".$fieldvalue."</b><br />";
}
echo"<br />";
}
?>

<br />
<a href="searchbyname.html">Go back to the search page</a>
</body>
</html>
---------------------------------

Share this post


Link to post
Share on other sites
Look at your query:

[code]SELECT users.usr_fname FROM users WHERE name LIKE '%".$name."%'[code]

Do you have a column named "name"?  Apparently you don't (from the error), so you are probably wanting that to be:

[code]SELECT users.usr_fname FROM users WHERE usr_fname LIKE '%".$name."%'[code][/code][/code][/code][/code]

Share this post


Link to post
Share on other sites
Hitman... wow. Well first off. lol, you were spot on. I'm just shocked becasue i could of sworn i went over that query a million times, and was sure it was perfect. When you pointed out the error i immediatly thought to myself "OH CHRIST OF COURSE?!@$!%R FASD FAFASDA GFA >:(>:(>:(>:("

haha... well thanks -a lot- Hitman and the rest of you who posted replys. I think i learnt something today about paying attention more carefully to my code....  :-\

Share this post


Link to post
Share on other sites

×

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.