title: Insert data in 2 tables for a categories system
body: <p>i am doing a practice of a small blog with php and sql and i need to insert data in 2 tables at the same time, for example when creating a new post selected their categories of that post and that these are shown in a different table called entries.</p>
  <p>These are the 3 tables that I have created, in POSTS all the data of the posts is stored and in CATEGORIAS all the data of the categories is stored.</p>
  <p><a href="https://i.stack.imgur.com/wuhot.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wuhot.png" alt="IMAGE 1" /></a></p>
  <p><strong>TABLE: POST</strong></p>
  <p><a href="https://i.stack.imgur.com/xOXcu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xOXcu.png" alt="IMAGE 2" /></a></p>
  <p><strong>TABLE: CATEGORIAS</strong></p>
  <p><a href="https://i.stack.imgur.com/DTBue.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DTBue.png" alt="IMAGE 3" /></a></p>
  <p>Then my question is how can I get the data inserted into the table of <strong>entradas</strong> and store the ID's of the publications and the ID's of the selected categories. For example, in this case the publication &quot;El Oceano&quot; has the category &quot;Naturaleza&quot;.</p>
  <p><strong>Example of the table: entradas</strong></p>
  <p><a href="https://i.stack.imgur.com/hRjZS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hRjZS.png" alt="IMAGE 4" /></a></p>
  <p>CODE: db.php</p>
  <pre><code>&lt;?php
  
  session_start();
  require('connect.php');
  
  function dd($value)
  {
      echo &quot;&lt;pre&gt;&quot;, print_r($value, true), &quot;&lt;/pre&gt;&quot;;
      die();
  }
  
  
  function executeQuery($sql, $data)
  {
      global $conn;
      mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
      $stmt = $conn-&gt;prepare($sql);
      $values = array_values($data);
      $types = str_repeat('s', count($values));
      $stmt-&gt;bind_param($types, ...$values);
      $stmt-&gt;execute();
      return $stmt;
  }
  
  
  function selectAll($table, $conditions = [])
  {
      global $conn;
      $sql = &quot;SELECT * FROM $table&quot;;
      if (empty($conditions)) {
          $sql = $sql . &quot; ORDER BY id DESC&quot;;
          $stmt = $conn-&gt;prepare($sql);
          $stmt-&gt;execute();
          $records = $stmt-&gt;get_result()-&gt;fetch_all(MYSQLI_ASSOC);
          return $records;    
      } else {
          // $sql = &quot;SELECT * FROM $table&quot;;
          
          $i = 0;
          foreach ($conditions as $key =&gt; $value) {
              if ($i === 0){
                  $sql = $sql . &quot; WHERE $key=?&quot;;
                  
              } else {
                  $sql = $sql . &quot; AND $key=?&quot;; 
              }
              $i++;
          }
          
          $stmt = $conn-&gt;prepare($sql);
          $values = array_values($conditions);
          $types = str_repeat('s', count($values));
          $stmt-&gt;bind_param($types, $values);
          $stmt-&gt;execute();
          $records = $stmt-&gt;get_result()-&gt;fetch_all(MYSQLI_ASSOC);
          return $records;
      }
  }
  
  
  
  function selectOne($table, $conditions)
  {
      global $conn;
      $sql = &quot;SELECT * FROM $table &quot;;
  
          $i = 0;
                  foreach ($conditions as $key =&gt; $value) {
              if ($i === 0){
                  $sql = $sql . &quot; WHERE $key=?&quot;;
                  
              } else {
                 $sql = $sql . &quot; AND $key=?&quot;; 
              }
              $i++;
          }
          $sql = $sql . &quot; LIMIT 1&quot;;
          $stmt = executeQuery($sql, $conditions);
          $records = $stmt-&gt;get_result()-&gt;fetch_assoc();
          return $records;
      }
  
  function create($table, $data)
  {
      global $conn;
      $sql = &quot;INSERT INTO $table SET &quot;;
        
      $i = 0;
      foreach ($data as $key =&gt; $value) {
              if ($i === 0){
                  $sql = $sql . &quot; $key=?&quot;;
                  
              } else {
                  $sql = $sql . &quot;, $key=?&quot;; 
              }
              $i++;
          }
      $stmt = executeQuery($sql, $data);
      $id = $stmt-&gt;insert_id;
      return $id;
      
  }
  
  function update($table, $id, $data)
  {
      global $conn;
      $sql = &quot;UPDATE $table SET &quot;;
        
      $i = 0;
      foreach ($data as $key =&gt; $value) {
              if ($i === 0){
                  $sql = $sql . &quot; $key=?&quot;;
                  
              } else {
                  $sql = $sql . &quot;, $key=?&quot;; 
              }
              $i++;
          }
      
      $sql = $sql . &quot; WHERE id=?&quot;;
      $data['id'] = $id;
      $stmt = executeQuery($sql, $data);
      return $stmt-&gt;affected_rows;
      
  }
  
  
  function delete($table, $id)
  {
      global $conn;
      $sql = &quot;DELETE FROM $table WHERE id=?&quot;;
        
      $stmt = executeQuery($sql, ['id' =&gt; $id]);
      return $stmt-&gt;affected_rows;
      
  }
  </code></pre>
  <p>CODE: posts.php</p>
  <pre><code>&lt;?php 
  
  include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
  include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
  
  $table = 'posts';
  $categorias = selectAll('categorias');
  
  $errors = array();
  $id = '';
  $title = '';
  $body = '';
  
  $posts = selectAll($table);
  
      
      if (!empty($_FILES['image']['name'])) {
          $image_name = time() . '_' . $_FILES['image']['name'];
          $destination = &quot;../../images/&quot; . $image_name;
          
          $result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
          
          if ($result){
              $_POST['image'] = $image_name;
          } else {
              array_push($errors, &quot;¡Algo fallo al subir la imagen!&quot;);
          }
          
          
      } else {
        array_push($errors, &quot;¡Necesitas subir una imagen!&quot;);
      }
      
      if (count($errors) === 0){
          unset($_POST['add-post']);
          $post_id = create($table, $_POST);
          $_SESSION['message'] = '¡Anime creado correctamente!';
          $_SESSION['type'] = 'success';
          header('location: ../../admin/posts/index.php');
          exit();
      } else {
          $title = $_POST['title'];
          $body = $_POST['body'];
  
      }
  
  
  if (isset($_GET['id'])){
      $id = $_GET['id'];
      $post = selectOne($table, ['id' =&gt; $id]);
  }
  
  if (isset($_GET['del_id'])){
      $id = $_GET['del_id'];    
      $count = delete($table, $id);
      $_SESSION['message'] = '¡Anime eliminado correctamente!';
      $_SESSION['type'] = 'success';
      header('location: ../../admin/posts/index.php');
      exit();
  }
  
  if (isset($_POST['update-post'])){
      $errors = validateEdit($_POST); 
      
      if (count($errors) === 0){
          $id = $_POST['id'];
          unset($_POST['update-post'], $_POST['id']);
          $post_id = update($table, $id, $_POST);
          $_SESSION['message'] = '¡Anime actualizado correctamente!';
          $_SESSION['type'] = 'success';
          header('location: ../../admin/topics/index.php');
          exit();        
      } else {
          $id = $_POST['id'];
          $title = $_POST['title'];
          $body = $_POST['body'];
  }
  } 
  ?&gt;      
  
                 
  </code></pre>
  <p>CODE: categorias.php</p>
  <pre><code>&lt;?php 
  
  include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
  include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');
  
  $table = 'categorias';
  
  $errors = array();
  $id = '';
  $name = '';
  $body = '';
  $topic_id = '';
  
  $categorias = selectAll($table);
  
  
  if (isset($_POST['add-post'])) {
      $errors = validateCategoria($_POST);
      
      if (count($errors) === 0){
          unset($_POST['add-post']);
          $post_id = create($table, $_POST);
          $_SESSION['message'] = '¡Categoria creada correctamente!';
          $_SESSION['type'] = 'success';
          header('location: ../../admin/categorias/index.php');
          exit();
      } else {
          $name = $_POST['name'];
  
      }
  }
  
  if (isset($_GET['id'])){
      $id = $_GET['id'];
      $post = selectOne($table, ['id' =&gt; $id]);
  }
  
  if (isset($_GET['del_id'])){
      $id = $_GET['del_id'];    
      $count = delete($table, $id);
      $_SESSION['message'] = '¡Categoria eliminada correctamente!';
      $_SESSION['type'] = 'success';
      header('location: ../../admin/categorias/index.php');
      exit();
  }
  
  if (isset($_POST['update-post'])){
      $errors = validateEdit($_POST); 
      
      if (count($errors) === 0){
          $id = $_POST['id'];
          unset($_POST['update-post'], $_POST['id']);
          $post_id = update($table, $id, $_POST);
          $_SESSION['message'] = '¡Categoria actualizada correctamente!';
          $_SESSION['type'] = 'success';
          header('location: ../../admin/categorias/index.php');
          exit();        
      } else {
          $id = $_POST['id'];
          $name = $_POST['name'];
  
  }
  } 
  ?&gt;          
  
                              
  </code></pre>