简体   繁体   中英

How can i insert into database in joomla?

How do I insert into database?

function create(){
    $db=JFactory::getDbo();
    $query=$db->getQuery(true);
    $query="INSERT INTO '#__mycomp_posts' (`title`,`body`) VALUES         ('First Post','This is enter code herepost body')";
    $db->setQuery($query);
    $db->$query();
}

You should really have done a little research before asking your question. I've taken your code and converted it to up to date Joomla coding standards:

function create(){
   $db = JFactory::getDbo();
   $query = $db->getQuery(true);
   $columns = array('title', 'body');
   $values = array($db->quote('First Post'), $db->quote('This is enter code herepost body'));

   $query->insert($db->quoteName('#__mycomp_posts'))
         ->columns($db->quoteName($columns))
         ->values(implode(',', $values));

   $db->setQuery($query);
   $db->query();
}

You then need to call this function to execute the code

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