Forums

What is wrong with this code?

tiagopc 12 Jan, 2016
I have this code that insert user and user group map in my database. But this doesn't work.
What's wrong with this:

<?php 
$db = &JFactory::getDBO(); 
$query = 
" INSERT INTO prefix_users (id,name,username,email,password) VALUES ('{$form->data['id']}','{$form->data['name']}','{$form->data['username']}','{$form->data['email']}','{$form->data['password']}'), INSERT INTO prefix_user_usergroup_map (user_id,group_id) VALUES ('{$form->data['id']}',6)"; 
$db->setQuery($query);
$db->query();
?>
GreyHead 12 Jan, 2016
Hi tiagopc,

Which version of Joomla! is this?

And is the table prefix really 'prefix' ?

Bob
tiagopc 12 Jan, 2016
this is the latest version of joomla, and my prefix table is not "prefix" is just an exemple.
GreyHead 12 Jan, 2016
Hi tiagopc,

For Joomla! 3 please use $db->execute() instead of $db->query()

Bob
tiagopc 13 Jan, 2016
What i want to do is two inserts in the same custom code, code i'm doing is below:

<?php 
$db = &JFactory::getDBO();
$idaux = $form->data['id'];
$query = "INSERT INTO table_users (id, name) VALUES ('{$form->data['id']}, {$form->data['name']}');
er_usergroup_map (user_id,group_id) VALUES ('{$form->data['id']}',6)"; 
$db->setQuery($query);
$db->query();
?>

This code is working, but i want to insert in this table at the same time:
INSERT INTO table_user_usergroup_map (user_id,group_id) VALUES ('{$form->data['id']}',6)"

Can you help me?
GreyHead 13 Jan, 2016
Hi tiagopc,

Please repeat the code again for the second query.

Bob
tiagopc 13 Jan, 2016
The second insert is:
INSERT INTO eweb_user_usergroup_map (user_id,group_id) VALUES ('{$form->data['id']}',6)"

The intention is to get the user id that i've registrated and associate it with the group id '6'. Is that clear?

And thanks a lot for the help, Bob!
GreyHead 13 Jan, 2016
Hi tiagopc,

Sorry, I meant that you should just add a second query to the PHP.
<?php 
$db = &JFactory::getDBO(); 
$query = 
" INSERT INTO prefix_users (id,name,username,email,password) VALUES ('{$form->data['id']}','{$form->data['name']}','{$form->data['username']}','{$form->data['email']}','{$form->data['password']}')"; 
$db->setQuery($query);
$db->execute();

$query = 
" INSERT INTO prefix_user_usergroup_map (user_id,group_id) VALUES ('{$form->data['id']}',6)"; 
$db->setQuery($query);
$db->execute();
?>


BUT you probably should not write directly to the Joomla! User tables. There are JUser methods you can use to do make these changes. You should certainly NEVER write a password like this.

Bob
tiagopc 13 Jan, 2016
Why shouldn't write password like this, Bob? Can you give me some advice?
GreyHead 13 Jan, 2016
Hi tiagopc,

Because Joomla! doesn't save the password in plain text. It saves a 'hash' that can be used to check the password but not to recover it.

Bob
tiagopc 13 Jan, 2016
So, how can i save this password as hash that will work properly in Joomla?
GreyHead 13 Jan, 2016
Hi tiagopc,

Please see the Joomla! docs for JUserHelper - there are some here and Google will find you other examples.

Bob
This topic is locked and no more replies can be posted.