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();
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. 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
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!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.