我正在尝试将文章和标题保存到我的数据库中。我正在使用带有mysqli的OOP编程语言(getter,setters)。在这里,您可以看到我的代码。
<?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中显示消息“出错”的未捕获异常'异常'堆栈跟踪:#0 ../index.php(52) :Article-&gt; saveArticle()#1 {main}在第48行的..assets / article.class.php中抛出(此行$ article1 =新文章;)
感谢您的回复!
通过替换查询(类型错误)解决了问题,您可以在此句子下面看到工作部分(查询)。
$sSql = "INSERT INTO tblArticles
(titel,
article
)
VALUES
('".mysqli_real_escape_string($mysqli, $this -> m_sTitle) . "',
'" .mysqli_real_escape_string($mysqli, $this -> m_sArticle) . "');";
答案 0 :(得分:0)
在您的查询中article
之后有一个不需要的逗号:
INSERT INTO tblArticles
(titel,
article,) VALUES ...