Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用上次插入的id插入时出现问题_Php_Mysql_Database_Insert - Fatal编程技术网

Php 使用上次插入的id插入时出现问题

Php 使用上次插入的id插入时出现问题,php,mysql,database,insert,Php,Mysql,Database,Insert,我拥有的:一个包含两个表的数据库:Reactions和ReactionImages。 用户可以用一些图片发布反应。因此,表Reactions有一个自动递增的id。表ReactionImages有一个名为:Reaction_id的列。因此,通过这种方式它们是连接的。请参见下面的代码,我是如何上传反应+图像的: if(isset($_POST['react_btn'])){ unset($q1); $q1['reactie'] = $app->

我拥有的:一个包含两个表的数据库: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>';
                    }
                }
            }
            }
if(设置($\u POST['react\u btn'])){
未结算(第一季度);
$q1['reactie']=$app->检查字符串($\u POST['editor1']);
$q1['topic\u id']=$app->check\u字符串($\u POST['topicid']);
$q1['klant\u id']=$app->check\u字符串($\u POST['klantid']);
$q1['ledenpagina_id']=$app->check_字符串($\u POST['ledenpagina_id']);
$q1['onderreactie\u id']=$app->check\u string($\u POST['OnDerreactide']);
$app->insert_query('reacties',$q1,'id');
如果(!空($_FILES['FILES']['name'])){
foreach($\u FILES[“FILES”][“tmp\u name”]作为$key=>$tmp\u name){
$file\u name=$\u FILES[“FILES”][“name”][$key];
$file\u name=$app->makeurlshop($file\u name);
$file\u name=$app->检查字符串($file\u name);
$file\u tmp=$\u FILES[“FILES”][“tmp\u name”][$key];
如果($\u文件['FILES']['error'][$key]==1)或($\u文件['FILES']['error'][$key]==2)){
“回声”警报(“
×
米斯卢克
Je foto是photoshop的bijvoorbeeld画作中的te groot,verklein de foto。
");';
}
$gelukt=$app->fotoupload($file\u tmp,$file\u name,'assets/images/reactiefootos',800);
如果($gelukt=='ok'){
未结算(第一季度);
$q1['reactie_id']=$database->lastInsertId();
$q1['foto']=$file\u name;
$app->insert_query2('fotoreactie',$q1);
}否则{
回显“警报”(“.$gelukt.”);
}
}
}
}

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

foreach
生成一个类似
$photoupload=false的变量之前,我暂时会选择一个基本的解决方案然后当您获得最后一个插入ID时,将其设置为true,然后使用if语句预先检查它是否为true,如果为true,则使用最后一个插入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>';
    }
}
$photoUpload=false;
foreach($\u FILES[“FILES”][“tmp\u name”]作为$key=>$tmp\u name){
$file\u name=$\u FILES[“FILES”][“name”][$key];
$file\u name=$app->makeurlshop($file\u name);
$file\u name=$app->检查字符串($file\u name);
$file\u tmp=$\u FILES[“FILES”][“tmp\u name”][$key];
如果($\u文件['FILES']['error'][$key]==1)或($\u文件['FILES']['error'][$key]==2)){
“回声”警报(“
×
米斯卢克
Je foto是photoshop的bijvoorbeeld画作中的te groot,verklein de foto。
");';
}
$gelukt=$app->fotoupload($file\u tmp,$file\u name,'assets/images/reactiefootos',800);
如果($gelukt=='ok'){
未结算(第一季度);
如果($photoUpload){
$q1['foto']=$file\u name;
$app->insert_query2('fotoreactie',$q1);
}
否则{
$q1['reactie_id']=$database->lastInsertId();
$q1['foto']=$file\u name;
$app->insert_query2('fotoreactie',$q1);
$photoUpload=true;
}
}否则{
回显“警报”(“.$gelukt.”);
}
}

我建议在insert查询之后立即获取最后一个insert id,并将其保存在变量中供以后使用

只需将反应id放入变量中即可

$app->insert_query('reacties',$q1,'id');
$reactie_id=$database->lastInsertId()

然后在循环内部使用它

$q1['reactie\u id']=$reactie\u id