简体   繁体   中英

Joomla JDatabase Query inserts empty row

I am developing a simple guestbook for Joomla 1.7.

If I try to insert a new message JDatabase inserts a empty row and I don't know why.

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.

  • Don't confuse with Update Query format :)

Consider also using the JDatabase class from Joomla to do the insert ;)

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

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

I guess that it's also available for Joomla 1.7

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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