這是我對同一問題的第二篇文章,在我的第一篇文章中,我使用的是mysql
而不是mysqli
,所以我根據建議做了一些更新。 我有一個由用戶通過javascript控制的可變字段集(1到20集)的表單。使用動態表單將一組或多組字段插入數據庫
沒有動態內容,一切正常。在將我的html表單從city
更改爲city[]
後,它顯然停止將數據傳遞到我的數據庫。
我嘗試了各種方法使這項工作沒有成功。有任何想法嗎?
形式操作:
var i=0; //Global Variable i
//function for increment
function increment(){
i +=1;
}
$(document).ready(function(e){
/// Variables
var html ='<p /><div>';
html+='<label for="city">City:</label>';
html+=' <input type="text" name="childcity[]" id="city">';
html+=' <label for="date">Date:</label>';
html+=' <input type="text" name="childdate[]" id="date">';
html+='<a href="#" id="removecity">Remove City</a>';
html+='</div>';
var maxRows = 20;
// var x = 1;
// Rows
$("#addcity").click(function(e){
if(i <= maxRows){
$("#container").append(html);
i++;
}
});
// Remove Rows
$("#container").on('click','#removecity', function(e){
$(this).parent('div').remove();
i--;
});
});
表單數據檢索和查詢:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "root", "", "pb1646a_jos1");
// Check connection
// Escape user inputs for security
$city = $_POST['city'];
$date = $_POST['date'];
foreach($city AS $key => $value){
// attempt insert query execution
$sql = "INSERT INTO employe_table(name,age)
VALUES (
'".$mysqli->real_escape_string($value)."',
'".$mysqli->real_escape_string($date['$key'])."'
)";
$insert = $mysqli->query($query);
}
$mysqli->close(); // Close connection
header("location: http://www.orastravelagency.com/modules/mod_pari/test/index.html")
?>
形式:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="java.js" type="text/javascript"></script>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="insert2.php" method="post">
<div>
<p>
<label for="name">Trip Name:</label>
<input type="text" name="name" id="name">
</p>
</div>
<div id="container">
<p>
<label for="">City:</label>
<input type="text" name="city" id="city[]">
<label for="date">Date:</label>
<input type="text" name="date" id="date[]">
<a href="#" id="addcity">Add City</a>
</p>
</div>
<p />
<input type="submit" value="submit">
</form>
</body>
</html>
當我嘗試它時,它給了我一個服務器錯誤(「si」代表什麼?) –
si代表第一列是字符串,第二列是整數。 – Barmar
什麼是錯誤? – Barmar