Insertar en unir tablas Mysql

I've seen several questions and answers about this, but none is quite similar to my problem, so now I am asking.

Esta es la situación

Tengo una mesa llamada users, roles y user_role

mesa: users

campos: user_id, username, password

mesa: roles

campos: role_id, role_name

mesa: user_role

campos: user_id, role_id

clave extranjera of user_role.user_id is user.user_id

clave extranjera of user_role.role_id is role.role_id

My problem is when I create a user, I also want to set their roles to a default value or a selected value I chose in <select> elemento.

I tried several things like:

INSERT INTO users as t1 (t1.username, t1.password, t2.role)
JOIN user_role as t2 ON t1.user_id = t2.user_id
VALUES (:username, :password, :role)

Am I doing it super wrong?

EDIT BECAUSE OF AN ANSWER BELOW

I think something is still wrong, it gives me a blank screen

$this->db->beginTransaction();
$string = "INSERT INTO users (username, password)
VALUES (:user,:pass,:first,:middle,:last,:course)";
$sth = $this->db->prepare($string);
$sth->execute(array(
':user'=> $data['user'],
':pass'=> $data['pass']
));

$sth = $this->db->prepare("INSERT INTO user_roles (user_id, role_id)
VALUES (' . $this->db->lastInsertId(user_id) . ', :role)");
$sth->execute(array(
':role' => 3
));

$this->db->commit();

preguntado el 29 de julio de 12 a las 08:07

if i add another insert like insert INto user_role VALUES (x,y) i cannot set parameter x, bec. user_role.user_id hasn't been created yet. -

What language do you use for your server side ? -

using triggers would be a better option. -

1 Respuestas

Since it seems like you're using PHP & PDO, this simplified code will take care of your insert:

$pdo->beginTransaction();
$stmt = $conn->prepare('INSERT INTO users (username, password) VALUES (?, ?)');
$stmt->execute(array($username, $password));

$stmt = $conn->prepare('INSERT INTO user_roles (user_id, role_id) VALUES (' . $pdo->lastInsertId() . ', ?)');
$stmt->execute(array($role));
$pdo->commit();

After you insert a user into the users table, the value returned by $pdo->lastInsertId() es la id that was just inserted into the users mesa.

Lea más sobre la últimoInsertId().

Encase these two insert statements in a transaction and you're good to go.

Respondido 29 Jul 12, 08:07

wow,i haven't check that part of pdo.. it's been only a day since started pdo.., let me try to understand that - Joey Hipólito

i think something is still wrong, it gives me a blank screen $this->db->beginTransaction(); $string = "INSERT INTO users (username, password) VALUES (:user,:pass,:first,:middle,:last,:course)"; $sth = $this->db->prepare($string); $sth->execute(array( ':user'=> $data['user'], ':pass'=> $data['pass'] )); $sth = $this->db->prepare("INSERT INTO user_roles (user_id, role_id) VALUES (' . $this->db->lastInsertId(user_id) . ', :role)"); $sth->execute(array( ':role' => 3 )); $this->db->commit(); - Joey Hipólito

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.