简体   繁体   English

Joomla JDatabase查询插入空行

[英]Joomla JDatabase Query inserts empty row

I am developing a simple guestbook for Joomla 1.7. 我正在为Joomla 1.7开发一个简单的留言簿。

If I try to insert a new message JDatabase inserts a empty row and I don't know why. 如果我尝试插入新消息,则JDatabase会插入一个空行,但我不知道为什么。

Here the table structure 这里的表结构

  DROP TABLE IF EXISTS `#__agb_messages`;

  CREATE TABLE `#__agb_messages` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NOT NULL,
  `message` TEXT NOT NULL,
  PRIMARY KEY (`id`)
  );

Here the code i am using to insert the new row and load the existing rows 这是我用来插入新行并加载现有行的代码

    public function getMessages() {

    $db = JFactory::getDBO();

    echo "<pre>";
    print_r(JRequest::get('post'));
    echo "</pre>";

    $post = JRequest::get('post');

    if (
            !empty($post['name'])
         && !empty($post['message'])
            )
    {
        $query = 'INSERT INTO #__agb_messages SET name="'.(string)$post['name'].'" AND message="'.(string)$post['message'].'"';
        $db->setQuery($query);
        echo $db->getQuery();
        $db->query();
        echo $db->getErrorMsg();
    }

    $query = 'SELECT * FROM #__agb_messages ORDER BY id DESC';
    $db->setQuery($query);
    $messages = $db->loadObjectList();

    return $messages;
}

This is what i get 这就是我得到的

  Array
  (
  [0] => stdClass Object
    (
        [id] => 1
        [name] => 0
        [message] => 
    )
  )

hi check the inser query format... 嗨,请检查插入查询格式...

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

This is the correct syntax for INSERT query. 这是INSERT查询的正确语法。

  • Don't confuse with Update Query format :) 不要混淆更新查询格式:)

Consider also using the JDatabase class from Joomla to do the insert ;) 还考虑使用Joomla的JDatabase类进行插入;)

public function insertObject (
        $table
        &$object
        $keyName=NULL
)

http://docs.joomla.org/JDatabase::insertObject/1.6 http://docs.joomla.org/JDatabase::insertObject/1.6

I guess that it's also available for Joomla 1.7 我想Joomla 1.7也可以使用

扩展Jtable并使用Joomla API中的内置方法,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM