2013-03-14 244 views
0

所以我只能用这个话题在我的形式找出一些JavaScript问题做到:How Can I Insert Multiple Rows Into a DB from my HTML Form with Multiple Rows Dynamically?需要HTML表单提交多行插入SQL数据库

但因为我有这么多部分的问题我已经被问创造一个新的话题。下面是我正在使用的新代码,其中我被卡住了,希望能够将我的表单提交给我的数据库,并将这些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);

+0

gah。现在用火烧掉这个代码,或者至少从轨道上把它烧掉。 [SQL注入漏洞](http://bobby-tables.com)嘉豪...你的错误信息是一个SQL错误,因为你正在构建你的查询(非常糟糕)。 – 2013-03-14 20:01:18

+0

我也不喜欢它,但不幸的是,我不知道更好。 – jflay 2013-03-14 20:07:30

+0

停止懒惰,并建立查询很长的路要走。你只是乞求你的服务器被摧毁。 – 2013-03-14 20:08:01

回答

0

我认为没有这样的事情在PHP中批量插入。 JAVA的addBatch & executeBatch没有等价物。

所以正确的做法是简单地对数组进行迭代,并用准备语句插入单行。

+0

所以基本上每个结构都有一个SQL语句在里面呢? – jflay 2013-03-14 21:00:39