Lyricsride Posted August 1, 2006 Share Posted August 1, 2006 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 /><?phpecho $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 Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 1, 2006 Author Share Posted August 1, 2006 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 ... Quote Link to comment Share on other sites More sharing options...
bpops Posted August 1, 2006 Share Posted August 1, 2006 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;} Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 1, 2006 Author Share Posted August 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
bpops Posted August 1, 2006 Share Posted August 1, 2006 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 :PBut 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! :) Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 1, 2006 Author Share Posted August 1, 2006 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! ;) Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 1, 2006 Author Share Posted August 1, 2006 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><?phpforeach($_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_ciOR `usr_alias` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciOR `usr_fname` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciOR `usr_lname` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciOR `usr_joindate` LIKE '%%jas%%'OR `usr_email` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciOR `usr_bio` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciOR `usr_location` LIKE CONVERT( _utf8 '%%jas%%'USING latin1 )COLLATE latin1_swedish_ciLIMIT 0 , 30---------So that's about all the info i can think of giving... hopefully i'm not testing your knowledge -too- much =0 Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 2, 2006 Author Share Posted August 2, 2006 bump. Still stumped.. ??? Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 2, 2006 Share Posted August 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 2, 2006 Share Posted August 2, 2006 :D, haha you beat me to the punch! ;) Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 3, 2006 Author Share Posted August 3, 2006 :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 databasemysql_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 pageecho $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>--------------------------------- Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 3, 2006 Share Posted August 3, 2006 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] Quote Link to comment Share on other sites More sharing options...
Lyricsride Posted August 3, 2006 Author Share Posted August 3, 2006 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.... :-\ Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 3, 2006 Share Posted August 3, 2006 what dvd auther are you using for your course cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.