简体   繁体   English

Joomla3:手动更新数据库中的表

[英]Joomla3: manually updating a table in the database

I've got many articles that I need to copy to a joomla+k2 site, I don't want to go to backend and add them one by one, so I am now trying to directly update the databse, partly for learning purpose. 我有很多文章需要复制到joomla + k2网站,我不想去后端并逐个添加它们,所以我现在正尝试直接更新数据库,部分出于学习目的。

I've created a k2 item and duplicated it until I have enough fake items, this way I just need to update the title and introtext and ignore other fields, and I've prepared the articles in JSON. 我创建了一个k2项并进行了复制,直到有足够多的假项为止,这样,我只需要更新titleintrotext并忽略其他字段,就可以用JSON准备文章。

Now my first question is, where is the best place to put this .PHP file that is going to manipulate the databse? 现在,我的第一个问题是,放置将要操纵数据库的.PHP文件的最佳位置在哪里? It has to have the database connection ready and not load the page. 它必须已准备好数据库连接并且不加载页面。 Currently I am using a copy of the index.php file under ROOT, having commented out $app->execute(); 目前,我在ROOT下使用index.php文件的副本,已注释掉$app->execute(); so it won't actually load the page. 因此它实际上不会加载该页面。 Although the database connection is good(I successfully "select" data from the table), it feels very awkward, and also I can't use the developer plugins like "j!dump". 尽管数据库连接很好(我成功地从表中“选择”了数据),但是感觉很尴尬,而且我不能使用“ j!dump”之类的开发人员插件。

Secondly and more importantly , my code doesn't work: I copy the code from this documentation and make as little change as I can, but it doesn't work, the $result is true but the table isn't updated at all. 其次,更重要的是 ,我的代码不起作用:我从本文档中复制了代码,并进行了尽可能少的更改,但它不起作用, $resulttrue但表根本没有更新。 With very little knowledge about joomla coding and database, it is impossible for me to debug it by myself. 由于对joomla编码和数据库了解甚少,所以我自己无法调试它。

define('_JEXEC', 1);

if (file_exists(__DIR__ . '/defines.php'))
{
    include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';

// Set profiler start time and memory usage and mark afterLoad in the profiler.
JDEBUG ? $_PROFILER->setStart($startTime, $startMem)->mark('afterLoad') : null;

// Instantiate the application.
$app = JFactory::getApplication('site');

//mycode
$db = JFactory::getDbo();
$query = $db->getQuery(true);

$fields = array(
    $db->quoteName('title') . ' = ' . $db->quote('asdfasdf')
);

$conditions = array(
    $db->quoteName('id') . ' = 2'
);

$query->update($db->quoteName('#__k2_items'))->set($fields)->where($conditions);
$result = $db->execute();

There is no error and $db->execute() returns true, please teach me when this happens how to debug? 没有错误,并且$db->execute()返回true,请教我发生这种情况时如何调试?

It is OK to place the file at the same level of the index.php file, but make sure you add some kind of hash to access the file via a browser. 可以将文件放置在与index.php文件相同的级别上,但是请确保添加某种哈希值以通过浏览器访问文件。 For example, the link should be www.yourdomain.com/youfile.php?hash=1234567890 例如,链接应为www.yourdomain.com/youfile.php?hash=1234567890

Providing a hash will prevent other people from running your file. 提供哈希将阻止其他人运行您的文件。

As for your query not working, try the following (there is no need to follow the strict Joomla query methods for a script like this): 至于您的查询无效,请尝试以下操作(对于这样的脚本,无需遵循严格的Joomla查询方法):

$db = JFactory::getDbo();
$sql = "Your SQL query";
$db->setQuery($sql);
$db->query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM