将表数据插入SQL

时间:2019-03-04 10:13:13

标签: php mysql sql database phpmyadmin

我有一个模态,可以让用户选择设备然后输入数量。

<div class="container">
    <!-- Trigger the modal with a button -->
    <button type="button" class="button-1 btn-lg" data-toggle="modal" data-target="#myModal" style="width: 26%; margin:0; height: 40px; font-size: 100%;">Choose Equipment</button>

    <!-- MODAL -->
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog">

        <!-- MODAL CONTENT-->
        <div class="modal-content">
          <div class="modal-header">
            <p>Choose An Equipment Below</p>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
          </div>
          <div class="modal-body">
            <p>Available Equipments</p>
            <div class="form-inline">
              <!-- DROPDOWN LIST FROM DATABASE -->
              <select id="equipment" name="equipment" class="form-control">
                <option selected="" disabled="">Select Equipment</option>
                <?php
                                $sql = mysqli_query($conn, "SELECT resource_name From resources WHERE resource_type = 'EQUIPMENT';");
                                $row = mysqli_num_rows($sql);
                                while ($row = mysqli_fetch_array($sql)){
                                echo "<option name = 'equipment' value ='". $row['resource_name'] ."'>" .$row['resource_name'] ."</option>" ;
                                }
                                ?>
              </select>
              <br><br>
              <input type="text" id="qnty-1" class="form-control" placeholder="Quantity" name="quantity" style="width:24%; margin-left: 50px;"><br/>

              <br/><br/>
            </div>
          </div>
          <div class="modal-footer">
            <input class="submit-3" id="addToTable" type="submit" value="Submit" name="submit-2" onclick="passValue1(); trigData()" data-dismiss="modal" />
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>

提交后,模态将消失,选择的数据将显示在表格上。我可以将第一个选定的数据插入数据库,但是如果我选择2个不同的数据或相同的数据,它将只在数据库中插入一个。有什么方法可以将所有选定的数据插入数据库?

<div class="b-form-outline">
<h1>Equipment Borrowing Form</h1>
<div class="borrow-list-area">
  <table class="container-2" id="container-2">
    <thead>
      <tr>
        <style>
          th,
          td {
            text-align: center;
          }
        </style>
        <th>Equipment</th>
        <th>Quantity</th>
        <th hidden>Serial No.</th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>
<script>
function passValue1() {
document.getElementById("quantity").innerHTML = 
document.getElementById("qnty-1").value;
}

$('#addToTable').click(function() {
  var eqpmnt = $('#equipment').val(),
  qnt = $('#qnty-1').val();      
  $('table tbody').append('<tr><td>' + eqpmnt + '</td><td>' + qnt + '</td> 
</tr>');
});

我已经尝试了此插入代码,但是同样,它只会从表中插入一行数据。如果有人可以帮助我。非常感谢!

<?php

include('dbconnector.php');


$id_num = $_POST['idnumber'];
$s_date = $_POST['startdate'];
$e_date = $_POST['enddate'];
$s_time = $_POST['starttime'];
$e_time = $_POST['endtime'];
$prpse = $_POST['purpose'];
$equipment = $_POST['equipment'];
$qnty = $_POST['quantity'];
$subj = $_POST['subject'];
$onCamp = isset($_POST['check-1']) ? $_POST['check-1'] : "Off-Campus";

// CREATES A NEW TRANSACTION THAT WAS SUBMITTED USING THE BORROW FOWM

$query = "SELECT * FROM resources;";                // ACCESS TABLE RESOURCES
$con = mysqli_query($conn, $query);                 
if ($con->num_rows > 0){
    while($row = mysqli_fetch_array($con)){
        $rsname = $row['resource_name'];
        if (strcmp($equipment, $rsname) == 0) {

            $sql = "INSERT INTO transactions (id_number, start_date, end_date, start_time, end_time, purpose, quantity, subject, use_place) VALUES ('$id_num', '$s_date', '$e_date', '$s_time', '$e_time', '$prpse', '$qnty', '$subj', '$onCamp');";

        }
    }
}

if (!mysqli_query($conn,$sql)) {
    $message = "Error Sending Form";
    echo "<script type='text/javascript'>alert('$message');</script>";
}
else {       
    header("refresh: 1; url=borrow.php");   
}

?>

针对@Jon Stirling进行了调整

$query = "SELECT * FROM resources;";                // ACCESS TABLE RESOURCES
$con = mysqli_query($conn, $query);                 
if ($con->num_rows > 0){
    while($row = mysqli_fetch_array($con)){
        $rsname = $row['resource_name'];
        if (strcmp($equipment, $rsname) == 0) {

            $sql = "INSERT INTO transactions (id_number, start_date, end_date, start_time, end_time, purpose, quantity, subject, use_place) VALUES ('$id_num', '$s_date', '$e_date', '$s_time', '$e_time', '$prpse', '$qnty', '$subj', '$onCamp');";

            if (!mysqli_query($conn,$sql)) {

                $message = "Error Sending Form";
                echo "<script type='text/javascript'>alert('$message');</script>";
            }
            else {       
                header("refresh: 1; url=borrow.php");   
            }
        }
    }
}

0 个答案:

没有答案