2015-02-11 51 views
0

此代碼包含在擁有全部功能,SQL的SQL.php文件:我在做一個項目的大學,我們必須創建一個博客

功能check_login($ USER_NAME,$密碼){

#create the PDO object 
/** 
* Used to instanciate the host of the server 
* @var string 
*/ 
    $hostname = 'localhost'; 
      /** 
* Used to instanciate the username to connect to the server 
* @var string 
*/ 
    $username = 'ODBC'; 
      /** 
* Used to instanciate the password to connect to the server 
* @var string 
*/ 
    $pass = ""; 
      /** 
* Used to instanciate the database name 
* @var string 
*/ 
    $db_name = 'bloggie_db'; 
    try{ 
     mysql_query("SET NAMES 'utf8"); 
     $dbh = new PDO("mysql:host=$hostname;dbname=$db_name" , $username, $pass); 

     #set PDO error mode to exception 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     #Check to see if the user exists 
     $stmt = $dbh->query("SELECT password, username, firstname, surname FROM users WHERE username = '" . $user_name . "'"); 

     $row = $stmt->fetch(PDO::FETCH_ASSOC); 

     $check_username = $row['username']; 
     $check_password = $row['password']; 
     $name = $row['firstname']; 
     $surname = $row['surname']; 

     if ($row){ 
      if($check_password=$password & $check_username=$user_name){ 
       header("Content-Type: text/html; charset=utf-8"); 
       echo 'pass check= ' . $check_password . ' password= '. $password . ' firstname= ' . $name . ' surname= ' . $surname; 
       return array($name, $surname); 
      }else{ 

       echo "Your details are invalid."; 
       return false; 
      } 
     }else{ 
      echo "Your account does not exist"; 
     } 
    }catch(PDOException $e){ 
     $e->getTrace(); 
    } 
    $dbh = null; 
} 

當密碼打印,我得到含gaves如「dapb`」當值實際上應該是一個奇怪的值6544.

在login.php中我調用SQL函數:

/** 
* Requesting the users username from Bloggie_Welcome.php 
* @var string 
*/ 
$username = $_REQUEST['Email']; 

/** 
* Retrieving the users password from Bloggie_Welcome.php 
* @var string 
*/ 
$password = $_REQUEST['Password']; 

#Including the path to the validation object 
include '../Objects/SQL.php'; 
#Instanciating the SQL object 
$sql = new SQL(); 
#Calling the sql function check_login 
$details = $sql->check_login($username, $password); 
session_start(); 
if(isset($details)){ 
    $_SESSION['username'] = $username; 
    $_SESSION['firstname'] = $details[0]; 
    $_SESSION['surname'] = $details[1]; 
    //header("Location: ../Bloggie_Profile.php"); 

} 

我似乎不明白爲什麼當我打印的用戶名,密碼,名字和姓氏,所有的數據是正確的,除了密碼。用戶表的

創作: 功能create_Users(){

#create the PDO object 
/** 
* Used to instanciate the host of the server 
* @var string 
*/ 
    $hostname = 'localhost'; 
      /** 
* Used to instanciate the username to connect to the server 
* @var string 
*/ 
    $username = 'ODBC'; 
      /** 
* Used to instanciate the password to connect to the server 
* @var string 
*/ 
    $password = ""; 
      /** 
* Used to instanciate the database name 
* @var string 
*/ 
    $db_name = 'bloggie_db'; 
    try{ 

     $dbh = new PDO("mysql:host=$hostname;dbname=$db_name" , $username, $password); 

     echo "<br/>Database connected <br/>"; 

     #set PDO error mode to exception 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     #create users table 
     $sqlU = 'CREATE TABLE users('. 
        'user_id INT NOT NULL AUTO_INCREMENT,'. 
        'firstname VARCHAR(50),'. 
        'surname VARCHAR(50),'. 
        'username VARCHAR(100),'. 
        'password VARCHAR(255),'. 
        'contact_num VARCHAR(10),'. 
        'email VARCHAR(100),'. 
        'gender VARCHAR(50),'. 
        'DOB DATE,'. 
        'profile_path VARCHAR(200),'. 
        'bio VARCHAR(255),'. 
        'PRIMARY KEY(user_id))'; 
     $dbh->exec($sqlU); 
     echo "<br/> Users table dropped."; 

     $dbh = null; 
    }catch(PDOException $e){ 
     echo "<br/>" . $e->getMessage() . "<br/>"; 
     die(print_r($e->getTrace())); 
    } 
} 

請幫我GUYS :(

+0

歡迎來到Stack Overflow。請花點時間閱讀[常見問題#hotwoask]並儘量縮短您的問題。這不是一個代碼寫入服務。嘗試提出具體問題並描述你所做的事情。 – simbabque 2015-02-11 13:13:39

+0

好..他問的是爲什麼密碼打印一個奇怪的值..只是有點埋在所有的代碼.. – Randy 2015-02-11 13:19:00

回答

1

我認爲你做的最常見的編程錯誤,只是拼寫錯誤操作英寸:

if($check_password=$password & $check_username=$user_name){ 

這就是爲什麼你第一次分配$chack_password$password,做一個二進制(&)運營商(不是&&(AND))與$user_namepassword現在返回一個不同的值。

只是修復,如果它應該是好的。

+0

謝謝這麼多,我一直在掙扎幾天,我真的是一個白癡哈哈,我真的欣賞它。 – Celeste 2015-02-13 07:58:06

相關問題