首页 > 解决方案 > 使用最后插入的 id 插入问题

问题描述

我有:一个有 2 个表的数据库:Reactions 和 ReactionImages。用户可以发布带有一些图像的反应。所以表 Reactions 有一个自动递增的 id。并且表 ReactionImages 有一个名为:Reaction_id 的列。所以通过这种方式他们是连接的。请参阅下面的代码我如何上传反应 + 图片:

 if(isset($_POST['react_btn'])){
            unset($q1);
            $q1['reactie'] = $app->check_string($_POST['editor1']);
            $q1['topic_id'] = $app->check_string($_POST['topicid']);
            $q1['klant_id'] = $app->check_string($_POST['klantid']);
            $q1['ledenpagina_id'] = $app->check_string($_POST['ledenpaginaid']);
            $q1['onderreactie_id'] = $app->check_string($_POST['onderreactieID']);
            $app->insert_query('reacties', $q1, 'id');

            if(!empty($_FILES['files']['name'])){

                foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
                    $file_name=$_FILES["files"]["name"][$key];
                    $file_name = $app->makeurlshop($file_name); 
                    $file_name = $app->check_string($file_name);
                    $file_tmp=$_FILES["files"]["tmp_name"][$key];

                    if(($_FILES['files']['error'][$key] == 1) or ($_FILES['files']['error'][$key] == 2)){
                        echo  '<script>alert("<div class="alert alert-error alert-dismissable">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            <h4><i class="icon fa fa-exclamation-triangle"></i> Mislukt</h4>
                            Je foto is te groot, verklein de foto eerst in bijvoorbeeld paint of photoshop.
                          </div>");</script>';
                    }

                    $gelukt = $app->fotoupload($file_tmp, $file_name, 'assets/images/reactiefotos', 800);

                    if($gelukt == 'ok'){
                        unset($q1);
                        $q1['reactie_id'] = $database->lastInsertId();
                        $q1['foto'] = $file_name;
                        $app->insert_query2('fotoreactie', $q1);

                     } else {
                        echo '<script>alert("'.$gelukt.'");</script>';
                    }
                }
            }
            }

我的问题是:当我上传包含 2 张或更多图片的反应时。第一张图片是正确的reaction_id。但是第二张图片获取的是在这张图片之前上传的图片的 id。我知道为什么会发生这种情况,因为我使用$database->lastInsertId();了但是我如何使它能够上传例如 2 张图片并让两张图片获得相同的反应 ID?

标签: phpmysqldatabaseinsert

解决方案


我暂时会寻求一个基本的解决方案,在foreach创建一个变量之前,比如$photoUploaded = false;当你得到最后一个插入 ID 时,将它设置为 true,然后事先有一个 if 语句检查它是否为真,如果它是真的使用最后一个插入 ID... 像这样:

$photoUpload = false;
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
    $file_name=$_FILES["files"]["name"][$key];
    $file_name = $app->makeurlshop($file_name); 
    $file_name = $app->check_string($file_name);
    $file_tmp=$_FILES["files"]["tmp_name"][$key];

    if(($_FILES['files']['error'][$key] == 1) or ($_FILES['files']['error'][$key] == 2)){
        echo  '<script>alert("<div class="alert alert-error alert-dismissable">
                     <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                     <h4><i class="icon fa fa-exclamation-triangle"></i> Mislukt</h4>
                     Je foto is te groot, verklein de foto eerst in bijvoorbeeld paint of photoshop.
    </div>");</script>';
    }

    $gelukt = $app->fotoupload($file_tmp, $file_name, 'assets/images/reactiefotos', 800);

    if($gelukt == 'ok'){
        unset($q1);
        if($photoUpload){
            $q1['foto'] = $file_name;
            $app->insert_query2('fotoreactie', $q1);
        }
        else{
            $q1['reactie_id'] = $database->lastInsertId();
            $q1['foto'] = $file_name;
            $app->insert_query2('fotoreactie', $q1);
            $photoUpload = true;
        }

    } else {
        echo '<script>alert("'.$gelukt.'");</script>';
    }
}

推荐阅读