dtcpvz8162 2015-08-30 18:15
浏览 48
已采纳

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

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).

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.

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();
  • 写回答

2条回答 默认 最新

  • dongwei3336 2015-09-07 15:20
    关注

    I finally got it - thanks to all for help:

    For Joomla 3.x

    Update a column/field in MySQL-Database via php Display column/field from a MySQL-Database via php

    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!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统