title: php and mysql insert into multiple tables from one form
body: <p>I am creating a simple site to keep records of users, customers, suppliers, etc.</p>
  
  <p>I have created the forms to register clients and users using single forms capturing data through $ _POST form method</p>
  
  <p>So far I have not had problems since the INSERTS are done on singles table wich PK is an AutoIncremented field</p>
  
  <p>In the code shown below, my goal is to let users create a vendor (name, phone number, email, address) as well as asign 1 or many vendor-category. This information is contained in one form</p>
  
  <p>vendors, vendors-categories, users etc are stored in a mysql database with the structure shown in attached file</p>
  
  <p><a href="https://i.stack.imgur.com/UcbNa.png" rel="nofollow noreferrer">structure</a></p>
  
  <p>There is something wrong about my code after succesfully create vendor and i am not sure how to solve the part that manages insertions in the join table (tblprovxrubro)</p>
  
  <p>Code</p>
  
  <pre><code>&lt;?php
  
      session_start();
      //available for admin, power-users and role-4
      if ($_SESSION['rol'] ==2) {
        header("location: ./");
      }
      include "../conexion.php";
  
  
      if (!empty($_POST)) {
        $alert='';
        if (empty($_POST['razonSocial'])|| empty($_POST['email'])) {
          $alert='&lt;p class="msg_error"&gt;Vendor name and email must not be blank.&lt;/p&gt;';
  
        }else{
  
  
          $razonSocial  = $_POST['razonSocial'];
          $email        = $_POST['email'];
          $domicilio    = $_POST['domicilio'];
          $telefono     = $_POST['telefono'];
          $usuario_id   = $_SESSION['iduser'];
  
          $query = mysqli_query($conection,"SELECT * FROM tblprov WHERE razonSocial = '$razonSocial'");
          $result = mysqli_fetch_array($query);
  
          if ($result &gt;0)
           {
            $alert='&lt;p class="msg_error"&gt;vendor already exists.&lt;/p&gt;';
           }else{
            $query_insert = mysqli_query($conection, "INSERT INTO tblprov(razonSocial, numero, domicilio, email, idUsuario)
                                                                    VALUES('$razonSocial','$telefono','$domicilio','$email', '$usuario_id')");
  
            if ($query_insert) 
            {
              $alert='&lt;p class="msg_save"&gt;Vendor created succesfully.&lt;/p&gt;';
  
              //Once vendor is created i need his ID in order to insert in table
              //tblprovxrubro as many records as vendor-types selected in form
  
              //Not sure about how to achieve this
  
              $queryBuscaprov = mysqli_query($conection, "SELECT id FROM tblprov WHERE razonSocial = '$razonSocial'");
              $result_prov = mysqli_fetch_array($queryBuscaProv);
              $idProv = $result_prov['id'];
  
              foreach ($_POST['idRubro'] as $opcionSeleccionada) 
              {
                //This INSERT should execute as many times as vendor-types selected in form
                $query2 = mysqli_query($conection, "INSERT INTO tblprovxrubro (idRubro, idProv) VALUES ('$opcionSeleccionada', '$idProv')");
              }
            }else{
              $alert='&lt;p class="msg_error"&gt;Error creating vendor.&lt;/p&gt;';
                 }
               }
             }
             mysqli_close($conection);
           }
       ?&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;?php include "includes/scripts.php"; ?&gt;
          &lt;title&gt;Registro de Proveedores&lt;/title&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
      &lt;?php include "includes/header.php"; ?&gt;
          &lt;section id="container"&gt;
          &lt;div class="form_register" name="form_register"&gt;
            &lt;h1&gt;&lt;i class="fas fa-building"&gt;&lt;/i&gt; Vendor List&lt;/h1&gt;
            &lt;hr&gt;
  
          &lt;form action="" method="post" class="form_register"&gt;
  
              &lt;div class="alert"&gt;&lt;?php echo isset($alert) ? $alert : ''; ?&gt;&lt;/div&gt;
              &lt;label for="razonSocial"&gt;Razón Social&lt;/label&gt;
              &lt;input type="text" name="razonSocial" id="razonSocial" placeholder="Razón Social"&gt;
              &lt;label for="telefono"&gt;Número Telefónico&lt;/label&gt;
              &lt;input type="text" name="telefono" id="telefono" placeholder="Número Telefónico"&gt;
              &lt;label for="domicilio"&gt;Domicilio&lt;/label&gt;
              &lt;input type="text" name="domicilio" id="domicilio" placeholder="Domicilio"&gt;
              &lt;label for="email"&gt;Email&lt;/label&gt;
              &lt;input type="text" name="email" id="email" placeholder="Email"&gt;
  
              &lt;?php
                $query_rubro = mysqli_query($conection,"SELECT * FROM tblrubros");
                mysqli_close($conection);
                $result_rubro = mysqli_num_rows($query_rubro);
               ?&gt;
               &lt;select name="rubro[]" id="rubro" multiple size="12"&gt;
  
            &lt;?php
              if($result_rubro &gt; 0)
              {
                while ($rubro = mysqli_fetch_array($query_rubro)) 
                {
                  ?&gt;
                &lt;option value="&lt;?php echo $rubro["idRubro"]; ?&gt;"&gt;&lt;?php echo $rubro["rubroDescripcion"] ?&gt;&lt;/option&gt;
                  &lt;?php
                }
              }
             ?&gt;
          &lt;/select&gt;
          &lt;p&gt;press Ctrl in order to select multiple options.&lt;/p&gt;
          &lt;button name="submit" type="submit" class="btn_save"&gt;&lt;i class="fas fa-save"&gt;&lt;/i&gt; Crear Proveedor&lt;/button&gt;
        &lt;/form&gt;
      &lt;/div&gt;
      &lt;/section&gt;
  
      &lt;?php include "includes/footer.php"; ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>conection.php</p>
  
  <pre><code>&lt;?php
  
      $host = 'localhost';
      $user = 'root';
      $password = 'mypass';
      $db = 'compras';
  
      $conection = @mysqli_connect($host,$user,$password,$db);
      mysqli_set_charset($conection,"utf8");
  
      if(!$conection){
          echo "connection error";
      }
  
  ?&gt;
  </code></pre>