0

im building a property website.

the current situation is that I have a property.php that includes many isset _get to determine the output. The property page is querying the property table and depending on the field allowing different types of input. I am allowing uploads of multiple images which is in the images table. This is all working perfectly.

the problem is that due to the two tables, I need to insert two queries (I've defined tables and columns and fields into variables so I can reuse them in different pages and sites :) )

    $qry=mysql_query("INSERT INTO $table($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8,$col9,$col10,$col11,$col12,$col13,$col14)VALUES('$field','$field2','$field3','$field4','$field5','$field6','$field7','$field8','$field9','$field10','$field11','$field12','$field13','$field14')", $con);


if(!$qry)
{
die("Query Failed: ". mysql_error());
}
else
{

include 'upload.php';

the upload.php does all the movement of the images and runs another query

$query="INSERT into images (`property`,`image`)  VALUES(SELECT LAST_INSERT_ID(),'$file_name'); ";

my problem is that the property field is never populated with the last_insert_id() and always 0.

basically I want to be able to track all the images uploaded to the id of the property table where it was inserted during the submit and I believe last_insert_id() would work to my needs - but not working with the current code.

before anyone talks about mysqli and pdo, let me mention that I will do it at a later stage.

Saud Kazia
  • 192
  • 2
  • 15
  • You should use pdo for this. Refer here http://stackoverflow.com/questions/1685860/how-do-i-get-the-last-inserted-id-of-a-mysql-table-in-php You can select that not directly in the query but calling the function after first insert. So: 1)INSERT $table - 2)$lastinsert = mysql_insert_id(); - 3) INSERT images – SBO Jan 26 '15 at 10:57
  • [`mysql_insert_id()`](http://php.net/manual/en/function.mysql-insert-id.php). [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? – Prix Jan 26 '15 at 10:57

1 Answers1

0

No need to use select before LAST_INSERT_ID() on inserting try

$query="INSERT into images (`property`,`image`)  VALUES( LAST_INSERT_ID(),'$file_name'); ";

OR you can try with mysql_insert_id()

$qry=mysql_query("INSERT INTO $table($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8,$col9,$col10,$col11,$col12,$col13,$col14)VALUES('$field','$field2','$field3','$field4','$field5','$field6','$field7','$field8','$field9','$field10','$field11','$field12','$field13','$field14')", $con);
if(!$qry) {
  die("Query Failed: ". mysql_error());
}
else {
$last_id = mysql_insert_id();
include 'upload.php';

then upload.php query will be

$query="INSERT into images (`property`,`image`)  VALUES('$last_id','$file_name'); ";
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • this actually solves the initial issue, but I see that it doesn't resolve my problem. when i run the 2nd query first time, it takes the id of property, but if i upload multiple pictures the 2nd query run 2nd time, will generate id of 2nd query and not 1st query. i hope that's clear. – Saud Kazia Jan 26 '15 at 11:21
  • ok then save your first id in table and get back the inserted id for multiple queries – Rakesh Sharma Jan 26 '15 at 11:26
  • sorry, i don't quite get that. 1st query is for properties table 2nd query is for images table 1st query runs . properties table fields are populated if successful, 2nd query runs 2nd query gets last id correct for the 1st insert on 2nd insert part of the array, it last id becomes the id of 2nd query not first. tried mysql_insert_id as well, same issue – Saud Kazia Jan 26 '15 at 11:27
  • if each time page refrehing you will get a new last id so what's problem. if you do multiple upload then above variable $last_id will be return same – Rakesh Sharma Jan 26 '15 at 11:30
  • the last id, should only be of query 1, not of query 2... the reason is that the id of property table will be inserted to track all the images from that property in the images table – Saud Kazia Jan 26 '15 at 11:31
  • hmm. sorry rakesh, i didn't try the variable bit, will try and let you know – Saud Kazia Jan 26 '15 at 11:38
  • doesn't seem to like it Notice: Undefined variable: last_id in D:\xampp\htdocs\Websites\mindia\upload.php on line 13 Notice: Undefined variable: last_id in D:\xampp\htdocs\Websites\mindia\upload.php on line 13 Notice: Undefined variable: last_id in D:\xampp\htdocs\Websites\mindia\upload.php on line 13 Notice: Undefined variable: last_id in D:\xampp\htdocs\Websites\mindia\upload.php on line 13 – Saud Kazia Jan 26 '15 at 11:43
  • never mind. it works. just noticed I had included upload.php twice... thanks a lot. – Saud Kazia Jan 26 '15 at 12:29