Jump to content

Recommended Posts

I have a login system that works perfectly but i need to greet the user with his/her Firstname once they have logged in. I have a Mysql table sorted. And the registration works fine. All the info is put into the table. But how can i retrieve it.

 

AIM - I want login_success.php to show the users firstname.

 

checklogin.php

<?php

session_start();
$host="**********"; // Host name
$username="***********"; // Mysql username
$password="********"; // Mysql password
$db_name="*********"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form
$myemail=$_POST['myemail'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE Email='$myemail' and Password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myemail and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $myemail and redirect to file "login_success.php"
$_SESSION['myemail'] = $myemail;
$_SESSION['mypassword'] = $mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

final.php (registration form)

	<?php
$host="**********"; // Host name
$username="***********"; // Mysql username
$password="********"; // Mysql password
$db_name="*********"; // Database name
$tbl_name="web_members"; // Table name
// Connect to server and select database.
echo '<form action="index.php" method="post">
<input type="hidden" name="email" value="'.$_POST['email'].'">	
<input type="hidden" name="fname" value="'.$_POST['fname'].'">
<input type="hidden" name="sname" value="'.$_POST['sname'].'">
<input type="hidden" name="pass" value="'.$_POST['pass'].'">
<input type="hidden" name="passtwo" value="'.$_POST['passtwo'].'">
<input type="hidden" name="llno" value="'.$_POST['llno'].'">
<input type="hidden" name="mobno" value="'.$_POST['mobno'].'">
<input type="hidden" name="addno" value="'.$_POST['addno'].'">
<input type="hidden" name="addone" value="'.$_POST['addone'].'">
<input type="hidden" name="addtwo" value="'.$_POST['addtwo'].'">
<input type="hidden" name="addthree" value="'.$_POST['addthree'].'">
<input type="hidden" name="city" value="'.$_POST['city'].'">
<input type="hidden" name="other" value="'.$_POST['other'].'">
<input type="hidden" name="country" value="'.$_POST['country'].'">
<input type="hidden" name="noc" value="'.$_POST['noc'].'">
<input type="hidden" name="ccno" value="'.$_POST['ccno'].'">
<input type="hidden" name="expdate" value="'.$_POST['expdate'].'">
<input type="hidden" name="cvv" value="'.$_POST['cvv'].'">
 <input type="image" src="register.gif" name="submit" alt="Log In" value="Log In" />';

mysql_connect("$host", "$username", "$password") or die ("cannot connect: " . mysql_error());
mysql_select_db("$db_name") or die("cannot select DB: " . mysql_error());

$sql = "INSERT INTO web_members VALUES ('".$_POST['email']."', '".$_POST['pass']."', '".$_POST['fname']."', '".$_POST['sname']."', '".$_POST['addno']."', '".$_POST['addone']."', '".$_POST['addtwo']."', '".$_POST['addthree']."', '".$_POST['city']."', '".$_POST['country']."', '".$_POST['other']."', '".$_POST['noc']."', '".$_POST['ccno']."', '".$_POST['expdate']."', '".$_POST['expdatey']."', '".$_POST['cvv']."')";

mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());
?>

 

login_success.php

<?
// start your session
   session_start();

   // variables to connect to db here
$host="**********"; // Host name
$username="***********"; // Mysql username
$password="********"; // Mysql password
$db_name="*********"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(!session_is_registered(myemail)){

header("location:loginotd.php");

}else{

  // query string. just an example to pull all info from 'something'

  $sql = "select * from web_members WHERE Email='$myemail'";

  // execute the query and put the result SOURCE in $result

  $result = mysql_query($sql);

  // loop through the result source to pull out the rows 1 at a time

  // until there are no more rows to pull out

  while ($list = mysql_fetch_assoc($result)) {

     // assign each row to a session variable. we're going to make it an array

     $_SESSION['blah'][] = $list['Email'];

  } // end while

  
  if ($_SESSION['blah']) {
      // example of echoing out each 'something'
      echo $_SESSION['blah'][0]; // echo out first one
   echo $_SESSION['blah'][1];
   echo $_SESSION['blah'][3];
   echo $_SESSION['blah'][4];
   echo $_SESSION['blah'][5];
      echo $_SESSION['blah'][2]; // echo out third one
      echo $_SESSION['blah'][6]; // echo out seventh one

      // example of looping through all of them...
      foreach($_SESSION['blah'] as $key => $val) {
         echo "element: $key  value: $val <br/>";
      } // end foreach
   // end if exists
   // else, if it doesn't exist...
   } else {
			 echo "blah doesnt exist";   

   } // end else
  
  
  }
  
