好的,所以我的目标是练习一些 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 中用于引用的。