如何使用php将多行插入mysql数据库?

时间:2017-09-17 18:30:39

标签: php mysql database

提前致谢。我是php新手,想为商店创建一个简单的软件。现在我创建了一个允许一个接一个地添加项目的表单(一次一个)。但是我想让它变得灵活,所以当我按下“添加行”按钮时会再显示一行,填写多​​行后,我只需要按“提交”一次,所有行都存储在数据库中。

到目前为止,我已经编写了这段代码(我还没有添加CSS):

这是档案: index.php

<?php
    function companyName() {        
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT Name FROM `Companies`";
        $sql2 = "SELECT CompanyName FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $companies_result = mysqli_query($conn,$sql);
        $companies_result_2 = mysqli_query($conn,$sql2);
        if ((!$companies_result)||(!$companies_result_2)) {
            echo "Query Failed for company names";
        }
        while($company = mysqli_fetch_array($companies_result_2, MYSQLI_ASSOC)) {
            echo '<option value="';
            echo $company['CompanyName'];
            echo '">';
            echo $company['CompanyName'];
            echo '</option>';
        }
        while($companies = mysqli_fetch_array($companies_result, MYSQLI_ASSOC)) {
            echo '<option value="';
            echo $companies['Name'];
            echo '">';
            echo $companies['Name'];
            echo '</option>';
        }
        mysqli_close($conn);
    }

    function lastInvoice() {
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT InvoiceNumber FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $invoice_result = mysqli_query($conn,$sql);
        if (!$invoice_result) {
            echo "Query Failed for company names";
        }
        while($invoice = mysqli_fetch_array($invoice_result, MYSQLI_ASSOC)) {
            echo $invoice['InvoiceNumber'];
        }
        mysqli_close($conn);
    }

    function lastDate() {
        $conn = mysqli_connect('localhost','root','root','DataShop');
        if (!$conn) {
            echo "Couldn't retrieve company names...!";
        }
        $sql = "SELECT Date FROM `DataShop` WHERE ID = ( SELECT max(ID) FROM `DataShop` )";
        $invoice_result = mysqli_query($conn,$sql);
        if (!$invoice_result) {
            echo "Query Failed for company names";
        }
        while($invoice = mysqli_fetch_array($invoice_result, MYSQLI_ASSOC)) {
            echo $invoice['Date'];
        }
        mysqli_close($conn);
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title>Submission Form</title>
    <link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
    <div id="division1">
    <div id="division2">
        <form method="post" action="action.php">
            <table>
                <tr>
                    <td>Company Name :</td>
                    <td>
                        <select name="company_name">
                            <?php companyName(); ?>
                        </select><br>
                    </td>
                </tr>
                <tr>
                    <td>Date :</td>
                    <td><input type="date" name="date1" placeholder="" value="<?php echo date('Y-m-d');?>" size=""><br></td>
                </tr>
                <tr>
                    <td>Invoice Number :</td>
                    <td><input type="text" name="invoice_number" placeholder="" value="<?php lastInvoice(); ?>" size=""><br></td>
                </tr>
                <tr>
                    <td>Item Name :</td>
                    <td><input type="text" name="item_name" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>Description :</td>
                    <td><input type="text" name="description" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>HSN Number :</td>
                    <td><input type="text" name="hsn_number" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>Quantity :</td>
                    <td><input type="number" name="quantity" placeholder="" size="" min="1" max="1000"><br></td>
                </tr>
                <tr>
                    <td>MRP :</td>
                    <td><input type="number" name="mrp" placeholder="" size="" min="1"><br></td>
                </tr>
                <tr>
                    <td>Cost :</td>
                    <td><input type="number" name="cost" placeholder="" size=""><br></td>
                </tr>
                <tr>
                    <td>GST % :</td>
                    <td>
                        <select name="gst">
                            <option value="0.0">0.00</option>
                            <option value="5.5">5.50</option>
                            <option value="15.0">12.00</option>
                            <option value="15.0">18.00</option>
                            <option value="28.0">28.00</option>
                        </select><br>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" name="" placeholder=""></td>
                    <td><input type="reset" name="" placeholder=""></td>
                </tr>
            </table>      
        </form><br><br><br>
        <a href="Goods.php">Show Records</a><br><br>
        <a href="Search.html">Search</a>
    </div>   
   </div>
</body>
</html> 

行动档案: action.php

<?php

    $db = mysqli_connect('localhost','root','root','DataShop');

   if (!$db) {
    echo 'Failed to connect to database...';
   }

    $companyName = $date = $invoiceNumber = $itemName = $hsnNumber = $description = $quantity = $mrp = $cost = $gst = '';

    $companyName = $_POST['company_name'];
    $date = $_POST['date1'];
    $invoiceNumber = $_POST['invoice_number'];
    $itemName = $_POST['item_name'];
    $hsnNumber = $_POST['description'];
    $description = $_POST['hsn_number'];
    $quantity = $_POST['quantity'];
    $mrp = $_POST['mrp'];
    $cost = $_POST['cost'];
    $gst = $_POST['gst'];

    $sql = "INSERT INTO `DataShop` (CompanyName, Dates, InvoiceNumber, ItemName, Description, HSNNumber, Quantity, MRP, Cost, GST) VALUES ('$companyName','$date','$invoiceNumber','$itemName','$description','$hsnNumber','$quantity','$mrp','$cost','$gst')";

    $result = mysqli_query($db,$sql);
    if(!$result) {
        echo 'No result';
    }
    else {
        echo 'One record inserted';
    }

?>

我已经知道使用数组存储数据,但目前尚不清楚如何做到这一点。

我希望用户输入“Product Name”等数据,然后进入变量/数组“ProductName []”,然后自动逐个存储到数据库中。

再次感谢...

0 个答案:

没有答案