title: insert new tag create by user into my table(database) by php
body: <p>I'm currently building a blog system and here is my question.</p>
  <p>I provide several basic tags(using checkbox) for users to select.</p>
  <p>In the same time user can also insert a new tag by clicking the button of NEW TAG!.</p>
  <p>here is my code.</p>
  <pre><code>$newestTags = 0;
  
  $title = $_POST['blog_title'] ?? '';
  $content = $_POST['blog_content'] ?? '';
  $tagValue = $_POST['tags'] ?? '';
  $newTagFromhtml = $_POST['inserttag'] ?? '';
  
  $totalBlog = $pdo-&gt;query(&quot;SELECT COUNT(*) from `blog`&quot;)-&gt;fetch(PDO::FETCH_NUM)[0];
  $newestBlog = intval($totalBlog) + 1;
  
  $sql = &quot;INSERT INTO `blog`(`blog_id`,`blog_title`,`blog_content`)
                  VALUES(?,?,?)&quot;;  
  
  $stmt = $pdo-&gt;prepare($sql);
  
  $stmt-&gt;execute([
     $newestBlog,
     $title,
     $content,
  ]);
  
  $tagsInsert = &quot;INSERT INTO `blog_tagtoblog`(`tags_ID`, `blog_ID`) VALUES (?,?)&quot;;
  
  $tags = $pdo-&gt;prepare($tagsInsert);
  
  foreach($tagValue as $value){
    $tags-&gt;execute([
      $value,
      $newestBlog,
   ]);
  }
  
  </code></pre>
  <p>and here is my table for blog and tag</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>blog_tagstoblog</th>
  <th>tags_id</th>
  <th>blog_id</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>22</td>
  </tr>
  <tr>
  <td>2</td>
  <td>1</td>
  <td>23</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2</td>
  <td>22</td>
  </tr>
  </tbody>
  </table>
  </div><div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>tags_id</th>
  <th>tags_desc</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>food</td>
  </tr>
  <tr>
  <td>2</td>
  <td>lunch</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>also my HTML and Javascript</p>
  <pre><code>&lt;p&gt;TAGS!&lt;/p&gt;
  &lt;?php foreach($tags as $t): ?&gt;
  &lt;div&gt;
      &lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot; name=&quot;tags[]&quot; value=&quot;&lt;?= $t['tags_id']?&gt;&quot; id=&quot;&lt;?= $t['tags_id']?&gt;&quot;&gt;
      &lt;label for=&quot;&lt;?= $t['tags_id']?&gt;&quot; class=&quot;form-label&quot; id=&quot;haha&quot;&gt; &lt;?= $t['tags_desc']?&gt;&lt;/label&gt;
  &lt;/div&gt;
  &lt;? endforeach; ?&gt;
  &lt;button class=&quot;insertNewTag&quot;&gt;NEW TAG!&lt;/button&gt;
  &lt;/div&gt;
  
  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;submit&lt;/button&gt;
  
  // javascript down below
  
  insertNewTag.addEventListener(&quot;click&quot;, function(e) {
      e.preventDefault();
      input.type = &quot;text&quot;;
      input.name = &quot;tags[]&quot;
      input.value = input.value;
      spaceForNewTag.append(input);
  
  fetch(&quot;insertArticle-api.php&quot;, {
      method: 'POST',
      body: fd,
  }).then(res =&gt; res.json()).then(obj =&gt; {
      if (obj.success || obj.tagsCheck) {
          console.log(obj)
          alert(&quot;OKAY!&quot;)
          location.href = &quot;articleList.php&quot;;
  } else {
      alert(`${obj.error}`)
  }
  })
  })
  </code></pre>
  <p>and now my question is,</p>
  <p>it's okay for me to insert tag that I use foreach to loop over
  into tagstoblog and insert tags that created by user into table of tag.</p>
  <p>But I don't know how to fetch the tags_id of newest tag created by user
  and insert it into table of blog_tagstoblog.</p>
  <p>It is possible to do that in one go?</p>