簡體   English   中英

將當前頁面,ID,日期插入到MySQL表中

[英]Insert current page, id, date into mySQL table

在我的Joomla 2.5.14中,我試圖將當前的page_id,user_id和date插入到mySQL表(稱為xmb9d_hist)中。

這是我正在使用的代碼:

<?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);

?>

代碼的第一部分工作正常(檢索3個變量的值。但是,數據未插入數據庫中,並且不會產生錯誤。

在MySQL表中,user_id和page_id定義為INT(11),日期定義為日期。

在此先感謝您的幫助。

使用Joomla時,您需要遵守Joomla編碼標准,除非它們沒有為您需要的內容提供類。 在嘗試某些操作之前,請先閱讀文檔,以防萬一有相關信息。

嘗試使用以下內容:

<?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);

?>

希望這可以幫助

我發現您的插入查詢存在一些問題。 首先,我相信JFactory::getDate()返回一個對象,因此要獲取某些內容,您可以將其插入數據庫中,然后對其執行toFormat() 嘗試從以下位置更改線路:

$date =JFactory::getDate();

對此:

$date =JFactory::getDate()->toFormat();

而且由於日期不是整數,因此您需要對插入內容中的字符串進行轉義,因此將該行更新為:

mysqli_query($link, "INSERT INTO `portalge_formacao`.`xmb9d_hist` (`user_id`, `page_id`, `date`) VALUES ($usr_id, $page, '$date')");

您應該使用toSql()方法,它用於SQL例如

$date = JFactory::getDate()->toSql();

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM