patchido Posted June 26, 2013 Share Posted June 26, 2013 (edited) 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 June 26, 2013 by patchido Quote Link to comment https://forums.phpfreaks.com/topic/279612-insert-with-joins/ Share on other sites More sharing options...
patchido Posted June 26, 2013 Author Share Posted June 26, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279612-insert-with-joins/#findComment-1438104 Share on other sites More sharing options...
Solution kicken Posted June 26, 2013 Solution Share Posted June 26, 2013 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']}) ) "; Quote Link to comment https://forums.phpfreaks.com/topic/279612-insert-with-joins/#findComment-1438111 Share on other sites More sharing options...
patchido Posted June 27, 2013 Author Share Posted June 27, 2013 let me try that out, thats exactly what i wanted, (didn't know that existed) Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/279612-insert-with-joins/#findComment-1438249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.