【问题标题】:issue with Inserting with last inserted id使用最后插入的 id 插入问题
【发布时间】: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-&gt;lastInsertId(); 但是我如何才能上传例如 2 张图片并让两张图片获得相同的反应 ID?

【问题讨论】:

    标签: php mysql database insert


    【解决方案1】:

    只需将反应 id 放入变量中

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

    然后在循环内部使用它。

    $q1['reactie_id'] = $reactie_id;

    【讨论】:

      【解决方案2】:

      我暂时会选择一个基本的解决方案,在 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>';
          }
      }
      

      【讨论】:

        【解决方案3】:

        我建议在插入查询后立即获取最后一个插入 ID,并将其保存在变量中以备后用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-13
          • 1970-01-01
          • 2012-07-27
          • 2011-06-01
          • 1970-01-01
          • 2017-02-06
          相关资源
          最近更新 更多