【问题标题】:Error saving variables into a database将变量保存到数据库时出错
【发布时间】:2012-05-17 13:12:56
【问题描述】:

我正在尝试将一篇文章和标题保存到我的数据库中。我正在使用带有 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 中出现消息“出现问题”堆栈跟踪:#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) . "');";

【问题讨论】:

  • 可以发一下tblArticles表的结构吗?

标签: php database mysqli


【解决方案1】:

在您的查询中article 后面有一个不需要的逗号:

INSERT INTO tblArticles
            (titel, 
            article,) VALUES ...

【讨论】:

  • 你是我的男人!有一个 , 到你说的很多地方,也在我的查询末尾!现在工作正常。非常感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
相关资源
最近更新 更多