好的,所以我的目标是练习一些OOP。如果这个课程对我想要完成的事情有点过分,请告诉我,但我只是为了练习而做。基本上我有一个非常基本的表格,它所做的只是取名字,电子邮件,以及来自他们的评论或消息,并将其上传到数据库。我知道(实际上不知道,只是很确定)它是类的uploadMessage函数。想知道是否有人能告诉我这里出错了。谢谢! 首先是消息类 -
class message{
public $name;
public $email;
public $message;
function __construct($name, $email, $message){
$this->name = $name;
$this->email = $email;
$this->message = $message;
}
public function uploadMessage(){
if(!isset($this->name) || !isset($this->email) || !isset($this->message)){
throw new Exception("One of the fields was incorrectly typed.");
} else{
$database = new DB("localhost","username","password","Personal_blog");
$this->name = mysqli_real_escape_string($database->_db,$this->name);
$this->email = mysqli_real_escape_string($database->_db,$this->email);
$this->message = mysqli_real_escape_string($database->_db,$this->message);
$query = "INSERT INTO messages(name,email,message) VALUES('".$this->name."', '".$this->email."', '".$this->message."')";
$results = $database->query($query);
if($results->affected_rows > 0){
return true;
}
}
}
}
下面是我用来实例化类的代码,并且还调用了方法。
ini_set("display_errors",1);
include_once "blog_functions.php";
@ $name = "eric" ; //$_POST['name'];
@ $email = "email"; //$POST['email']; USING FAKE VALUES HERE FOR TESTING
@ $message = "message"; //$_POST['message'];
echo "hey";
try{
$message = new message($name, $email, $message);
$message->uploadMessage();
}
正如你所看到的,我想通过$ _POST方法从表单中获取值,但是只是对于某些测试我给它添加了一些随机变量值,它不起作用。新的DB()方法来自另一个基本上只是扩展php mysqli库的类。而关闭的$ database-> _db属性实际上是mysqli处理程序,这是我用于mysqli_real_escape_string中的引用。
答案 0 :(得分:0)
尝试:
$query = "INSERT INTO messages(`name`,`email`,`message`) VALUES('".$this->name."', '".$this->email."', '".$this->message."')";