Jump to content

[SOLVED] php search username and echo that username's profile


contra10

how to connect to username's profile?  

1 member has voted

  1. 1. how to connect to username's profile?

    • displaying a searched users profile while logged in as yourself?
      1
    • display user profile?
      0


Recommended Posts

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.

Link to comment
Share on other sites

is today "Don't use [ code ] tags" day. I chose, just like Columbus day, not to participate!  ;D

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.