Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to echo user name and id

    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

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Try this

    PHP Code:
    $query mysql_query("SELECT * FROM users WHERE user_name='".$username."'"); 
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by pphpnewbie View Post
    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:
    PHP Code:
    $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')"
    ); 
    Leonard Whistler

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by Len Whistler View Post
    PHP Code:
    $user_id $row[user_id]; 
    should generate a warning, because of the undefined constant user_id. correct use:

    PHP Code:
    $user_id $row["user_id"]; 

  • Users who have thanked Dormilich for this post:

    Len Whistler (01-15-2010)

  • #6
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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());
    ?>

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Before all of that you first need to connect to the database and select a database to work with using mysql_connect() and 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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JAY6390 View Post
    Before all of that you first need to connect to the database and select a database to work with using mysql_connect() and 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.

    Code:
    <?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>

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 Code:
    <?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>
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It still not working, I suspect the problem is with this row -

    Code:
    $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.

  • #12
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Good point. Where is $username being set? I dont see it anywhere
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #13
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JAY6390 View Post
    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

  • #14
    New Coder
    Join Date
    Jan 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    By the way I am using dreamweaver most of the codes are generated. This is the user login code for my page -

    Code:
    <?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 );
      }
    }
    
    ?>

  • #15
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by Dormilich View Post
    should generate a warning, because of the undefined constant user_id. correct use:

    PHP Code:
    $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.


    ---
    Leonard Whistler


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •