0

I am putting data to my database with two different queries for different tables. I have created the functionality of submitting a few data from one another table and few fields data from input fields. When I use submitting data using only an id it works fine, but when I submit data with both id and other remaining input fields, it only submits from input fields and left the data that need to put by ID.

Here is the 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>

Here is my 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 Answer 1

3

If you want to run more than 2 queries, run mysqli_query twice. Don't combine both queries into one.

As an side, your code is extremely susceptible to SQL injection, making it extremely easy to 'hack'. Learn about bound parameters or prepared statements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.