How to display User Specific content from Table in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • demon917
    New Member
    • Feb 2014
    • 1

    #1

    How to display User Specific content from Table in PHP

    I have 8 rows/columns. They are;
    Key (which will never repeat), Name, HireDate, Seniority, VacHrs, PTOhrs, username AND password.

    After a successful login, this is my code;

    Code:
    <?php
    $dbhost = '***7.000webhost.com';
    $dbuser = 'a3674631_***';
    $dbpass = 'm***';
    $conn = mysql_connect("mysql17.000webhost.com", "a36***31_***n9", "****08");
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    $sql = 'SELECT Name, HireDate, Seniority, VacHrs, PTOhrs FROM att';
    
    mysql_select_db('a3674631_test');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_NUM))
    {
        echo "Name :{$row[0]}  <br> ".
             "Hire Date : {$row[1]} <br> ".
             "Seniority : {$row[2]} <br> ".
    		 "Vacation Hours Remaining : {$row[3]} <br> ".
    		 "PTO Hours Remaining : {$row[4]} <br> ".
             "--------------------------------<br>";
    }
    mysql_free_result($retval);
    echo "Fetched data successfully\n";
    mysql_close($conn);
    ?>
    The problem I’m having is it’s putting everyone’s data, all on one page together for everyone to see. This is private information, and Person A should not be able to see the values for person B. My table has 6 records(current ly). If user A has a login of 111791, on a successful login, I would like it to say;

    Name: First Last
    HireDate: 04/04/2012
    Seniority: 1 (dates that are sorted by earliest to latest)
    VacHrs: 45
    PTOhrs: 18
    Key, username & password will never show.

    Can someone tell me what I’m doing wrong. I know that I’m a noob, so please skip all the “I need read this book….or that tutorial”. I know I do, I’m going to, but in the mean time, I need to get this taken care of. I thank you sincerely for any help.
    Last edited by Dormilich; Feb 5 '14, 06:27 AM.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    If you only want to show the data of the person logged in, you need to restrict your DB data through an SQL WHERE clause.

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      1) Call your app with the user's login id as a parameter. This will send one parameter to your app.
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
       <head></head>
       <body>
       <form method="POST" action="[B]<[U]your path[/U]>/<[U]your app name[/U]>[/B].php">
      		Enter login id:
      		<input type="text" id="userId">
      		<input type="submit" value="Look up info">
      		<input type="reset">
      	</form>  
       </body>
      </html>
      2)Separate the key field into two fields userId and password or create a numeric auto increment field for the key and a userId field

      note the "where" clause at the end of the sql statement that Dormilich mentioned above.

      Code:
      $userId = $_REQUEST;
      
      $sql = 'SELECT Name, HireDate, Seniority, VacHrs, PTOhrs FROM att [B]WHERE userid = '.$userId;[/B]
      Last edited by Claus Mygind; Feb 5 '14, 01:42 PM. Reason: Added note to view off screen text

      Comment

      Working...