I’m trying to save a article and title into my database. I’m using OOP programming language (getters, setters) with mysqli. Here you can see my code from the class.
<?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");
}
}
}
?>
Here you can see the code in the php file.
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() ;
}
I receive this error: Fatal error: Uncaught exception ‘Exception’ with message ‘Something went wrong’ in ../assets/article.class.php:48 Stack trace: #0 ../index.php(52): Article->saveArticle() #1 {main} thrown in ..assets/article.class.php on line 48 (this line $article1 = new Article;)
Thanks for the response!
Problem solved with replacing a query(type errors), you can see the working part (query) below this sentence.
$sSql = "INSERT INTO tblArticles
(titel,
article
)
VALUES
('".mysqli_real_escape_string($mysqli, $this -> m_sTitle) . "',
'" .mysqli_real_escape_string($mysqli, $this -> m_sArticle) . "');";
There’s an unwanted comma after
articlein your query: