2013-02-21 142 views
0

我在這裏有一個問題,那就是我想在員工輸入員工ID後顯示員工姓名。我的index.php會顯示員工姓名,但沒有成功。這個問題在歡迎它後顯示「資源ID#4」。如何根據用戶名顯示用戶名會話

我的表:

CREATE TABLE `staff` (     
      `staffid` varchar(25) NOT NULL,   
      `password` varchar(25) DEFAULT NULL,  
      `staffname` varchar(50) DEFAULT NULL, 
      `staffemail` varchar(50) DEFAULT NULL, 
      `level` int(2) DEFAULT NULL,    
      PRIMARY KEY (`staffid`)     
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

的index.php:

<?php require_once('Connections/sqlconnection.php'); 

//initialize the session 
if (!isset($_SESSION)) { 
    session_start(); 
} 
$colname_rsstaff = $_SESSION['staffid']; 
if (isset($_GET['staffid'])) { 
    $colname_rsstaff = $_GET['staffid']; 
} 

// ** Logout the current user. ** 
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; 
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ 
    $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); 
} 

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ 
    //to fully log out a visitor we need to clear the session varialbles 
    $_SESSION['MM_Username'] = NULL; 
    $_SESSION['MM_UserGroup'] = NULL; 
    $_SESSION['PrevUrl'] = NULL; 
    unset($_SESSION['MM_Username']); 
    unset($_SESSION['MM_UserGroup']); 
    unset($_SESSION['PrevUrl']); 

    $logoutGoTo = "login.php"; 
    if ($logoutGoTo) { 
    header("Location: $logoutGoTo"); 
    exit; 
    } 
} 
?> 
<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ?  mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 

mysql_select_db($database_sqlconnection, $sqlconnection); 
$query_rsstaff = sprintf("SELECT staffname FROM staff WHERE staff.staffid = %s",  GetSQLValueString($colname_rsstaff, "text")); 
$rsstaff = mysql_query($query_rsstaff, $sqlconnection) or die(mysql_error()); 
$row_rsstaff = mysql_fetch_assoc($rsstaff); 
$totalRows_rsstaff = mysql_num_rows($rsstaff); 
?> 
<title>Sistem Pengurusan Stok</title> 
<center> 
    <form name="form1" method="POST" action=""> 
    <table width="633" height="431" border="1"> 
     <tr> 
     <td height="124" colspan="2" align="right"><?php include 'header.php'?> 
<p><?php echo date("d-m-y")."<br>";?></p></td> 
     </tr> 
     <tr> 
     <td width="167" height="262"><?php include 'menu.php'?></td> 
     <td width="450" align="center"><p>Welcome 
      <?php echo $rsstaff?> 
</p> 
     <p><a href="<?php echo $logoutAction ?>">Log out </a></p> 
     <p><a href="<?php echo $logoutAction ?>"> </a></p></td> 
    </tr> 
    <tr> 
     <td height="37" colspan="2"></td> 
    </tr> 
    </table> 
    </form> 
</center> 
<? session_destroy()?> 
<?php 
mysql_free_result($rsstaff); 
?> 

有人幫我請... #Thanks。

+3

[**請不要在新代碼'mysql_ *'功能**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-02-21 02:21:15

回答

1

當你做<?php echo $rsstaff?>,你打印出MySQL查詢資源$rsstaff。這種輸出沒有明顯的形式,所以PHP只是告訴你「這是一種資源」,而不是更有意義的東西。這是你看到的輸出。

您打算打印出$row_rsstaff的元素之一,推測是"staffname"元素。

所以:

<?php echo $row_rsstaff["staffname"] ?> 
+0

非常感謝。這是工作!!!! :D – 2013-02-21 02:39:19

+0

@Nurul:請在出口時填寫客戶滿意度表。 – 2013-02-21 02:41:04

1

不要使用MYSQL_ *它已被棄用

這個答案是根據你試圖但是做什麼。

信息在這裏找到:mysql_query

當你做$rsstaff = mysql_query($query_rsstaff, $sqlconnection),你得到的是一個資源,而不是你對SELECT staffname FROM staff WHERE staff.staffid = %s對數據庫的期望。

嘗試以下操作:

$result = mysql_query($query_rsstaff, $sqlconnection); 
$rsstaff = mysql_fetch_assoc($result); 

然後

Welcome <?php echo $rsstaff["staffname"]; ?> 

或者也可以簡單

$rsstaff = mysql_result(mysql_query($query_rsstaff),0); 

然後用你的代碼。

+0

好的,但是你改變他的變量名的方式是相當混亂的。 – 2013-02-21 02:33:31

+0

但是這個代碼是由軟件提供的..我只是單擊Dreamweaver中的記錄集,它被編碼..每當我做了一個變化,它會提醒JavaScript有錯誤..所以如何? – 2013-02-21 02:44:04