<?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('carloadap@htomail.com', 123, 1);
?>
这是我的代码,我一直收到错误,我不知道如何弄清楚。我希望你们做到了。
这是错误。
警告:mysql_num_rows() 期望参数 1 是资源,布尔值在第 17 行的 C:\xampp\htdocs\myPHPWebsite\include\classRegister.php 中给出
可捕获的致命错误:RegisterUser 类的对象无法在第 21 行的 C:\xampp\htdocs\myPHPWebsite\include\classRegister.php 中转换为字符串
更新
<?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('carloadap@htomail.com', 123, 1);
?>
这是我得到的错误
连接到数据库!可捕获的致命错误:RegisterUser 类的对象无法在第 22 行的 C:\xampp\htdocs\myPHPWebsite\include\classRegister.php 中转换为字符串