问题
I am beginner in joomla development. I know PHP, MySql. But I want to insert data into database (phpmyadmin) in joomla. how to do it in a joomla article. Thanks
回答1:
You can run insert query in joomla in this way.
$db =& JFactory::getDBO();
$query = "INSERT INTO '#__example_table' ('name','email','username')
VALUES ('John Smith','johnsmith@domain.example','johnsmith')";
$db->setQuery($query);
$db->query();
回答2:
Another way of insert query.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
Get column names to a variable as array.
$columns = array('fieldname1','fieldname2');
$values = array(1,$db->quote('my value'),$db->quote('my message');
Insert query
$query->insert($db->quoteName('#__tablename'))
->columns($db->quoteName($columns)
->values(implode(',',$values));
Set the query
$db->setQuery($query);
$db->execute();
来源:https://stackoverflow.com/questions/10757084/how-to-insert-data-into-database-at-joomla-article