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

时间:2018-12-25 12:03:16

标签: php mysql

我正在尝试获取我通过另一个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);
    ?>

0 个答案:

没有答案