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

时间:2012-02-16 00:53:33

标签: php 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;
 }
?>

以上是我的PHP代码。我想要做的是将数据库中的姓氏显示在网页上。

网页上的代码是

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

问题是在欢迎

之后没有显示姓氏

3 个答案:

答案 0 :(得分:0)

我看到你只是得到显示器,即从表中选择显示。如果您需要姓氏和显示,那么您应该选择显示,来自用户的姓氏。

答案 1 :(得分:0)

希望这会奏效。

<?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;
 }
?>

答案 2 :(得分:0)

你没有在数组键行15周围使用撇号,因此PHP正在寻找一个与名为lastname的常量相对应的键名匹配的键。