如何在mysqli中使用单个查询插入表的多行数据

时间:2015-11-10 11:08:22

标签: php mysqli

我有一个有多行的表,行数不固定。更确切地说,有时它可能有3行,有时它可能有30行,具体取决于用户的需求。我想在单个查询中将该表的所有行数据存储到mysqli数据库中。如果单个查询无法实现,我想知道是否有其他解决方案。我的代码如下:

HTML

   <form role="form" method="post">
          <table class="table" id="dataTable">
            <TR>
            <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD><input type="hidden" value="1" name="num" id="orderdata">1</TD>
            <TD><INPUT type="text" name="medname" class="form-control" id="orderdata" placeholder="Your Medicine name"/></TD>
            <TD><INPUT type="text" name="medqty" class="form-control" id="orderdata" placeholder="Quantity" /></TD>
            <TD>
               <SELECT name="medtype" class="form-control" id="orderdata">
                  <OPTION value="in">India</OPTION>
                  <OPTION value="de">Germany</OPTION>
                  <OPTION value="fr">France</OPTION>
                  <OPTION value="us">United States</OPTION>
                  <OPTION value="ch">Switzerland</OPTION>
                </SELECT>
              </TD>
          </TR>
  </table>
         <div class="center-block">
                    <span> <button type="button" class="btn btn-success" value="Add Row" onclick="addRow('dataTable')">Add More</button></span>
                    <span> <button type="button" value="Delete Row" class="btn btn-success" onclick="deleteRow('dataTable')">Delete</button></span>
                    <span><button type="submit" name="submit" class="btn btn-success">Submit</button></span>
                    <br>

                </div>  

            </form> 

PHP

  <?php

    $email = $_SESSION["email"];
    $cell = "Select `cellno` FROM `userreg` WHERE email = '$email'";
    $result = mysqli_query($conn, $cell);
    $cellno = "";
    if(mysqli_num_rows($result)>0){
        while($row = mysqli_fetch_assoc($result)) {
            $cellno = $row["cellno"];
        }
    }


    if(isset($_POST['submit'])){

        $fullname = $_SESSION["fullname"];
        $medname = $_POST['medname'];
        $medqty  = $_POST['medqty'];
        $medtype = $_POST['medtype'];
        $num = $_POST['num'];
        $date = date("Y.m.d");


      $medorder = "INSERT INTO `medorder` (`fullname`, `phoneno`, `medname`, `quantity`, `medtype`, `date`,`orderno` ) VALUES ('$fullname', '$cellno', '$medname','$medqty', '$medtype', CURRENT_TIMESTAMP, '$num' )";

      if($conn->query($medorder)=== TRUE) {
        echo"<br>";
        echo " Your Question has been submitted successfully";
      }

      else {
        echo " Error!! Try again or later";
      }

   }     


?>

0 个答案:

没有答案