Jump to content

Viewing full user data after $_GET request


funkfact

Recommended Posts

Hi,

 

Firstly, I am a complete newcomer to PHP though I am enjoying my first tentative steps into learning the language!

 

I have been searching Google for hours now to try and find a simple solution to a problem I just can't seem to figure out, though I thought would be easy to find? I have found a lot of very informative solutions to other questions on this forum so figured it was the best one to join?

 

My question is: I have built and can interact with a simple database and now populate a page with usernames in a list. Each one is a link which, when clicked sends the username to another page. On this page I use the $_GET function and see the username displayed ok. What I can't figure out is how to then display other information about them from the fields in the database identified with that user?

 

Any help would be greatly appreciated as this is driving me nuts! Also can anyone suggest a good book for learning PHP? There are lots out there but the reviews on Amazon seem to always talk about coding errors in print etc. so I really don't know which one to buy?

 

FF

Link to comment
Share on other sites

Hey Welcome!

 

So you need to send the users ID in the link

 

then on the new "profile page" you will do something like

 

$id = $_GET['id']; //or whatever you want to name it

 

then you will have a query like this

 

$sql = (" SELECT * FROM members WHERE id = '$id' ")

 

then you will do a while

 

while($row = mysql_fetch_assoc($sql))

{

  $username = $row['name'];

  $userbday = $row['bday'];

}

 

then you can use those vars in the page where ever you want by jsut echoing them!

 

if you need more help let me know!

 

Link to comment
Share on other sites

Actually, you'll want to validate and sanitize the incoming $_GET variable before you use it in the DB query.

 
// this assumes that the user id is an integer value, and that the value in the URL is like: script.php?id=[some_value]
if( isset($_GET['id']) && ctype_digit($_GET['id']) ) {
     $id = (int) $_GET['id'];
}

 

Don't use a wildcard SELECT * to select the database fields unless you're actually going to use all of them.

$query = "SELECT `field1`, `field2`, `field3` FROM `table` WHERE `user_id` = $id";

 

You need to also execute the query, and since there is only one record associated with each user id, there's no need to use a loop to retrieve the data. It's also best practice to make sure the query actually succeeded, so an error or debugging info can be echoed if it fails.

if( $result = mysql_query($query) ) {
     $array = mysql_fetch_assoc($result);
} else {
     echo "Query: $query<br>Error: " . mysql_error();
}

 

And I'm sure you already know it, but don't forget to include your database connection in the script.

Link to comment
Share on other sites

Wow, you guys reply fast! You are so kind, thanks very much!

 

I wasn't using the ID field in the previous list page, I was using the 'name' field in the list displayed:

 

<? echo '<ul>

';

for($count = 1; $count <= $numName; $count++)

{

    $row = mysql_fetch_array($getName);

    echo '<li><a href="user_details.php?name=' . $row['name'] . '">' . $row['name'] . '</a></li>

';

}

echo '</ul>

'; ?>

 

This displays the names as links just fine, should I be using the ID number instead?

 

I quickly tried your suggestions, as I have got to go to work in a minute :-(, but in the table where I expected the results to appear, ie:

 

<? echo $name; ?>

<a href="<? echo $link; ?>" target="_blank">Your Web Page</a>

 

etc, nothing appears! Guess I'll have to wait until tomorrow when I can review my code more closely!

 

Any suggestions on a good book to purchase?

 

Until tomorrow.... thanks again :-)

 

FF

 

 

 

Link to comment
Share on other sites

Ok. So I tried the options above but the linked page opens with this error:

 

"Query: SELECT `join_date`, `name`, `location`, `link` FROM `members` WHERE `name` =

Error: 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"

 

Here is my code for the first and second pages, maybe someone can tell me where I'm going wrong?:

 

-----------------------------------

(results.php - this page works just fine)

 

<?php
include("dbinfo.php");

$conn = mysql_connect($host,$username,$password);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db($db, $conn);
if(!$status) die("Failed to select database!");
$getMembers = mysql_query('SELECT name FROM members ORDER BY id') or die(mysql_error());
$numMembers = mysql_num_rows($getMembers);

echo '<ul>
';
for($count = 1; $count <= $numMembers; $count++)
{
    $row = mysql_fetch_array($getMembers);
    echo '<li><a href="member_details.php?name=' . $row['name'] . '">' . $row['name'] . '</a></li>
';
}
echo '</ul>
';
mysql_close();
?>

 

-----------------------------------

(member_details.php)

 

<?php
include("dbinfo.php");

$conn = mysql_connect($host,$username,$password);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db($db, $conn);
if(!$status) die("Failed to select database!");

if( isset($_GET['name']) && ctype_digit($_GET['name']) ) {
     $name = (int) $_GET['name'];
}
$query = ("SELECT `join_date`, `name`, `location`, `link` FROM `members` WHERE `name` = $name");
if( $result = mysql_query($query) ) {
     $array = mysql_fetch_assoc($result);
} else {
     echo "Query: $query<br>Error: " . mysql_error();
}
?>

 

Hope my code is not a mess!! As I said I'm just starting out with this...

 

FF

Link to comment
Share on other sites

You really should be asking the database to look for records where a USER ID is present, not the USER NAME. If you have more than one user named John Smith you are going to run into a hell of a lot of problems. Make USER ID the primary field in your table, with an AUTO_INCREMENT so that it will be unique.

Link to comment
Share on other sites

Hi Paddyfields,

 

The first field, primary auto increment, in my database table is already ID. I tried changing the query but it made no difference. My problem is that my code (above) doesn't work and yields no results? I was hoping somebody could look at the code and tell me what I am doing wrong?

 

FF

Link to comment
Share on other sites

Hi BlueSkyIS,

 

Added your code to Try what you suggested but still can't make no sense of it??

 

Here is the result I got (if it helps you diagnose it better):

 

Query: SELECT `join_date`, `name`, `location`, `link` FROM `members` WHERE `name` = 
Error: 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_GET['name']: Jayne Casey
name: 
query: SELECT `join_date`, `name`, `location`, `link` FROM `members` WHERE `name` = 

 

Cheers,

 

FF

Link to comment
Share on other sites

Bleary eyed, untested, but...

 

<?php
include("dbinfo.php");
$conn = mysql_connect($host,$username,$password);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db($db, $conn);
if(!$status) die("Failed to select database!");
$getMembers = mysql_query('SELECT id, name FROM members ORDER BY name') or die(mysql_error());
echo '<ul>';
while($row = mysql_fetch_array($getMembers)) {
?>
<li><a href="member_details.php?id=<?PHP echo $row['id']; ?>"><?PHP echo $row['name']; ?></a></li>
<?PHP
}
echo '</ul>';
mysql_close();
?>

 

and...

 

<?php
include("dbinfo.php");
$conn = mysql_connect($host,$username,$password);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db($db, $conn);
if(!$status) die("Failed to select database!");

if( isset($_GET['id']) && ctype_digit($_GET['id']) ) {
$id = (int) $_GET['id'];
}
$query = ("SELECT `join_date`, `name`, `location`, `link` FROM members WHERE id = '$id'");
if( $result = mysql_query($query) ) {
$row= mysql_fetch_array($result);
echo "Name: " . $row['name'] . "<br>";
echo "Join Date: " . $row['join_date'] . "<br>";
echo "Location: " . $row['location'] . "<br>";
echo "Link: " . $row['link'] . "<br>";
} else {
echo "Query: $query<br>Error: " . mysql_error();
}
?>

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.