2013-03-14 248 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