从mysql获取数据以显示在网页上

<?php
session_start(); // First thing... every time
require_once("connect.php");    if(!isset($_SESSION['username'])){
header('Location: login.php'); the top
exit; // Always include after redirect.
 }
if(isset($_POST['submit'])){

 $sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
 mysql_query($sql) or die("Error in SQL: " . mysql_error());  

 $query="SELECT lastname FROM user WHERE username = " . $_SESSION['username'] . ";"; 
 $result=mysql_query($query); 
   $row = mysql_fetch_assoc($result); 
  $_SESSION[lastname] = $row[lastname]; 
 header("Location: thankyou.html", true, 303); // Look up "303: See Other"
   exit;
 }
?>

above is my php code. What i want to do is display the lastname from the database onto the webpage.

code on webpage is

Welcome <?php echo $_SESSION['lastname']; ?>.

the issue is that the lastname is not displayed after welcome

i see that u r just getting the display i.e., select display from table. if you need lastname and display then you should be doing Select display,lastname from user.

Hopefully this will work.

<?php
session_start(); // First thing... every time
require_once("connect.php");    if(!isset($_SESSION['username'])){
header('Location: login.php'); the top
exit; // Always include after redirect.
 }
if(isset($_POST['submit'])){

$userName = mysql_real_escape_string($_SESSION['username']);
 $sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . $userName . "'";
 mysql_query($sql) or die("Error in SQL: " . mysql_error());  

 $query="SELECT lastname FROM user WHERE username = '" . $userName . "';"; 
 $result=mysql_query($query); 
   $row = mysql_fetch_assoc($result); 
  $_SESSION['display'] = $row['lastname']; 
  $_SESSION['lastname'] = $row['lastname']; 
 header("Location: thankyou.html", true, 303); // Look up "303: See Other"
   exit;
 }
?>

You haven't used apostrophes surrounding array keys line 15 so PHP is looking for a key matching a key name corresponding to a constant named lastname.