【问题标题】:How to insert more than 2 queries in MySQL using arrays如何使用数组在 MySQL 中插入超过 2 个查询
【发布时间】:2016-07-08 23:24:24
【问题描述】:

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

前 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>

【问题讨论】:

标签: php mysql arrays forms


【解决方案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')");
  }
}

说明:

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

不需要增量计数器。

【讨论】:

  • 对于第一个循环,您需要使用for 而不是foreach,因为您需要增加$i 而您foreach $_REQUEST 但得到REQUEST['field_name'] 不要使用$val 这个错误!
  • @Naumov,请再次阅读该程序。如果可能,请尝试在您的机器上运行它。使用 OP 的逻辑来回答不是强制性的。答案应该有效,这就是标准。
  • 不需要for。程序会变得不必要地复杂。
  • 您可以简单地将参数绑定为数组以查询多插入dev.mysql.com/doc/refman/5.5/en/insert.html。对不起,我不明白你的回答我可以投票
  • 是的,我们做到了。它是一种不同的方法。我用我的小知识给出了答案。
【解决方案2】:

试试这个

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

【讨论】:

  • 你试过了吗?
  • 我尝试了你的代码,然后我把解决方案放在这里。
【解决方案3】:

不用一个查询,用两个查询就可以了。

【讨论】:

    【解决方案4】:

    你可以这样做:

    $newdata = "('" . implode("'), ('", $_REQUEST['field_name']) . "')";
    
    $mysqli->query("INSERT INTO firearms_id_secs (year) VALUES $newdata");
    

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多