I have a INSERT function where it inserts the image filename in the ‘ImageFile’ field in the “Image” table, each row has it’s own ImageId thanks to auto number. An example of this is below:
ImageId ImageFile
23 orange.jpg
24 flowers.png
25 castle.png
26 orange.jpg
What I want to do is also insert the ImageId into another table with the QuestionId and SessionId so that this table (Image_Question) can use the ImageId to link the Image table with the Image Question table. But how do I code it so that when I insert image details in the table above, then it will retrieve it’s ImageId and also store it in the ImageId in the Image_Question table. Example below:
ImageId SessionId QuestionId
23 AAA 1
24 AAA 2
25 AAA 3
26 AAA 4
I have coded the INSERT values for SessionId and QuestionId but just need help retrieving and inserting the ImageId. Below is the current code:
<?php
session_start();
//connect to db
$i = 0;
$insertimage = array();
for($i = 0; $i < $c; $i++ ){
$insertimage[] = "'". mysql_real_escape_string($_SESSION['id'] ) .
($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'".
mysql_real_escape_string( $_POST['numQuestion'][$i] ) ."'";
}
$imagesql = "INSERT INTO Question (ImageId, SessionId, QuestionId)
VALUES (" . implode('), (', $insertimage) . ")";
echo($imagesql);
mysql_close();
?>
I have an old php version 5.2.13 because that is the version of the university’s server so I have to use mysql() code even though I know it is better to use PDO or Mysqli.
I think what you are looking for is mysql_insert_id . It will return the id of last inserted row, so you can assign it to variable and use that varriable to insert the id into another table with next query