我正在尝试将文章和标题保存到我的数据库中。我在 mysqli 中使用 OOP 编程语言(getter、setter)。在这里,您可以从课堂上看到我的代码。
<?php
class Article {
private $m_sTitle;
private $m_sArticle;
public function __set($p_sProperty, $p_vValue)
{
switch($p_sProperty)
{
case "Title" :
$this -> m_sTitle = $p_vValue;
break;
case "Article" :
$this -> m_sArticle = $p_vValue;
break;
}
}
public function __get($p_sProperty)
{
$vResult = null;
switch($p_sProperty)
{
case "Title" :
$vResult = $this -> m_sTitle;
break;
case "Article" :
$vResult = $this -> m_sArticle;
break;
}
}
public function saveArticle()
{
include("connection.php");
$sSql = "INSERT INTO tblArticles
(titel,
article,
)
VALUES
('".mysqli_real_escape_string($mysqli, $this -> m_sTitle) . "',
'" .mysqli_real_escape_string($mysqli, $this -> m_sArticle) . "',
);";
if (!$mysqli -> query($sSql))
{
throw new Exception("Something went wrong");
}
}
}
?>
在这里您可以看到 php 文件中的代码。
if(isset($_GET['title'])=="")
{
//do nothing
}
else {
$titel = $_GET['title'];
$artikel = $_GET['content'];
include("assets/connection.php");
include("assets/article.class.php");
$article1 = new Article;
$article1 -> Title = $titel;
$article1 -> Article = $artikel;
$article1 -> saveArticle() ;
}
我收到此错误:致命错误:在 ../assets/article.class.php:48 中未捕获异常 'Exception' 并带有消息 'Something wrong' 堆栈跟踪:#0 ../index.php(52): Article- >saveArticle() #1 {main} 在第 48 行的 ..assets/article.class.php 中抛出(此行 $article1 = new Article;)
感谢您的回复!
通过替换查询(类型错误)解决了问题,您可以在这句话下方看到工作部分(查询)。
$sSql = "INSERT INTO tblArticles
(titel,
article
)
VALUES
('".mysqli_real_escape_string($mysqli, $this -> m_sTitle) . "',
'" .mysqli_real_escape_string($mysqli, $this -> m_sArticle) . "');";