简体   繁体   English

joomla 3x将字符串插入sql数据库

[英]joomla 3x insert a string into sql database

I'm currently running a joomla 3x portal and need to insert a string into my MySQL database, but have had no success so far (sadly, I'm not a programmer). 我目前正在运行joomla 3x门户,需要在我的MySQL数据库中插入一个字符串,但是到目前为止还没有成功(遗憾的是,我不是程序员)。

So I added a new field into my users table called "ok1" and just want to insert a string into it (actually a hyperlink) and overwrite it every time with the current one on the page that executed the code last. 因此,我在用户表中添加了一个名为“ ok1”的新字段,只想在其中插入一个字符串(实际上是一个超链接),然后每次使用最后执行代码的页面上的当前字段覆盖它。

This is the code I used, but it always "blows" my portal... 这是我使用的代码,但是它总是使我的门户网站“炸毁”。

$query = $db->getQuery(true);

$columns = "ok1"; 
$values = "my hyperlink";

$query
    ->insert($db->quoteName('#__users'))
    ->columns($db->quoteName($columns))
    ->values($db->quoteName($values));

$db->setQuery($query);
$db->execute();

$query = $db->getQuery(true);

$db->setQuery($query);
$db->execute();

Hopefully somebody can help me out with this one, thanks in advance. 希望有人能帮助我解决这个问题,在此先感谢。

Second attempt: 第二次尝试:

$content ="test"
$query = $db->getQuery(true);
Update `x__users` SET `ok1`=$content WHERE `user` = $user->id;
$db->setQuery($query);
$result = $db->execute();

What you want is something like this 你想要的是这样的

$columns = "ok1"; 
$values = "my hyperlink";

$query
    ->update($db->quoteName('#__users'))

    ->set($db->quoteName($columns) = $db->quote($values))
    ->where($db-quoteName('user') = $user->id)
;

$db->setQuery($query);
$db->execute();

That is to say you want to update a record not insert a new one, and you want it to be the specific record indicated by the where clause. 也就是说,您要更新记录而不插入新记录,并且希望它成为where子句指示的特定记录。 Also use the correct quote for values (since they are not names). 还请对值使用正确的引号(因为它们不是名称)。

I finally got it - thanks to all for help: 我终于明白了-感谢大家的帮助:

For Joomla 3.x 对于Joomla 3.x

Update a column/field in MySQL-Database via php Display column/field from a MySQL-Database via php 通过php更新MySQL数据库中的列/字段通过php显示MySQL数据库中的列/字段

To get the Data into the Database use following Code: 要将数据放入数据库,请使用以下代码:

// $columns represent the name of the column/filed in the table
// $values represent the value the column/filed should be updated with
$columns = "ok1"; 
$values = "my hyperlink";

$query = $db->getQuery(true);
$query
    ->update($db->quoteName('#__users')) //the name of the table
    ->set($db->quoteName($columns) . ' = ' . $db->quote($values)) 
    ->where($db->quoteName('id') . ' = ' . $user->id) //the current user ID to match the column "id" 
;

$db->setQuery($query);
$db->execute();

To get the Data out of the Database use following Code: 要从数据库中获取数据,请使用以下代码:

$query= "SELECT `ok1` FROM `#__users` WHERE `id` = $user->id"; 
$database->setQuery($query);
$ok1_link= $database->loadResult();

I use und recommend a great extension called Sourcerer to place the code direct into an article - works flawlessly basic version is for free and support is also top!! 我使用und推荐一个伟大的扩展程序Sourcerer,以将代码直接放入文章中-完美无瑕的基本版本是免费的,支持也是最高的!

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

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