1

I'm trying to get last id which i inserted already in database through another php file and in current php file i can't get last id to use it.

This is my code.

    <?php 
    include "connection.php.php";

    $sucess = 105;
    $fail = 107;


    $planImage = $_POST['projects_plan_images'];
    $planFilename = $_POST['projectsPlanImagesNames'];

            $last_id = mysqli_insert_id($con);

        $planLength = count($planFilename);

        for($i = 0; $i < $planLength; $i++) {
            $imageNow = time();
            $new_imageFilename = $imageNow . "_" . $planFilename[$i]; 

        $sql6 = "INSERT INTO projects_plans_images(project_id, plan_image,         plan_name) VALUES ('$last_id','$new_imageFilename','$new_imageFilename')";
        $result6 = mysqli_query($con, $sql6);

            $binary=base64_decode($planImage[$i]);

            $file = fopen('files/plans_images/' . $new_imageFilename, 'wb');
            fwrite($file, $binary);
            fclose($file);    
            }


    $jsonResult = '{"state":';
    if($result){
        $jsonResult.= $sucess;
     }else {
        $jsonResult.= $fail;
     }
    $jsonResult .="}";
    print_r($jsonResult);
    ?>
Khaled
  • 238
  • 1
  • 3
  • 11
  • 3
    Possible duplicate of [LAST\_INSERT\_ID() MySQL](https://stackoverflow.com/questions/3837990/last-insert-id-mysql) – Ravi Dec 25 '18 at 12:06
  • Most simple is to use MySQL’s LAST_INSERT_ID() in the second query.. But you should worry first about the SQL injections in your code – Raymond Nijland Dec 25 '18 at 12:06
  • @Ravi Thank you so much for aswering me, it's working but it gives me last id in the same table but i want last id from another table, is it possible? – Khaled Dec 25 '18 at 12:12
  • @RaymondNijland Thank you so much for aswering me, it's working but it gives me last id in the same table but i want last id from another table, is it possible? – Khaled Dec 25 '18 at 12:12
  • @Khaled Why don't you use just `MAX` then ? – Ravi Dec 25 '18 at 12:14
  • @Ravi Can you explain more please? – Khaled Dec 25 '18 at 12:16
  • first of all I didn't understand why do you need last inserted id from other table ? and second I don't see any reference of your second table in your question and then how do you expect to get last inserted id of other table which is nowhere mentioned in your code. Please be clear with your information in your question. – Ravi Dec 25 '18 at 12:19
  • “it's working but it gives me last id in the same table but i want last id from another table, is it possible?” that function should get anny last insert id from anny insert query before look that @Ravi duplicated link where its explained – Raymond Nijland Dec 25 '18 at 12:20
  • Thank you guys for your replays , i'm sorry if i wasn't clear in my question and i found the solution and it's "INSERT INTO projects_plans_images(project_id, plan_image, plan_name) VALUES ((SELECT id FROM project ORDER BY id DESC LIMIT 1) ,'$new_imageFilename','$new_imageFilename')" – Khaled Dec 25 '18 at 12:26

0 Answers0