php和mysql从一种形式插入到多个表中

时间:2018-11-20 21:23:02

标签: php mysql insert

我正在创建一个简单的网站来保存用户,客户,供应商等的记录。

我已经创建了表单,以使用通过$ _POST表单方法捕获数据的单个表单来注册客户端和用户

到目前为止,我还没有遇到任何问题,因为在PK是一个AutoIncremented字段的单表上完成了插入操作

在下面显示的代码中,我的目标是允许用户创建供应商(名称,电话号码,电子邮件,地址)以及分配1个或多个供应商类别。此信息以一种形式包含

供应商,供应商类别,用户等存储在mysql数据库中,其结构显示在附件中

structure

成功创建供应商后,我的代码出了点问题,我不确定如何解决管理联接表中插入的部分(tblprovxrubro)

代码

<?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='<p class="msg_error">Vendor name and email must not be blank.</p>';

      }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 >0)
         {
          $alert='<p class="msg_error">vendor already exists.</p>';
         }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='<p class="msg_save">Vendor created succesfully.</p>';

            //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='<p class="msg_error">Error creating vendor.</p>';
               }
             }
           }
           mysqli_close($conection);
         }
     ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <?php include "includes/scripts.php"; ?>
        <title>Registro de Proveedores</title>

    </head>
    <body>
    <?php include "includes/header.php"; ?>
        <section id="container">
        <div class="form_register" name="form_register">
          <h1><i class="fas fa-building"></i> Vendor List</h1>
          <hr>

        <form action="" method="post" class="form_register">

            <div class="alert"><?php echo isset($alert) ? $alert : ''; ?></div>
            <label for="razonSocial">Razón Social</label>
            <input type="text" name="razonSocial" id="razonSocial" placeholder="Razón Social">
            <label for="telefono">Número Telefónico</label>
            <input type="text" name="telefono" id="telefono" placeholder="Número Telefónico">
            <label for="domicilio">Domicilio</label>
            <input type="text" name="domicilio" id="domicilio" placeholder="Domicilio">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" placeholder="Email">

            <?php
              $query_rubro = mysqli_query($conection,"SELECT * FROM tblrubros");
              mysqli_close($conection);
              $result_rubro = mysqli_num_rows($query_rubro);
             ?>
             <select name="rubro[]" id="rubro" multiple size="12">

          <?php
            if($result_rubro > 0)
            {
              while ($rubro = mysqli_fetch_array($query_rubro)) 
              {
                ?>
              <option value="<?php echo $rubro["idRubro"]; ?>"><?php echo $rubro["rubroDescripcion"] ?></option>
                <?php
              }
            }
           ?>
        </select>
        <p>press Ctrl in order to select multiple options.</p>
        <button name="submit" type="submit" class="btn_save"><i class="fas fa-save"></i> Crear Proveedor</button>
      </form>
    </div>
    </section>

    <?php include "includes/footer.php"; ?>
</body>
</html>

conection.php

<?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";
    }

?>

0 个答案:

没有答案