skip to Main Content

I have been trying to create a greeting after a user logs in that says “Welcome, first name (dynamic).” so when the user logs in they are greeted with their name. For some reasont it has not been working out. I am new at php so this may be a simple error. Any assist or advice would be useful. Thanks.

main code

session_start();


$mysql_hostname = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$mysql_database = 'db_users2015';

$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die ("Couldn't connect");

echo "<BR>Connection Successful";

  //to put data into database
  //select database
  $db_selected= mysql_select_db($mysql_database, $connect) 
  or die ("Couldn't connect to the database");

//frontend and backend data processing
  $email= $_POST['email'];
  $password= $_POST['password'];



//To see if email is registered
$sql = "SELECT COUNT(*) FROM users WHERE email= '{$_POST['email']}'";
$sql_result = mysql_query($sql);
if (mysql_result($sql_result, 0)<1)
 {
die("<BR>Email address not found");
}

else{
    echo "Login Successful!";
}


//To check if email and password match

$sql = "SELECT count(*) FROM users WHERE email = '$email' AND 
password ='$password' LIMIT 1";

$result = mysql_query($sql) or die(mysql_error());



$firstname = $_SESSION['firstname'];
$_SESSION['firstname']= $_POST['firstname'];


if (mysql_result($result, 0) > 0){

    echo "<BR>Login Successful, welcome";
    echo $_SESSION['firstname'];
}

if (mysql_result($result, 0) < 1){

    echo 'wrong password/username combo';
}






?>
<HTML>

  <HEAD>
    <TITLE> Programming</TITLE>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <LINK REL="stylesheet" TYPE="text/css" href="homework2.css"> 

    </HEAD>
    <BODY>

      <!-- CSS for http://the7.dream-demo.com/ -->

      <div id="container">
        
        <div id="header">
          <div class="menuitem"> <a href="home.html">Home</a> </div>
          <div class="menuitem"><a href="products.html">Products</a></div>
          <div class="menuitem"><a href="cases.html">Case Studies</a></div>
          <div class="menuitem"><a href="pricing.html">Pricing</a></div>
          <div class="menuitem"><a href="aboutus.html">About Us</a></div>
        </div>

        <div id="bodycontent">
          
          <div id="banner">
           <div id="bannerleft"> <h1> We make you better athletes. Find out how! </h1> </div>
           <div id="signin"> 
             
             <form class="well form-inline" action="login.php" method="post">

              <input type="text" class="input-small" placeholder="Email"  name="email" >
              <input type="password" class="input-small" placeholder="Password" name="password">
              <br><br>

<!--
  If you do not want to use twitter bootstrap css then you should uncomment next 6 lines and uncomment the
  above 2 lines that provide input boxes

	    <label for="email">Email:</label>
	    <input type="text" name="email" id="email">
	    <br>
	    <label for="password">Password:</label>
	    <input type="password" name="password" id="password">
	    <br>
    -->

    <input type="submit" name="submit" id="logmein" value="Log In">
  </form>
  
</div>

</div>
<div id="featurestrip"> 

  
  <div id="signup">

    <form action="signup.php" method="post">

     
     <label for="firstname">Firstname:</label>
     <input type="text" name="signup-firstname" id="signup-firstname">
     <br>

     <label for="lastname">Lastname:</label>
     <input type="text" name="signup-lastname" id="signup-lastname">
     <br>

     <label for="email">Email:&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
     <input type="text" name="signup-email" id="signup-email">
     <br>
     <label for="password">Password:</label>
     <input type="password" name="signup-password" id="signup-password">
     <br>
     <label for="password">Reconfirm Password:</label>
     <input type="password" name="signup-repassword" id="signup-repassword">
     <br><br>
     <input type="submit" name="signmeup" id="signmeup" value="Sign Me Up!">
   </form>

 </div>

 <div id="featureright"> <p>Sign up and find out more on how we can help. Pricing starts at $19.95 a month. </p>
  <p><h3>Premium service starts at $49.95.</h3></p>

</div>   


</div>
<div id="corefeatures"> 



 <img height="200px" src="http://www.hockeymanitoba.ca/wp-content/uploads/2013/02/ltad-model.jpg">
</div>

<div id="testimonials"> Testimonial
  <img height="200px" src="http://www.neuroexplosion.com/storage/development%20model%20jpeg.jpg?__SQUARESPACE_CACHEVERSION=1305662626397">

  <img height="200px" src="http://www.phecanada.ca/sites/default/files/physical_literacy/LTAD_FMS.jpg">
</div>
       <!--
       <div id="portfolio"> Portfolio</div>
       <div id="skills"> Skills</div>
     -->
   </div>
   
   <div id="footer">Copyright Notice. All Rights Reserved. 2014</div>

 </div>

 
</BODY> 
</HTML>

2nd php code edit

<?php

session_start();


$mysql_hostname = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$mysql_database = 'db_users2015';

$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die ("Couldn't connect");

echo "<BR>Connection Successful";

  //to put data into database
  //select database
  $db_selected= mysql_select_db($mysql_database, $connect) 
  or die ("Couldn't connect to the database");

//frontend and backend data processing
  $email= $_POST['email'];
  $password= $_POST['password'];



//To see if email is registered
$sql = "SELECT COUNT(*) FROM users WHERE email= '{$_POST['email']}'";
$sql_result = mysql_query($sql);
if (mysql_result($sql_result, 0)<1)
 {
die("<BR>Email address not found");
}

else{
    echo "Login Successful!";
}


//To check if email and password match

$sql = "SELECT(*) FROM users WHERE email = '$email' AND 
password ='$password' LIMIT 1";
$userdata = mysql_fetch_assoc($result);

$result = mysql_query($sql) or die(mysql_error());



$firstname = $_POST['firstname'];
$_SESSION['firstname]' = $userdata['firstname'];


if (mysql_result($result, 0) > 0){

    echo "<BR>Login Successful, welcome";
    echo $firstname;
}

if (mysql_result($result, 0) < 1){

    echo 'wrong password/username combo';
}






?>

3

Answers


  1. Problem is Been here

    $firstname = $_SESSION['firstname'];
    $_SESSION['firstname']= $_POST['firstname'];
    

    You are Trying to get First Name from login page html form. but i don’t remember if any login page ever have First name field. so you should get first name from database and put it in $_SESSION['firstname']

    Hope to be clear enough. 🙂

    To get data out of database
    Change this

    SELECT count(*) FROM users WHERE email = '$email' AND password ='$password' LIMIT 1 
    

    To This

    SELECT * FROM users WHERE email = '$email' AND password ='$password' LIMIT 1
    
    $userdata = mysql_fetch_assoc($result);
    
    $_SESSION['firstname'] = $userdata['firstname'];
    

    This will do the trick

    Login or Signup to reply.
  2. under //To check if email and password match

    put this:

    $result = mysql_query("SELECT * FROM users WHERE email = '$email' AND 
    password ='$password' LIMIT 1");
    
    if(mysql_num_rows($result) > 0){
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      $firstname = $row["firstname"];
      echo "<BR>Login Successful, welcome";
      echo $firstname;
    }else{   
      echo 'wrong password/username combo';
    }
    
    Login or Signup to reply.
  3. the place where you set your firstname should be changed with this

    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $_SESSION['firstname']= $row['firstname'];
    

    set firstname by fetching results from database because $_POST[‘firstname’] does not exists in your page.

    Thanks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search