0
海我試圖通過由一個爲每個查詢插入多個行作爲即時發送以下內容的數組元素中填充表添加數組值MySQL數據庫是我的代碼:使用PHP和jQuery
標記達:
<form action="post.php" method="POST">
<h1>user sign up</h1>
name:<input type="text" name="name" id="name"/>
password:<input type="text" name="pass" id="pass"/>
<div id="container">
<a href="#"><span id="add_field">» Add your list...</span></a><br/><br/>
</div>
<input id="go" class="btn" name="btnSubmit" type="submit" value="Signup" />
</form>
腳本:
<script>
var count = 0;
$(function(){
$('#add_field').click(function(){
count +=1;
//this is for appending input type="text" with the name of fields[]
// example like this: <input id="field_"+the incremented value (1,2,3..)+"name="fields[]" type="text"/>
$('#container').append('<strong>Link #' + count + '</strong>'+ '<input id="field_' + count + '" name="fields[]' + '" type="text" /><br/>');
});
});
</script>
php的:
<?php
$db = mysqli_connect("localhost","root","","test");
error_reporting(E_ALL);
//on click of the submit button
if(isset($_POST['btnsubmit'])){
//if the fields are not empty
if(!empty($_POST['fields']) && !empty($_POST['name']) && !empty($_POST['pass'])){
//the array/dynamic field
$fields = $_POST['fields'];
//the other two normal fields
$user = $_POST['name'];
$pass = $_POST['pass'];
//looping through and inserting the rows in table
foreach($fields as $key=> $value){
$query = mysqli_query($db,"INSERT INTO sample VALUES('','$user','$pass','$value')");
if($query){
echo "added <br/>";
} else {
echo "try";
}
}//end of foreach loop
echo "user added";
} else {
echo "fill the web fields";
}//end of the !empty if statement
}//end of first if
?>
在這裏,我想填充表像下面的
如果只有一個$ _ POST [「域」]類型
id | username | password | field
----------------------------------------
1 | name | password | field value
如果有一個以上的$ _ POST [「場」 ]等類型
id | username | password | field
------------------------------------------------
1 | name | password | field value 1
2 | name 2 | password 2 | field value 2
3 | name 3 | password 3 | field value 3
(取決於數組的大小/ ...)
AAAANNNNDDDD是什麼問題? –
它沒有插入數據 – user3104743