[英]Insert current page, id, date into mySQL table
In my Joomla 2.5.14 I'm trying to insert the current page_id, user_id and date into a mySQL table (called xmb9d_hist). 在我的Joomla 2.5.14中,我试图将当前的page_id,user_id和date插入到mySQL表(称为xmb9d_hist)中。
This is the code I'm using: 这是我正在使用的代码:
<?php
/* Define $jinput */
$jinput = JFactory::getApplication()->input;
/* Get the current page id */
$page= $jinput->get('id');
/* Get the current user id */
$user =JFactory::getUser();
$usr_id = $user->get('id');
/* Get the current date */
$date =JFactory::getDate();
/* Open a connection */
$link=mysqli_connect('localhost','peter','abc123');
if(!$link){
echo "Não há ligação!";
exit();
};
/* Insert current user id, page id and date in to table xmb9d_hist */
mysqli_query($link, "INSERT INTO `portalge_formacao`.`xmb9d_hist` (`user_id`, `page_id`, `date`) VALUES ($usr_id, $page, $date)");
/* Close connection */
mysqli_close($link);
?>
The first part of the code is working ok (retrieving the values for the 3 variables. However, the data isn't being inserted in the database and no error is produced. 代码的第一部分工作正常(检索3个变量的值。但是,数据未插入数据库中,并且不会产生错误。
In MySQL table, user_id and page_id are defined as INT(11) and date as date. 在MySQL表中,user_id和page_id定义为INT(11),日期定义为日期。
Thanks in advance for your help. 在此先感谢您的帮助。
You need to stick to Joomla coding standards when using Joomla unless they have not provided a class for whatever you need. 使用Joomla时,您需要遵守Joomla编码标准,除非它们没有为您需要的内容提供类。 Have a read of the documentation before trying something just incase there is information about it. 在尝试某些操作之前,请先阅读文档,以防万一有相关信息。
Try using the following: 尝试使用以下内容:
<?php
$db = JFactory::getDbo();
$date = JFactory::getDate();
$user = JFactory::getUser();
$jinput = JFactory::getApplication()->input;
$page= $jinput->get('id');
$usr_id = $user->get('id');
$query = $db->getQuery(true);
$columns = array('user_id', 'page_id', 'date');
$values = array($usr_id, $page, $date);
$query
->insert($db->quoteName('#__hist'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
?>
Hope this helps 希望这可以帮助
I see a few issues with your insert query. 我发现您的插入查询存在一些问题。 First off, I believe that JFactory::getDate()
returns an object, so to get something you can insert into the database you want to do a toFormat()
on it. 首先,我相信JFactory::getDate()
返回一个对象,因此要获取某些内容,您可以将其插入数据库中,然后对其执行toFormat()
。 Try changing your line from: 尝试从以下位置更改线路:
$date =JFactory::getDate();
to this: 对此:
$date =JFactory::getDate()->toFormat();
And since the date isn't an integer, you need to escape the string in your insert, so update that line to this: 而且由于日期不是整数,因此您需要对插入内容中的字符串进行转义,因此将该行更新为:
mysqli_query($link, "INSERT INTO `portalge_formacao`.`xmb9d_hist` (`user_id`, `page_id`, `date`) VALUES ($usr_id, $page, '$date')");
You should use the method toSql() , it's for SQL eg 您应该使用toSql()方法,它用于SQL例如
$date = JFactory::getDate()->toSql();
hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.