Jump to content

Insert with joins?


patchido

Recommended Posts

Hello, i have a table i have to fill out with info regarding other tables, is a join possible or do i have to make 2 queries?
 
This is what i have right now.

$date = date("Y-m-d H:i:s");
			          
			$sql = "INSERT INTO tblpaqueteria (
							sender_id, reciever_id, AddTime, Estatus, ubicacion, tipo,slocalidad_id,rlocalidad_id
						) VALUES (
							'{$_POST['id_send']}', '{$_POST['id_rec']}', '{$date}', '0' , '0', '{$_POST['tipo_paq']}',0,0
						)";
						
						
			$res = mysql_query($sql,$this->conn);
					return $sql;

What i need now is to change some of my insert into dynamic ones, i have to enter into "slocalidad_id" and into "ubicacion" the "id" of a table called localidad, and this id is being referenced from a table called "usuarios" (users) where "$_POST['id_send']" where localidad_id marks the id i need to insert into tblpaqueteria.
 
 
I am not sure if I explained myself correctly so ill try and make an example.
 
usuarios
 


 
So for my sql above, without joins i would be receiving from  POST id_send  = 2    and from id_rec  11
 
 
so my query should insert this
$sql = "INSERT INTO tblpaqueteria (
							sender_id, reciever_id, AddTime, Estatus, ubicacion, tipo,slocalidad_id,rlocalidad_id
						) VALUES (
							'{$_POST['id_send']}', '{$_POST['id_rec']}', '{$date}', '0' , '2', '{$_POST['tipo_paq']}',2,4
						)";

notice the numbers "2" and "4" being inserted, those are the ones i don't know how to enter dynamically, i think its  a join, but i don't know how to use them in an insert.
 
 
Thanks a lot

Link to comment
https://forums.phpfreaks.com/topic/279612-insert-with-joins/
Share on other sites

the table won't show up in the forums :/

 

 

ill put part of it then.

 

 

id username nombre       ap_Paterno     ap_Materno    mail                             region_id localidad_id admin

2   Qualtia     prueba        prueba           prueba            [email protected]   1               2                 3

11 romtorre ROMELIA       TORRES        VELI               [email protected] 1              4                  2

Link to comment
https://forums.phpfreaks.com/topic/279612-insert-with-joins/#findComment-1438104
Share on other sites

You could do a sub-query where necessary. Something like this if I am understanding right:

$sql = "
INSERT INTO tblpaqueteria (
	sender_id, 
	reciever_id, 
	AddTime, 
	Estatus, 
	ubicacion, 
	tipo,
	slocalidad_id,
	rlocalidad_id
) VALUES (
	'{$_POST['id_send']}', 
	'{$_POST['id_rec']}', 
	'{$date}', 
	'0' , 
	(SELECT localidad_id FROM usuarios WHERE id={$_POST['id_send']}), 
	'{$_POST['tipo_paq']}',
	(SELECT localidad_id FROM usuarios WHERE id={$_POST['id_send']}),
	(SELECT localidad_id FROM usuarios WHERE id={$_POST['id_rec']})
)
";
Link to comment
https://forums.phpfreaks.com/topic/279612-insert-with-joins/#findComment-1438111
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.