【发布时间】:2018-06-12 07:55:15
【问题描述】:
我有什么:一个有 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?
【问题讨论】: