2011-03-20 121 views
0

我已經得到了一段代碼來取回用戶名和密碼,看看它們是否匹配。我現在想要設置一個會話,使其與我在表中的「分值」相同,並與用戶名和傳遞位於同一行。可以做什麼?mysql - 從表格行中提取數據

<?php $username="asdin"; 
$password="1sdA2"; 
$database="a75sdting"; 
$pword = $_REQUEST['pword']; 
$uname = $_REQUEST['uname']; 
mysql_connect('mysqsdst.com',$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 
$query = mysql_query("SELECT * FROM `username` WHERE `password` = '$pword' AND `username` = '$uname'"); 
$exsists = 0; 

     WHILE($rows = mysql_fetch_array($query)){ 
     $exsists = 1; 
     break; 
     } 
      if ($exsists){ 
      $_SESSION['usern']=$uname; 
      $_SESSION['logged']=1; 

      header('Location: http://wwsdipts/logged2.php'); 


        } 



mysql_close(); 

?> 

我想設置$ _SESSION [「點」] = $行[分]我猜...但我不認爲這是正確的

+0

什麼是「分數」? – Prisoner 2011-03-20 02:48:19

回答

1
<?php 

// start session (required on every page that uses sessions 
session_start(); 

// db auth 
$username="asdin"; 
$password="1sdA2"; 
$database="a75sdting"; 

// user auth 
$pword = $_POST['pword']; // should use either $_POST or $_GET, NOT $_REQUEST 
$uname = $_POST['uname']; // should use either $_POST or $_GET, NOT $_REQUEST 

// open db connection 
$conn = mysql_connect('mysqsdst.com',$username,$password); 
@mysql_select_db($database,$conn) or die("Unable to select database"); 

// check user 
$query = mysql_query("SELECT * FROM `username` WHERE `password` = '$pword' AND `username` = '$uname'"); 

if(mysql_num_rows($query)){ 
    // user exists 
    $row = mysql_fetch_assoc($query); 
    $_SESSION['usern']=$uname; 
    $_SESSION['logged']=1; 
    header('Location: http://wwsdipts/logged2.php'); 
}else{ 
    header('Location: http://wwsdipts/login.php'); // take them back to login page if incorrect details 
} 

// close db connection 
mysql_close($conn); 

?> 

我收拾你的代碼一點,請看看筆記。這也是一文不值如下:

  1. 您應該使用某種針對SQL注入防護,如mysql_real_escape_string($_POST['uname']) - 爲密碼相同
  2. 您需要在使用會話變量的所有頁面的session_start()
  3. 您不應該使用$ _REQUEST,使用$ _POST或$ _GET(請閱讀它)
  4. 您是否確實有一個名爲username的表?你應該閱讀一些關於數據庫設計的知識,對於這張表來說,更好的名字/用法將會是用戶,因爲表格將持有用戶(唯一ID,用戶名&密碼的組合)
  5. 我不知道你的意思關於點,但要訪問「用戶名」表中的任何列名稱,設置後使用$row['column-name']$row = mysql_fetch_assoc($query);
  6. 如果您打算將來使用PHP很多,您應該查找PDO,這是一個很好的類用於處理SQL
0

你是對的,但在這種情況下,您的陣列是rows,它應該是在

$_SESSION['points'] = $rows['points'] 

它應該是在你的while循環:

WHILE($rows = mysql_fetch_array($query)){ 
    $exsists = 1; 
    $_SESSION['points'] = $rows['points'] 
    break; 
    } 

但是,它可能是更好的做這樣的事情:

if(mysql_num_rows($result) == 1) { 
    //Login Successful 
    rows = mysql_fetch_assoc($result); 
    $_SESSION['points'] = $rows['points'] 
    $_SESSION['usern']=$uname; 
    $_SESSION['logged']=1; 

    header('Location: http://wwsdipts/logged2.php'); 
}