如何在mysql中获得最后一个插入的id并使用它将其插入到另一个表中?

g6baxovj  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(221)

我试图通过另一个php文件获取我已经插入到数据库中的最后一个id,而在当前的php文件中,我无法获取使用它的最后一个id。
这是我的密码。

<?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);
    ?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题