Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. // Things to notice:
  4. // The main job of this script is to execute a SELECT statement to find the user's profile information (then display it)
  5.  
  6. // execute the header script:
  7. require_once "header.php";
  8.  
  9. if (!isset($_SESSION['loggedInSkeleton']))
  10. {
  11. // user isn't logged in, display a message saying they must be:
  12. echo "You must be logged in to view this page.<br>";
  13. }
  14. else
  15. {
  16. // user is already logged in, read their username from the session:
  17. if(isset($_GET['username'])){
  18. $username = $_SESSION['username'];
  19. }
  20. else
  21. {
  22. $username=$_SESSION['username'];
  23.  
  24. }
  25.  
  26. // now read their profile data from the table...
  27.  
  28. // connect directly to our database (notice 4th argument):
  29. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  30.  
  31. // if the connection fails, we need to know, so allow this exit:
  32. if (!$connection)
  33. {
  34. die("Connection failed: " . $mysqli_connect_error);
  35. }
  36.  
  37. // check for a row in our profiles table with a matching username:
  38. $query = "SELECT * FROM profiles WHERE username='$username'";
  39.  
  40. // this query can return data ($result is an identifier):
  41. $result = mysqli_query($connection, $query);
  42.  
  43. // how many rows came back? (can only be 1 or 0 because username is the primary key in our table):
  44. $n = mysqli_num_rows($result);
  45.  
  46. // if there was a match then extract their profile data:
  47. if ($n > 0)
  48. {
  49. // use the identifier to fetch one row as an associative array (elements named after columns):
  50. $row = mysqli_fetch_assoc($result);
  51. // display their profile data:
  52. echo "First name: {$row['firstname']}<br>";
  53. echo "Last name: {$row['lastname']}<br>";
  54. echo "Number of pets: {$row['pets']}<br>";
  55. echo "Email address: {$row['email']}<br>";
  56. echo "Date of birth: {$row['dob']}<br>";
  57. }
  58. else
  59. {
  60. // no match found, prompt user to set up their profile:
  61. echo "You still need to set up a profile!<br>";
  62. }
  63.  
  64. // we're finished with the database, close the connection:
  65. mysqli_close($connection);
  66.  
  67. }
  68.  
  69. // finish off the HTML for this page:
  70. require_once "footer.php";
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement