如何使用数组在MySQL中插入2个以上的查询

时间:2016-03-22 07:12:42

标签: php mysql arrays forms

我在MySQL中插入查询时遇到问题。每当我尝试在MySQL中提交超过2个查询时。像这样:

enter image description here

前2个查询(2011年和2012年)只会插入MySQL。虽然没有插入2013年。对不起,我只是用PHP新手。我希望提交表单并将所有查询插入MySQL。我希望每当我尝试输入两个以上的表单时,它都会被提交并插入到MySQL而不仅仅是2个查询。

<?php
$mysqli = new mysqli('localhost','root','','sample');

//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$i = 0;

foreach ($_REQUEST as $val) {
if (isset($_REQUEST['submit'])) {

$year = $_REQUEST['field_name'][$i];

$mysqli->query("INSERT INTO firearms_id_secs (year) VALUES ('$year')");
$i++;
} 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add more fields using jQuery</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a     href="javascript:void(0);" class="remove_button" title="Remove field"><img     src="remove-icon.png"/></a></div>'; //New input field html 
var x = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
    if(x < maxField){ //Check maximum number of input fields
        x++; //Increment field counter
        $(wrapper).append(fieldHTML); // Add field html
    }
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button     is clicked
    e.preventDefault();
    $(this).parent('div').remove(); //Remove field html
    x--; //Decrement field counter
});
});
</script>

<style type="text/css">
input[type="text"]{height:20px; vertical-align:top;}
.field_wrapper div{ margin-bottom:10px;}
.add_button{ margin-top:10px; margin-left:10px;vertical-align: text-bottom;}
.remove_button{ margin-top:10px; margin-left:10px;vertical-align: text-    bottom;}
</style>
</head>

<body>
<form name="codexworld_frm" action="" method="post">
<div class="field_wrapper">
<div>

    <a href="javascript:void(0);" class="add_button" title="Add field"><img src="add-icon.png"/></a>
</div>
</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>

</body>
</html>

4 个答案:

答案 0 :(得分:1)

而不是这段代码,

foreach ($_REQUEST as $val) {
if (isset($_REQUEST['submit'])) {

$year = $_REQUEST['field_name'][$i];

$mysqli->query("INSERT INTO firearms_id_secs (year) VALUES ('$year')");
$i++;
} 

使用此代码:

if (isset($_REQUEST['submit']) && isset($_REQUEST['field_name'])) {
  foreach ($_REQUEST['field_name'] as $val) {
    $mysqli->query("INSERT INTO firearms_id_secs (year) VALUES ('$val')");
  }
}

<强>解释

你只是field_name的值,所以循环它。

无需增量计数器。

答案 1 :(得分:1)

试试这个

    for($i=0;$i<=count($_REQUEST);$i++){
    $year = $_REQUEST['field_name'][$i];
    $mysqli->query("INSERT INTO firearms_id_secs (year) VALUES ('$year')");
}

答案 2 :(得分:1)

无需使用一个查询,进行两次查询即可。

答案 3 :(得分:1)

你可以这样做:

$newdata = "('" . implode("'), ('", $_REQUEST['field_name']) . "')";

$mysqli->query("INSERT INTO firearms_id_secs (year) VALUES $newdata");