?>

 

My database looks like this:

 

mysqltablejh0.png

 

I believe the problem lies within the login_success.php.

 

Thank you for reading.

 

Snooble

 

 

Link to comment
https://forums.phpfreaks.com/topic/36504-im-almost-there-a-little-help-please/
Share on other sites

Just echo the column name...

 

You're getting all the details from the database by using the query SELECT * FROM web_members WHERE Email='$myemail' and then assigning them with

$list = mysql_fetch_assoc($result)

so assuming the user's first name is in the web_members table and is called Firstname.  Just use this after the query.

 

<?php
echo "Welcome " . $list['Firstname'];
?>

 

Regards

Huggie

Huggie, i changed my login_successful.php page to:

 

<?
// start your session
   session_start();

   // variables to connect to db here
$host="************"; // Host name
$username="********"; // Mysql username
$password="********"; // Mysql password
$db_name="*******"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(!session_is_registered(myemail)){

header("location:loginotd.php");

}else{

  // query string. just an example to pull all info from 'something'

  $sql = "select * from web_members WHERE Email='$myemail'";

  // execute the query and put the result SOURCE in $result

  $result = mysql_query($sql);

  // loop through the result source to pull out the rows 1 at a time

  // until there are no more rows to pull out

  while ($list = mysql_fetch_assoc($result)) {

     // assign each row to a session variable. we're going to make it an array

  } // end while

  
echo "blah doesnt exist" . $list['Firstname'];   

   } // end else
  
  
  
?>

 

and i get the output "blah doesnt exist". Thats all

 

How would i set it in the session? As they log in with their email and password?

 

Snooble

In PHPMyAdmin i try

 

select * from web_members WHERE Email='thealbionbook@yahoo.co.uk'

 

and i get this:

 

sqlnw7.png

 

Which is correct.

 

Now i want to take "Firstname" out of that table and paste it on the welcome page. But, Obviously

 

select * from web_members WHERE Email='thealbionbook@yahoo.co.uk'

 

has to be set to:

 

select * from web_members WHERE Email='$myemail'

 

because i want it to greet the user personally, with THEIR firstname.

 

Cheers Huggie for the interest.

 

Snooble

OK, here's the code then, look at the change I made to it...

 

<?php
// start your session
session_start();

// variables to connect to db here
$host="************"; // Host name
$username="********"; // Mysql username
$password="********"; // Mysql password
$db_name="*******"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(!session_is_registered(myemail)){
   header("location:loginotd.php");
}
else{
   // query string. just an example to pull all info from 'something'
   $sql = "select * from web_members WHERE Email='$myemail'";

   // execute the query and put the result SOURCE in $result
   $result = mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); // Added error reporting

   // Did away with the loop as only one row is returned!
   $list = mysql_fetch_assoc($result);

   echo "blah doesnt exist" . $list['Firstname'];   

} // end else
?>

 

Regards

Huggie

I still get the output "blah doesnt exist"

 

What did you change in the code? As i must've missed something because the colors were gone in my code.

 

There must be trouble with

 

. $list['Firstname']; 

 

Thanks again for the effort, It CAN be done!!!! :)

 

Snooble

 

you have a try:

 

http://wezzsmusic.ifastnet.com/logintest/index.php

 

email = tester@test.com

pass = test

 

login is on the right.

 

Snooble

Snoob, I've looked at your code and sent you a revised version.

 

This was a simple case of overlooking something already posted.  My advice would be to sloooooow down :)

 

Basically after putting $myemail into a session variable $_SESSION['myemail'] and then redirecting to the login_success.php you still tried to run a query using $myemail.

 

login_success.php has no visibility of $myemail, you needed to use $_SESSION['myemail']

 

Regards

Huggie

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.