最近我正在为joomla 3.1后端制作一个上传组件。 基于How to Save Uploaded File's Name on Database 我成功地将文件移动到硬盘驱动器, 但是我不能让更新查询根据上面张贴的帖子工作。Joomla 3.1数据库更新查询不起作用
我没有得到任何SQL错误和保存工作,但以某种方式忽略数据库部分。
我真的很希望我错过了一些明显的东西。 (顺便说一句,我不知道查询的Joomla方式非常好)
在phpmyadmin
下面的查询工作:
UPDATE hmdq7_mysites_projects
SET project_file = 'test'
WHERE id IN (3);
我曾尝试以下查询:
$id = JRequest::getVar('id');
$db =& JFactory::getDBO();
$sql = "UPDATE hmdq7_mysites_projects
SET project_file =' " . $filename. "'
WHERE id IN (".$id.");";
$db->setQuery($sql);
$db->query();
$colum = "project_file";
$id = JRequest::getVar('id');
$data = JRequest::getVar('jform', null, 'post', 'array');
$data['project_file'] = strtolower($file['name']['project_file']);
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__mysites_projects');
$query->set($column.' = '.$db->quote($data));
$query->where('id'.'='.$db->quote($id));
$db->setQuery($query);
$db->query();
这里是当前的代码:
class MysitesControllerProject extends JControllerForm
{
function __construct() {
$this->view_list = 'projects';
parent::__construct();
}
function save(){
// ---------------------------- Uploading the file ---------------------
// Neccesary libraries and variables
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
$path= JPATH_SITE . DS . "images";
// Create the gonewsleter folder if not exists in images folder
if (!JFolder::exists(JPATH_SITE . "/images" )) {
JFactory::getApplication()->enqueueMessage($path , 'blue');
}
// Get the file data array from the request.
$file = JRequest::getVar('jform', null, 'files', 'array');
// Make the file name safe.
$filename = JFile::makeSafe($file['name']['project_file']);
// Move the uploaded file into a permanent location.
if ($filename != '') {
// Make sure that the full file path is safe.
$filepath = JPath::clean(JPATH_SITE . "/images/" . $filename);
// Move the uploaded file.
JFile::upload($file['tmp_name']['project_file'], $filepath);
$colum = "project_file";
$id = JRequest::getVar('id');
$data = JRequest::getVar('jform', null, 'post', 'array');
$data['project_file'] = strtolower($file['name']['project_file']);
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__mysites_projects');
$query->set($column.' = '.$db->quote($data));
$query->where('id'.'='.$db->quote($id));
$db->setQuery($query);
$db->query();
}
// ---------------------------- File Upload Ends ------------------------
JRequest::setVar('jform', $data);
return parent::save();
}
解决:审查后http://stackoverflow.com/questions/14531603/update-record-in-database-using-jdatabase?rq=1。我编写了一些固定的测试值。原来查询是正确的,但查询中的$ data varible没有数据。 $ data ['project_file'] = strtolower($ file ['name'] ['project_file']);从第一部分删除数组和可变工作。 – user2617187