簡體   English   中英

需要HTML表單才能將多行提交到SQL數據庫

[英]Need HTML Form to Submit Multiple Rows into SQL Database

因此,在本主題中,我剛剛弄清楚了表單中的一些javascript問題: 如何動態地將具有多行的HTML表單中的多行插入數據庫中?

但是由於我有這么多部分的問題,所以我被要求創建一個新主題。 以下是我在卡住的地方使用的新代碼,並希望能夠提交我的表單,該表單具有向數據庫添加多行並將這些HTML行分別添加到SQL數據庫中的行的功能。

預覽: http//cl.ly/image/0K0Z202O1Q3e/Screen%20Shot%202013-03-14%20at%203.00.19%20PM.png

的HTML

<html>
<header>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" language="javascript" src="/jquery/js/jquery-1.9.1.js">      
</script>
<script src="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?   
key=Gmjtd%7Cluua2q6bn9%2C8g%3Do5-lzbsh"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<title>Central Office Company Survey</title>
</header>
<body onload="get_company_name();">
<h1>Central Office Company Survey</h1>
<div id='map' style='width:0px; height:0px; position:absolute'></div>

<input type="hidden" id="co_city" name="co_city">
<input type="hidden" id="co_state" name="co_state">
<input type="hidden" id="co_zipcode" name="co_zipcode">

<table>
<th>Company</th>
<th>CO Name</th>
<th>Get Current Location</th>
<th>Lat</th>
<th>Long</th>
<th>Address</th>
<tr>
    <td><select id="company_name" name="company_name" /></select></td>
    <td><input name="co_name" type="text"></td>
    <td><input type="submit" value="Get GPS" onclick="gpslookup();" /></td>
    <td><input id="co_lat" name="co_lat" type="text" /></td>
    <td><input id="co_long" name="co_long" type="text" /></td>
    <td><input id="co_address" name="co_address" type="text" /></td>
</tr>
</table>
<table id="tabledata">
<thead>
    <th>Select</th>
    <th>Border Location Name</th>
    <th>Cable Location</th>
    <th>Direction of Vault Wall</th>
    <th>Cable Type</th>
    <th>Cable Size (pairs)</th>
    <th>Cable Gauge</th>
    <th>Vertical(s) appeared on Verticals</th>
    <th>Approximate Number of Jumpers</th>
    <th>Are Protectors Still In?</th>
    <th>Metered Distance</th>
    <th class="comments">Central Office Comments</th>
</thead>
<tbody id="input"></tbody>
<tbody id="template">
    <tr>
        <td><input type="checkbox" /></td>
        <td><input name="border_location" type="text" /></td>
        <td><input name="cable_location" type="text" /></td>
        <td><input name="vault_direction" type="text" /></td>
        <td><input name="cable_type" type="text" /></td>
        <td><input name="cable_size" type="text" /></td>
        <td><input name="cable_gauge" type="text" /></td>
        <td><input name="vertical" type="text" /></td>
        <td><input name="jumpers" type="text" /></td>
        <td><input name="protectors" type="text" /></td>
        <td><input name="metered_dist" type="text" /></td>
        <td><input name="comments" type="text" /></td>
    </tr>
</tbody>
</table>
<button id="ActionAddRow">Add Row</button>
<button onclick="deleteRow(); return false;">Delete Row</button>
<button id="ActionSubmit">Submit</button>
</body>
</html>

scripts.js

//Button Functions
$(function () {
var addInputRow = function () {
    $('#input').append($('#template').html());
};

addInputRow();
$('#ActionAddRow').on('click', addInputRow);
$('#ActionSubmit').on('click', function () {
    var data = $('#input tr').map(function () {
        var values = {};
        $('input', $(this)).each(function () {
            values[this.name] = this.value;
        });
        return values;
    }).get();
    $.post('./php/upload_survey.php', {
        json: JSON.stringify(data),
        delay: 1
    }).done(function (response) {
        alert("Thank you. Your form has been submitted.");
        console.log(response);
    });
});
});

//Delete Selected Rows
function deleteRow() {
 try {
    var table = document.getElementById("tabledata");
    var rowCount = table.rows.length;

    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 3) {
                alert("Cannot delete all the rows.");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}
catch(e) {
    alert(e);
}
};

upload_survey.php

//Assign passed parameters
    $values = json_decode($_POST['json']);

    $stringLogInfo =  "INFO: Location: $co_address CO Name = $co_name !!!\n\n";
    log_audit($logAuditFile, $logModule, $stringLogInfo);

//Parse and store the ini file, this will return an associative array
ini_set("display_errors", "1");
error_reporting(E_ALL);

//Insert Survey Form Information into the database
$sql="INSERT INTO table_name (company_name,...,...) 
VALUES ($values)";

mysql_query($sql) or die ("Unable to Make Query:" . mysql_error());

**到目前為止,每次我嘗試使其成功運行時,JS都會成功觸發,但是在Chrome開發人員工具箱中出現錯誤:無法進行查詢:列數與第1行的值計數不匹配

這是指js文件中的此功能:console.log(response);

我認為PHP中沒有諸如批處理插入之類的東西。 JAVA的addBatch和executeBatch沒有等效項。

因此,正確的方法是簡單地對數組進行迭代,並使用准備好的語句插入單行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM