如何使用两个不同的表查询将数据插入MySQL

时间:2018-03-12 22:17:04

标签: php mysqli

我将数据放入我的数据库,对不同的表有两个不同的查询。我创建了从另一个表中提交一些数据的功能,以及从输入字段中提交的少量字段数据。当我使用仅使用id提交数据时它工作正常,但是当我同时提交包含id和其他剩余输入字段的数据时,它只从输入字段提交并留下需要按ID放置的数据。

这是html:

<form id="addaccount" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input list="code" type="number" placeholder="Code..." name="code" class="inputs">
                <datalist id="code">
              <?php 
              while ($row3 = mysqli_fetch_array($result3))
              {
                  echo "<option required  value='".$row3['code']."'>".$row3['code']."</option>";
              }
              ?>        
              </datalist>   
               <input list="product" autocomplete="off" placeholder="*Products" value="<?php echo $product; ?>" name="product" id="products" class="inputs">
                    <datalist id="product">
                  <?php 
                  while ($row2 = mysqli_fetch_array($result2))
                  {
                      echo "<option required  value='".$row2['products']."'>".$row2['products']."</option>";
                  }
                  ?>        
                  </datalist>            

              <input list="suppliers" autocomplete="off" placeholder="*Suppliers" value="<?php echo $supplier; ?>" name="supplier" id="supplier" class="inputs">
                <datalist id="suppliers">
              <?php 
              while ($row1 = mysqli_fetch_array($result1))
              {
                  echo "<option required  value='".$row1['fullname']."'>".$row1['fullname']."</option>";
              }
              ?>        
              </datalist>   
            <input class="inputs" type="number" id="cost" name="cost" value="<?php echo $cost; ?>" placeholder="*Cost...">
            <input class="inputs" type="number" id="price" name="price" value="<?php echo $price; ?>" placeholder="*Price...">
            <input class="inputs" type="number" name="pay" value="<?php echo $pay; ?>" placeholder="*Pay...">
                <input class="inputs" type="text" name="other" value="<?php echo $other; ?>" placeholder="Other...">
              <select class="inputs" name="measure" id="sel1">
                <option value="Packs">Packs</option>
                <option value="KGs">Kilograms</option>
              </select>
            <input class="inputs" type="number" step="1.00" name="value" value="<?php echo $value; ?>" placeholder="*Quantity / Kilograms">
           <?php if($edit_state == false){ ?>
            <button class="btn btn-primary pull-left" type="submit" name="submit">Add Record</button>
           <?php } else { ?>
           <button class="btn btn-primary pull-left" type="submit" name="update">Update Record</button>
            &nbsp;
            <button class="btn btn-primary pull-right" onclick="clear();" type="reset" name="cancel">Cancel</button>
           <?php } ?>
          </form>

这是我的Config.php

//When Form Submits
    if (isset($_POST['submit'])) {
    $product = $_POST['product'];
    $date    = $_POST['date'];
    $cost = $_POST['cost'];
    $other = $_POST['other'];
    $code = $_POST['code'];
    $price = $_POST['price'];
    $pay = $_POST['pay'];      
    $measure = $_POST['measure'];
    $value  = $_POST['value'];
    $supplier = $_POST['supplier'];

    $getCode = "SELECT * FROM products WHERE code=$code";
    $result4 = mysqli_query($connection, $getCode);
    $row4 = mysqli_fetch_assoc($result4);
    if ($row4['code'] == $code) {
        $migrating = "INSERT INTO managment(date,product,supplier,cost) 
                      SELECT CURDATE(),products,supplier,price FROM products WHERE code=$code";
        $result5 = mysqli_query($connection, $migrating);
        //Query
    $query = "INSERT INTO managment(date, product, cost, other, code, price,pay, measure, value, supplier) VALUES (CURDATE(), '$product','$cost','$other', '$code', '$price',$pay, '$measure', '$value', '$supplier')";
    mysqli_query($connection, $query);
    $_SESSION['msg'] = "Record Saved Successfully";
    header("location: home.php");
    }

    }

1 个答案:

答案 0 :(得分:3)

如果要运行2个以上的查询,请运行mysqli_query两次。不要将两个查询合并为一个。

作为一方,您的代码极易受到SQL注入的影响,因此非常容易“破解”。了解绑定参数或准备好的语句。