contra10 Posted December 10, 2008 Share Posted December 10, 2008 I created a search engine, not specific yet cause I'm just testing in my website...nyways I conduct a search and the values that show up are the usernames of everyone in my database...when i click on the name it sends me to the page of the user already logged in. How do I make a link to the user whos name is clicked and echo their information? // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { $query = "SELECT username FROM users"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $user = "{$row['username']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/main/'>$user</td>"; echo "</tr>"; } } ?></td> </table> </form> <? } the form is above this code and is connected so it does show results just doesn't link to user specific profile. Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/ Share on other sites More sharing options...
JonnoTheDev Posted December 10, 2008 Share Posted December 10, 2008 Do you mean a specific profile page i.e. profile.php Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711582 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 is today "Don't use [ code ] tags" day. I chose, just like Columbus day, not to participate! but here is the simple issue. echo "<td width='35%'><a href='http://localhost/main/'>$user</td>"; (notice use of code tags :- ) the link is going to the same place no matter what. echo "<td width='35%'><a href='http://localhost/main/?user=$user'>$user</td>"; that will make a $_GET['user'] the username on the next page which you can use in a query. Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711586 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 ok i kinda understand and I changed the code so now its $user = "{$row['username']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/main/?user=$user'>$user</td>"; echo "</tr>"; and in the url it echos the username which i wanted...but how to i change the variables on the page not...do i create a profile.php and then go about creating a query...cause right now it shows the url but the page is still the same as the user who logged in... by the way thanks for the quick responses Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711604 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 yes, creating a profile.php page or whatever you want to call it would be a good direction to go. use the query string (the url variables) in the query for the profile.php. then echo the values as you'd like of course. A better practice would be to use the users unique id instead of name. $user_id = $row['id_field_on_table'] ... ... echo "<td width='35%'><a href='http://localhost/main/?user=$user_id'>$user</td>"; then on profile.php if(is_numeric($_GET['user']){ $ID = $_GET['user']; } else { die('Bad user input. screw off'); } then query WHERE ID_Field = $ID Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711615 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 Brian THANK YOU VEY VERY MUCH....i just signed up for this site and already its helped me a lot. thanks...hmm i dunno how to put as solved now...lol Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711625 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 solved is down and left. Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711632 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 sorry Brian I guess I was too quick to jump to me being right, now for some reason its not working... //This code runs if the form has been submitted if (isset($_POST['submit'])) { $query = "SELECT id FROM users"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $user = "{$row['id']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/profile/index.php?user=$user'>$user</td>"; echo "</tr>"; } } ?> it works and then in my profile .php i have <?php if(is_numeric($_GET['user']){ $ID = $_GET['user']; } else { die('Bad user input. screw off'); } $user= "SELECT username FROM users WHERE ID_Field = $id" ?> i got stuck and couldn't finish the code...confused once more...sry Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711671 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 what are your fields in the DB? is your ID field really called ID_Field or is it id? then, if it works. $query= "SELECT username FROM users WHERE ID_Field = $id" //ID_Field or is it id? $result = mysql_query($query); $user = mysql_fetch_assoc($result); echo $user['username']; that should be a good testing start Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711708 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 its called id if(is_numeric($_GET['user']){ $ID = $_GET['user']; } else { die('Bad user input.'); } $query= "SELECT username FROM users WHERE id = $id"; $result = mysql_query($query); $user = mysql_fetch_assoc($result); echo $user['username']; Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711713 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 yeah, whats it do when you run it? Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711718 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 i put it at top of the page it says Parse error: syntax error, unexpected '{' in C:\wamp\www\profile\index.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711721 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 if(is_numeric($_GET['user']){ should be if(is_numeric($_GET['user'])){ if() and is_numeric both need to be closed... 1(2(3(4(5())))) Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711726 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 this is what i have on my search page i have //This code runs if the form has been submitted if (isset($_POST['submit'])) { $query = "SELECT id FROM users"; $result = mysql_query($query); $query2 = "SELECT username FROM users"; $result2 = mysql_query($query2); while($row = mysql_fetch_assoc($result) AND $row2 = mysql_fetch_assoc($result2)) { $user = "{$row['id']}"; $userq = "{$row2['username']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/profile/index.php?user=$user'>$userq</td>"; echo "</tr>"; } } my sql tables for my databse registration are username and id...among other things the link goes to profile.php which then has the code <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if(is_numeric($_GET['user'])){ $ID = $_GET['user']; $query= "SELECT username FROM users WHERE id = $id"; $result = mysql_query($query); $user = mysql_fetch_assoc($result); echo $user['username']; } else { die('Bad user input.'); } ?> it now says Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\profile\index.php on line 11 line 11 is $user = mysql_fetch_assoc($result); Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711734 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 dude, seriously... use the [ code ][ /code ] tags if you are putting anything more than like two lines. I use it all the time if its more than one variable or function. [ code ]code here, use code tags without the spaces between the brackets[ /code ] that looks like code here, use code tags without the spaces between the brackets Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711738 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 ok on my search page //This code runs if the form has been submitted if (isset($_POST['submit'])) { $query = "SELECT id FROM users"; $result = mysql_query($query); $query2 = "SELECT username FROM users"; $result2 = mysql_query($query2); while($row = mysql_fetch_assoc($result) AND $row2 = mysql_fetch_assoc($result2)) { $user = "{$row['id']}"; $userq = "{$row2['username']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/profile/index.php?user=$user'>$userq</td>"; echo "</tr>"; } } my sql tables for my databse registration are username and id...among other things the link goes to profile.php which then has the code <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if(is_numeric($_GET['user'])){ $ID = $_GET['user']; $query= "SELECT username FROM users WHERE id = $id"; $result = mysql_query($query); $user = mysql_fetch_assoc($result); echo $user['username']; } else { die('Bad user input.'); } ?> it now says Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\profile\index.php on line 11 line 11 is $user = mysql_fetch_assoc($result); Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711746 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 um $query = "SELECT id FROM users"; will only return the id of the user, not the user name $query = "SELECT * FROM users"; will get all fields, which is fine. just use one of your mysql_fetch_assoc on both instances of $result = mysql_query($query); change to $result = mysql_query($query) or die(mysql_error()); that will feed you out an error, which is good for debugging. The reason it is coming back as a invalid resource is that if the $result returns an error, it is not what mysql_fetch_assoc is expecting as a resource. btw, thanks for using code tags! lol Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711748 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 ok i changed the first field so like u said it works <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { $query = "SELECT * FROM users"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $user = "{$row['id']}"; $userq = "{$row['username']}"; echo "<TABLE width=650 border=1 align=center cellpadding=4 cellspacing=0>"; echo "<tr>"; echo "<td width='35%'><a href='http://localhost/profile/index.php?user=$user'>$userq</td>"; echo "</tr>"; } } ?> and i also changed the profile.php <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if(is_numeric($_GET['user'])){ $ID = $_GET['user']; $query= "SELECT username FROM users WHERE id = $id"; $result = mysql_query($query) or die(mysql_error());; $user = mysql_fetch_assoc($result); echo $user['username']; } else { die('Bad user input.'); } ?> it now says You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711758 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 $ID = $_GET['user']; needs to be $id = $_GET['user']; reason is because in your query you have it lowercase and you define it in caps... Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711769 Share on other sites More sharing options...
contra10 Posted December 10, 2008 Author Share Posted December 10, 2008 Thank you...I checked it now, and it works Quote Link to comment https://forums.phpfreaks.com/topic/136377-solved-php-search-username-and-echo-that-usernames-profile/#findComment-711779 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.