Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于i循环PHP-MYSQL_Php_Mysql_Arrays_For Loop_If Statement - Fatal编程技术网

对于i循环PHP-MYSQL

对于i循环PHP-MYSQL,php,mysql,arrays,for-loop,if-statement,Php,Mysql,Arrays,For Loop,If Statement,编辑 我有一个关于循环和向MYSQL插入数据的问题 我正在创建一个档案系统,这是我实习计划的一部分,该系统的作用是存储有关盒子和文件的数据。每个框可以包含1个或多个文件 <form> <input type="text" maxlength="2" name="box_number[]" placheholder="Box No" /> <input type="text" name="item_name[]" placeholder="Item name"

编辑

我有一个关于循环和向MYSQL插入数据的问题

我正在创建一个档案系统,这是我实习计划的一部分,该系统的作用是存储有关盒子和文件的数据。每个框可以包含1个或多个文件

    <form>
<input type="text" maxlength="2" name="box_number[]" placheholder="Box No" />
<input type="text" name="item_name[]" placeholder="Item name"  /></td>
    </form>
我有两个表,分别是BOX和BOX\u数据。BOX包含自动生成的BOX_ID(主键),该ID链接到BOX_数据的BOX_ID(FK)

感谢Gayan,前面的问题已经解决了,Gayan根据框号有不同的id,但另一个问题出现了,我无法将表框中的框id链接到表框数据中的外键(框id)

以下是我的表格代码:

  <SCRIPT src="jquery-3.2.1.js"></SCRIPT>
    <script>
    function addMore() {
        $("<table>").load("input.php", function() {
                $("#product").append($(this).html());
        }); 
    }

    function deleteRow() {
        $('tr').each(function(index, item){
            jQuery(':checkbox', this).each(function () {
                if ($(this).is(':checked')) {
                    $(item).remove();
                }
            });
        });
    }

    </script>


    <form id="form" method="post" action="submit_request.php" enctype="multipart/form-data">

    <div style="border:black 1px solid;padding:10px;border-radius:20px;background:silver;">
       <strong>** INSTRUCTIONS **</strong><br /> 
       <strong>** Please refer and follow the format below. **</strong> <br />
       <strong> For example:-  3 boxes , Box 1 (2 items) , Box 2 (1 item) and Box 3 (1 item). </strong> <br />   
       <strong> Box No : 1 , File Name : Item 1  </strong> <br />
       <strong> Box No : 1 , File Name : Item 2  </strong> <br />
       <strong> Box No : 2 , File Name : Item 3  </strong> <br />
       <strong> Box No : 3 , File Name : Item 4  </strong> <br />   
       </div>

<div>
<table id="product" >
<tr>
<th></th>
<th>Box No:</th>
<th>File Name:</th>
<th>From</th>
<th>To</th>
<th>Description</th>
</tr>
<tr>
<td ><input type="checkbox" name="item_index[]" /></td>
<td><input class="form-control input-md" style="width:50px;" type="text" maxlength="2" name="box_number[]" placheholder="Box No" required="required"/></td>
<td><input class="form-control input-md" type="text" name="item_name[]" placeholder="File name" required="required" /></td>
<td><input class="form-control input-md" placeholder="Year from" maxlength="4"  name="date_from[]" /></td>
<td><input class="form-control input-md" placeholder="Year to" maxlength="4" name="date_to[]" /></td>
<td><textarea class="form-control input-md" cols="50" rows="5" name="description[]" placeholder="Any information on this file "></textarea></td>
</tr>
</table>
</div>

<div class="row">       
<div class="col-xs-3 col-md-3">
<input class="btn btn-primary btn-block btn-md"  type="button" name="add_item" value="Add More Files" onClick="addMore();" />
</div>
<div class="col-xs-3 col-md-3">
<input class="btn btn-primary btn-block btn-md"  type="button" name="del_item" value="Delete" onClick="deleteRow();" />
</div>    
</div>  
</div>

