...

View Full Version : how to echo user name and id



pphpnewbie
01-15-2010, 05:15 AM
This script does not echo the user name and id when the user logged in,

<?php
session_start();
$username = $_SESSION["username"];

$query = mysql_query("SELECT * FROM users WHERE user_name='$username'");

$row = mysql_fetch_array($query);

$ID = $row['id']; //we all know what a variable is=?

// Display's the id and the user name
echo "<p>User ID:". $ID;
echo "<p>Username: " . $_SESSION["username"];

?>

Would appreciate any help.
TIA

rfresh
01-15-2010, 05:56 AM
Try this



$query = mysql_query("SELECT * FROM users WHERE user_name='".$username."'");

pphpnewbie
01-15-2010, 06:49 AM
Thanks for your reply. Still no result.

Anyway this is what I want to accomplish:

I have 2 tables. Table 1 is user username; password; First name; and user_id (primary key)

Second table is details details_id(primary key), user_id(foreign key from user table); contact; age; etc.

When a user logged in to fill up a form to insert into details table, how can the user_id be automatically populated dynamically from user table to details table.

TIA.

Len Whistler
01-15-2010, 07:18 AM
When a user logged in to fill up a form to insert into details table, how can the user_id be automatically populated dynamically from user table to details table.



It would be something like:

$result = mysql_query("SELECT * FROM user WHERE user_id = '$user_id'");
$row = mysql_fetch_row($result);

$user_id = $row[user_id];

$details_id = // from form
$contact = // from form
$age = // from form

mysql_query("INSERT INTO details (user_id, details_id, contact, age)
VALUES ('$user_id', '$details_id', '$contact', '$age')");

Dormilich
01-15-2010, 10:28 AM
$user_id = $row[user_id];

should generate a warning, because of the undefined constant user_id. correct use:


$user_id = $row["user_id"];

pphpnewbie
01-15-2010, 12:55 PM
Still cannot figure out how to make it happen. How can I run this sql behind the scene, when an authenticated user log in to submit a form.


<?php
session_start();

mysql_query("SELECT * FROM users WHERE user_name = '$username'");
$row = mysql_fetch_row($result);
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$_SESSION['username'] = $row['user_name'];
$_SESSION['userid'] = $row['user_id'];


$query2 = "INSERT INTO toys (user_id) VALUES ('$userid')";
$result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());
?>

whizard
01-15-2010, 01:43 PM
You're close, but just a little mixed up.

1) Change mysql_query("SELECT * FROM users WHERE user_name = '$username'"); to $query = "SELECT * FROM users WHERE user_name = '$username'";

2) Your $row and $result lines are mixed up. You need to put the $result row first.

3) Get rid of the while loop. (But keep the two $_SESSION lines)

Dan

