lingo5 Posted May 30, 2012 Share Posted May 30, 2012 Hi, I have 2 queries to insert stuff into 2 tables like so: $idpropiedad=$_POST["id_propiedad"]; //this is a hidden field on my form. Don't know if this is right $regimen=$_POST["regimen"]; $localidad=$_POST["localidad"]; $isla=$_POST["isla"]; $tipopropiedad=$_POST["tipopropiedad"]; $zona=$_POST["zona"]; $amueblado=$_POST["amueblado"]; $habitaciones=$_POST["habitaciones"]; $estado=$_POST["estado"]; $baños=$_POST["baños"]; $cocina=$_POST["cocina"]; $salon=$_POST["salon"]; $terraza=$_POST["terraza"]; $aseos=$_POST["aseos"]; $metros=$_POST["metros"]; $descripcion=$_POST["descripcion"]; $precio=$_POST["precio"]; $precio_oferta=($_POST['precio_oferta']) ? GetSQLValueString($_POST['precio_oferta']) : "`precio_oferta`"; $novedad=$_POST["novedad"]; $destacada=$_POST["destacada"]; $query = "INSERT INTO midatabase.t_propiedades (id_propiedad,regimen, localidad, isla, tipopropiedad, zona, amueblado, habitaciones, estado, baños, cocina, salon, terraza, aseos, metros, descripcion, precio, precio_oferta, novedad, destacada)". "VALUES ('$idpropiedad', '$regimen', '$localidad','$isla','$tipopropiedad', '$zona','$amueblado','$habitaciones','$estado','$baños','$cocina','$salon','$terraza','$aseos','$metros','$descripcion','$precio','$precio_oferta','$novedad','$destacada')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); $query2 ="INSERT INTO midatabase.t_imagenes(id_propiedad,imagen_path)". "VALUES ('$idpropiedad','$filePath')"; mysql_query($query2) or die('Error, query failed : ' . mysql_error()); My problem is that I need the id_propiedad inserted in the first table to get also inserted in the second one....but all I get in the second one is "0". How can I insert the same id_property into both tables? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/263390-please-help-with-query/ Share on other sites More sharing options...
wigwambam Posted May 30, 2012 Share Posted May 30, 2012 If id_propiedad is an auto_increment field then:- After: mysql_query($query) or die('Error, query failed : ' . mysql_error()); Add: $id_propiedad = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/263390-please-help-with-query/#findComment-1349851 Share on other sites More sharing options...
lingo5 Posted May 30, 2012 Author Share Posted May 30, 2012 Thanks wigwambam, but that's not what I'm after. id_propiedad on table 1 is the primary key and auto increment and is inserted automatically when a new property is created. id_propiedad on table 2 gets set to 0 and it should be the same value of id_propiedad on table 1 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/263390-please-help-with-query/#findComment-1349852 Share on other sites More sharing options...
Barand Posted May 30, 2012 Share Posted May 30, 2012 try it before you knock it! mysql_insert_id() returns the last auto_increment id that was created during the current connection. Quote Link to comment https://forums.phpfreaks.com/topic/263390-please-help-with-query/#findComment-1349853 Share on other sites More sharing options...
wigwambam Posted May 30, 2012 Share Posted May 30, 2012 I don't understand your logic. If id_propiedad on table 1 is the primary key and auto increment why are you passing it in your form data? You should insert the record without the id and let the database generate the next one for you. Change this line: $query = "INSERT INTO midatabase.t_propiedades (id_propiedad,regimen, localidad, isla, tipopropiedad, zona, amueblado, habitaciones, estado, baños, cocina, salon, terraza, aseos, metros, descripcion, precio, precio_oferta, novedad, destacada)". "VALUES ('$idpropiedad', '$regimen', '$localidad','$isla','$tipopropiedad', '$zona','$amueblado','$habitaciones','$estado','$baños','$cocina','$salon','$terraza','$aseos','$metros','$descripcion','$precio','$precio_oferta','$novedad','$destacada')"; To: $query = "INSERT INTO midatabase.t_propiedades (regimen, localidad, isla, tipopropiedad, zona, amueblado, habitaciones, estado, baños, cocina, salon, terraza, aseos, metros, descripcion, precio, precio_oferta, novedad, destacada)". "VALUES ('$regimen', '$localidad','$isla','$tipopropiedad', '$zona','$amueblado','$habitaciones','$estado','$baños','$cocina','$salon','$terraza','$aseos','$metros','$descripcion','$precio','$precio_oferta','$novedad','$destacada')"; AND stick the other code in I mentioned earlier. Quote Link to comment https://forums.phpfreaks.com/topic/263390-please-help-with-query/#findComment-1349854 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.