函数addMore(){
$(“”)加载(“input.php”,function(){
$(“#product”).append($(this.html());
}); 
}
函数deleteRow(){
$('tr')。每个(功能(索引,项){
jQuery(':checkbox',this).each(函数(){
如果($(this).is(':checked')){
$(项).remove();
}
});
});
}
**说明**
**请参考并遵循以下格式**
例如:-3个盒子、盒子1(2项)、盒子2(1项)和盒子3(1项)
第1号框,文件名:项目1
第1号框,文件名:项目2
第2号框,文件名:项目3
第3号框,文件名:项目4
信箱号码: 文件名: 从…起 到 描述

if(isset($\u POST['submit'])){
$uniqueItems=array_unique($_POST[“box_number”]);
foreach($key=>$uniqueItem的uniqueItems){
//使用$uniqueItem,您可以持久化到数据库。。。
$stmt_box=$db->prepare(“插入框中(创建日期)值(:创建日期)”);
$stmt_box->bindParam(':date_created',$date);
$stmt_box->execute();
$last_box_id=$db->lastInsertId();
}
如果(!空($\u POST[“项目名称”])){
$itemCount=计数($\u POST[“item\u name”]);
$itemValues=0;
$query=“将数据(项目、方框id)值插入方框”;
$queryValue=“”;
对于($i=0;$iprepare($sql);
$result->execute();
打印json_编码($sql);
打印json_编码($stmt);
}}}
我不知道最后一个插入的id放在哪里,它是否相关。因为到目前为止我得到的是

  TABLE BOX 
                Box_ID  | Date |
                1       | 2017 |         <- box_number = 1 , Item= Item 1 and 2
                2       | 2017 |         <- box_number = 1 , Item= Item 3 and 4
                3       | 2017 |         <- box_number = 2 , Item= Item 5

TABLE BOX_DATA 
            Items  | Box_ID | Date |
            Item 1 |   3    | 2017 |     <- this is what i get
            Item 2 |   3    | 2017 |         
            Item 3 |   3    | 2017 |     
            Item 4 |   3    | 2017 |         
            Item 5 |   3    | 2017 | 
表格框
箱号|日期|

1 | 2017 |您可以做的是首先隔离唯一的
框编号
,然后继续

$uniqueItems = array_unique($_POST["box_number"])

foreach($uniqueItems as $key => $uniqueItem) {
    // using $uniqueItem you can persists to the database...
}

你的解释很难理解,我假装这是你正在寻找的解决方案

如果
和中的语句看起来完全一样,为什么你要复制代码?你也不需要一直准备相同的语句。只准备一次。而且你没有改变任何变量如果你绑定了,那么它每次都会插入相同的内容。像
$i+2;$j+2;
这样的语句没有什么作用。你不应该像在第一个代码块中那样有嵌套的循环。只有一个循环处理所有的输入。但是你没有在
插入
查询中使用任何输入数据。这个问题令人难以置信地困惑。请e显示HTML的真实外观、数据库模式以及应该插入数据库的行。对不起,希望编辑的版本足够解决问题,但它会引发另一个问题
  TABLE BOX 
                Box_ID  | Date |
                1       | 2017 |         <- box_number = 1 , Item= Item 1 and 2
                2       | 2017 |         <- box_number = 1 , Item= Item 3 and 4
                3       | 2017 |         <- box_number = 2 , Item= Item 5

TABLE BOX_DATA 
            Items  | Box_ID | Date |
            Item 1 |   3    | 2017 |     <- this is what i get
            Item 2 |   3    | 2017 |         
            Item 3 |   3    | 2017 |     
            Item 4 |   3    | 2017 |         
            Item 5 |   3    | 2017 | 
TABLE BOX 
                Box_ID  | Date |
                1       | 2017 |         <- box_number = 1 , Item= Item 1 and 2
                2       | 2017 |         <- box_number = 1 , Item= Item 3 and 4
                3       | 2017 |         <- box_number = 2 , Item= Item 5

    TABLE BOX_DATA 
                Items  | Box_ID | Date |
                Item 1 |   1    | 2017 |     <- This is what im trying to achieve
                Item 2 |   1    | 2017 |         
                Item 3 |   2    | 2017 |     
                Item 4 |   2    | 2017 |         
                Item 5 |   3    | 2017 |    
$uniqueItems = array_unique($_POST["box_number"])

foreach($uniqueItems as $key => $uniqueItem) {
    // using $uniqueItem you can persists to the database...
}