0

因此,我刚刚在本主题中解决了我的表单的一些 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>

脚本.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);

4

1 回答 1

0

我认为 PHP 中没有批量插入这样的东西。JAVA 的 addBatch 和 executeBatch 没有等价物。

因此,正确的方法是简单地迭代数组并使用准备好的语句插入单行。

于 2013-03-14T20:41:30.167 回答