Jump to content

Insert with joins?


patchido
 Share

Go to solution Solved by kicken,

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

Edited by patchido
Link to comment
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            prueba@qualtia.com   1               2                 3

11 romtorre ROMELIA       TORRES        VELI               romtorre@qualtia.com 1              4                  2

Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.