2013-03-07 75 views
0
<?php 
class RegisterUser{ 
    var $email; 
    var $password; 
    var $password_hash; 
    var $roleid; 
    public function __construct($email, $password, $roleid){ 
    $this->email = $email; 
    $this->password = $password; 
    $this->roleid = $roleid; 
    $this->addUsertoDB(); 
    } 

    public function addUsertoDB(){ 
    $this->password_hash = md5($this->password); 
    $checkemail = mysql_query("SELECT * FROM users WHERE (email ='" . mysql_real_escape_string($this->email) . "')"); 
    if(mysql_num_rows($checkemail) == 1){ 
     echo '<script type="text/javascript"> alert ("Email already used!");</script>'; 
    } 
    else{ 
     $register = "INSERT INTO users(email, password, roleid) VALUES('$this->email', '$this->password_hash', '$this->$roleid')"; 
     if($query_run = mysql_query($register)){ 
     echo('<scrip type="text/javascript"> alert("Registration complete!"); location.replace("login")</script>'); 
     } 
     else{ 
     die('<script type="text/javascript"> alert("Error inserting data!");</script>'); 
     } 
    } 
    } 
} 

$connect = new RegisterUser('[email protected]', 123, 1); 
?> 

這是我的代碼,我不斷得到一個錯誤,我不知道如何弄清楚。我希望你們做到了。傳遞值到類

這是錯誤。

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\myPHPWebsite\include\classRegister.php on line 17

Catchable fatal error: Object of class RegisterUser could not be converted to string in C:\xampp\htdocs\myPHPWebsite\include\classRegister.php on line 21

更新

<?php 
require 'connectDB.php'; 
class RegisterUser{ 
    var $email; 
    var $password; 
    var $password_hash; 
    var $roleid; 
    public function __construct($email, $password, $roleid){ 
    $this->email = $email; 
    $this->password = $password; 
    $this->roleid = $roleid; 
    $this->addUsertoDB(); 
    } 

    public function addUsertoDB(){ 
    $this->password_hash = md5($this->password); 
    $checkemail = mysql_query("SELECT * FROM users WHERE (email ='" . mysql_real_escape_string($this->email) . "')"); 
    if(mysql_num_rows($checkemail) == 1){ 
     echo '<script type="text/javascript"> alert ("Email already used!");</script>'; 
    } 
    else{ 
     $register = "INSERT INTO users(email, password, roleid) VALUES('$this->email', '$this->password_hash', '$this->$roleid')"; 
     if($query_run = mysql_query($register)){ 
     echo('<scrip type="text/javascript"> alert("Registration complete!"); location.replace("login")</script>'); 
     } 
     else{ 
     die('<script type="text/javascript"> alert("Error inserting data!");</script>'); 
     } 
    } 
    } 
} 

$connect = new RegisterUser('[email protected]', 123, 1); 
?> 

這是錯誤我得到

Connected to Database! Catchable fatal error: Object of class RegisterUser could not be converted to string in C:\xampp\htdocs\myPHPWebsite\include\classRegister.php on line 22

回答

0

第一個錯誤是由什麼是錯在你的MySQL。嘗試在查詢後添加
if($e = mysql_error()) die("MySQL error: ".$e);

另外,您的可捕捉的致命錯誤是由$this->roleid錯誤地寫爲$this->$roleid造成的。另外請注意,您無法在該行上轉義字符串,因此您很容易被注入。這是不夠的,只有確保第一次查詢時,你有充分的時間去做;)

+0

我更新了問題 – user2139960 2013-03-07 02:15:19

+0

您沒有更正我告訴您的任何一件事... – 2013-03-07 02:22:54

0

形式,你給我們

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in 
C:\xampp\htdocs\myPHPWebsite\include\classRegister.php on line 17 

好像$checkmail值無效mysql_num_rows()結果的誤差。檢查你的查詢,並嘗試在你的數據庫控制檯上運行它(如果你使用的話,可以使用phpmyadmin)。

public function addUsertoDB(){ 
    ... 
    $query = "SELECT * FROM users WHERE (email ='" . mysql_real_escape_string($this->email) . "')"; 
    checkemail = mysql_query($query) or die(mysql_errno() . " : " . mysql_error() . " : " . $query); 
    ...