JAY6390
01-15-2010, 01:53 PM
Before all of that you first need to connect to the database and select a database to work with using mysql_connect() (http://www.php.net/mysql_connect) and mysql_select_db() (http://www.php.net/mysql_select_db)
As far as I can tell this script is just standalone (ie there's no other script calling this) since you have session_start() at the start of it and that doesn't usually go in sub-files

pphpnewbie
01-15-2010, 03:02 PM
Before all of that you first need to connect to the database and select a database to work with using mysql_connect() (http://www.php.net/mysql_connect) and mysql_select_db() (http://www.php.net/mysql_select_db)
As far as I can tell this script is just standalone (ie there's no other script calling this) since you have session_start() at the start of it and that doesn't usually go in sub-files

Yes, I just cannot figure out where to insert them. Whichever page I insert this code the page just won't work. :(


<?php include("includes/connections.php"); ?>
<?php include("includes/validate.php"); ?>

<?php
session_start();

$query = "SELECT * FROM users WHERE user_name = '$username'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_row($result);

($row = mysql_fetch_assoc($result)) {
$_SESSION['username'] = $row['user_name'];
$_SESSION['userid'] = $row['user_id'];


$query2 = "INSERT INTO toy (user_id) VALUES ('$userid')";
$result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());
?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
</head>

<body class="main">
<div id="wrapper">
<div id="header">
<h1>My Toy Collection</h1>
<h2>Welcome</h2>
</div>
<div id="mainContent">
<form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<table border="0" cellpadding="0" cellspacing="0" id="userTbl" >
<tr>
<td>User Name:<label>
<input type="text" name="user_name" id="user_name" />
</label></td>
</tr>
<tr>

<td>Password :<label>
<input type="password" name="user_password" id="user_password" />
</label></td>
</tr>
<tr>

<td><label>
<input type="submit" name="submit" id="submit" value="Log in" />
</label></td>
</tr>
</table>
</form>
</div>
<div id="footer">copy righted 2010</div>
</div>
</body>
</html>

JAY6390
01-15-2010, 03:07 PM
Well that code had a few errors in it. You should have error reporting on for this
Here is your code without errors, and hopefully will work

<?php
session_start();

include ("includes/connections.php");
include ("includes/validate.php");

$query = "SELECT * FROM users WHERE user_name = '$username'";
$result = mysql_query($query) or die('Query failed: '.mysql_error());
$row = mysql_fetch_row($result);

while($row = mysql_fetch_assoc($result)) {
$_SESSION['username'] = $row['user_name'];
$_SESSION['userid'] = $row['user_id'];


$query2 = "INSERT INTO toy (user_id) VALUES ('$userid')";
$result2 = mysql_query($query2) or die('Query failed: '.mysql_error());
}
?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
</head>

<body class="main">
<div id="wrapper">
<div id="header">
<h1>My Toy Collection</h1>
<h2>Welcome</h2>
</div>
<div id="mainContent">
<form id="form1" name="form1" method="POST" action="<?php

echo $loginFormAction;

?>">
<table border="0" cellpadding="0" cellspacing="0" id="userTbl" >
<tr>
<td>User Name:<label>
<input type="text" name="user_name" id="user_name" />
</label></td>
</tr>
<tr>

<td>Password :<label>
<input type="password" name="user_password" id="user_password" />
</label></td>
</tr>
<tr>

<td><label>
<input type="submit" name="submit" id="submit" value="Log in" />
</label></td>
</tr>
</table>
</form>
</div>
<div id="footer">copy righted 2010</div>
</div>
</body>
</html>

pphpnewbie
01-15-2010, 04:06 PM
It still not working, I suspect the problem is with this row -


$result = mysql_query("SELECT * FROM users WHERE user_name = '$username'");

I cannot understand how does '$username' work? When John logs in with the username of 'john123', can the '$username' be changed to 'john123' automatically.

JAY6390
01-15-2010, 04:08 PM
Good point. Where is $username being set? I dont see it anywhere

pphpnewbie
01-15-2010, 04:16 PM
Good point. Where is $username being set? I dont see it anywhere

Sorry I am clueless how to set it. I thought when an authenticated user log in, with his username it will be captured somewhere and be assigned as $username.

Please guide me along. Thanks

pphpnewbie
01-15-2010, 04:21 PM
By the way I am using dreamweaver most of the codes are generated. This is the user login code for my page -


<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['user_name'])) {
$loginUsername=$_POST['user_name'];
$password=$_POST['user_password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "menu.php";
$MM_redirectLoginFailed = "index.php";
$MM_redirecttoReferrer = true;
mysql_select_db($database_dw_prestigeproperty, $dw_prestigeproperty);

$LoginRS__query=sprintf("SELECT user_name, user_password FROM users WHERE user_name=%s AND user_password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

$LoginRS = mysql_query($LoginRS__query, $dw_prestigeproperty) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && true) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}

?>

Len Whistler
01-15-2010, 11:58 PM
should generate a warning, because of the undefined constant user_id. correct use:


$user_id = $row["user_id"];


Thanks Dormilich ........ I was wondering why I ended up with that warning when I backed up one of my sites to my home computer. The hosted site was OK, but on my computer I got some undefined constant warnings. I solved the problem by placing error_reporting(0); into the header.

I assume that PHP must have been configured differently at my host than on my computer running XAMPP.


---

pphpnewbie
01-16-2010, 12:17 AM
It would be something like:

$result = mysql_query("SELECT * FROM user WHERE user_id = '$user_id'");


Hi, Len Whistler how to you identify the user_id of the login user.
How this $user_id works?
TIA

Len Whistler
01-16-2010, 12:24 AM
Hi, Len Whistler how to you identify the user_id of the login user.
How this $user_id works?
TIA

I don't full understand how such a script works, but I do use one based on this tutorial. I have it working with no problems.

http://www.phpeasystep.com/workshopview.php?id=6



--------



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum