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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上