<?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
我更新了問題 – user2139960 2013-03-07 02:15:19
您沒有更正我告訴您的任何一件事... – 2013-03-07 02:22:54