Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.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
Php 使用for循环将数组插入SQL_Php_Html_Mysql_Arrays_Foreach - Fatal编程技术网

Php 使用for循环将数组插入SQL

Php 使用for循环将数组插入SQL,php,html,mysql,arrays,foreach,Php,Html,Mysql,Arrays,Foreach,我在一个页面上有一个表单,它是由for循环创建的 $scripte = ($_POST["scriptake"]); $scriptot = ($_POST["scriptot"]); include 'config.php'; echo '<h2>'.$scripte.' Equipment</h2>'; echo '<h2>Total Hours '.$scriptot.'</h2>'; echo '<table class="tab

我在一个页面上有一个表单,它是由for循环创建的

$scripte = ($_POST["scriptake"]);
$scriptot = ($_POST["scriptot"]);
include 'config.php';

echo '<h2>'.$scripte.' Equipment</h2>';
echo '<h2>Total Hours '.$scriptot.'</h2>';

echo '<table class="table table-responsive">
        <tr>
          <form action="equiptest.php" method="post">
          <th>Script Hour</th>
          <th>Equipment Required</th>
          <th>Stage</th>
        </tr>';
$x = 1;
$p = 1;
$r = 1;
while($x <= $scriptot ) {
  echo "<tr>
          <td>".$x++."</td>
          <td>
            <input name='equip[".$r++."]'>
          </td>
          <td>
            <input name='stage[".$p++."]'>
            <input name='ohyeah' type='hidden' value= '".$scriptot."'>
          </td>
        </tr>";
}
echo '<tr>
        <td colspan="2">
          <input type="submit" class="btn btn-primary">
        </td>
      </tr>
      </form>
     </table>';
$scripte=($_POST[“scriptake]”);
$scriptot=($_POST[“scriptot]”);
包括'config.php';
回显“”。$scripte.“设备”;
回显“总小时数”。$scriptot.';
回声'
脚本时间
所需设备
阶段
';
$x=1;
$p=1;
$r=1;
而($x$value){
foreach($_POST['stage']作为$key=>$stage){
$query=$con->stmt_init();
$statement=$con->prepare(“插入dgam_设备
(设备清单,阶段)值(?,)”;
$statement->bind_参数('ss',$value,$stage);
$statement->execute();
}
}
呼应"成功",;
//绑定要打印的结果变量
$statement->close();
回音“




”;
然后我尝试将数组插入到数据库中

ID|设备清单|阶段
ID设备[]阶段[]

我正在尝试嵌套foreach循环或将数组添加到正确的行中。我可以把这一行输入,但是数据超出了很多次。我猜这是因为foreach循环超过了第二个foreach循环 将此类数据放入数据库的正确方法是什么。我正试图先建立一个阵列,但我很挣扎

$length=count($_POST['stage']);
  $length = count($_POST['stage']);
  $stages = $_POST['stage'];
  $equips = $_POST['equip'];
  $stage = '';
  $equip = '';
  $query = $con->stmt_init();
  $statement = $con->prepare( "INSERT INTO dgam_equip (equiplist,stage)  VALUES (?,?) ");
  $statement->bind_param('ss', $equip, $stage);

  for( $i=0; $i < $length; $i++ ) {
     $stage = $stages[$i];
     $equip = $equips[$i];

     if ( ! empty($stage) ) $statement->execute();

   }
$stages=$_POST['stage']; $Equipms=$_POST['Equipm']; $stage=''; $Equipment=''; $query=$con->stmt_init(); $statement=$con->prepare(“插入dgam_设备(设备列表,阶段)值(?)”; $statement->bind_参数('ss',$equipment,$stage); 对于($i=0;$i<$length;$i++){ $stage=$stages[$i]; $Equipment=$Equipms[$i]; 如果(!empty($stage))$语句->执行(); }
它可以优化为在一个查询中获取所有值。

$equipes=$\u POST['equipe'];
$equipes = $_POST['equip'];
$stages = $_POST['stage'];

$query = $con->stmt_init();
$statement = $con->prepare( "INSERT INTO dgam_equip (equiplist,stage)  VALUES (?,?) ");

for ($i = 0, $total = count($equipes); $i < $total; $i = $i + 100) {
        $insertEquipe = array_slice($equipes, $i, 100);
        $insertStage = array_slice($stages, $i, 100);
        $conn->beginTransaction();

        foreach ($insertEquipe as $equipe) {
           foreach ($insertStage as $stage) {
               $stmt->bindValue(1, $equipe);
               $stmt->bindValue(2, $stage);
               $stmt->execute();
            }  
        }
        $conn->commit();
    }
$stages=$_POST['stage']; $query=$con->stmt_init(); $statement=$con->prepare(“插入dgam_设备(设备列表,阶段)值(?)”; 对于($i=0,$total=count($equipes);$i<$total;$i=$i+100){ $insertEquipe=array\u slice($equipes,$i,100); $insertStage=array\u slice($stages$i,100); $conn->beginTransaction(); foreach($insertEquipe为$equipe){ foreach($insertStage作为$stage){ $stmt->bindValue(1$equipe); $stmt->bindValue(2$stage); $stmt->execute(); } } $conn->commit(); }
编辑并删除不必要的html,唯一相关的是实际的循环,您还应该改进代码的缩进,使其更易于阅读。很抱歉,过去我因为没有提交代码而获得6票反对票。我现在就开始使用一个循环,并使用索引访问两个数组。您可以使用
sizeof
获得数组的大小,您可能希望在对数组进行循环之前比较数组的大小。每次标记错误时,数组的大小都相同:
。它们没有正确匹配,
不适用于
每个步骤都需要准备吗?:)您不应该在循环中调用
prepare()
bind_param()
,只需在启动循环之前执行一次即可,即:
$stage=null$设备=空$语句=$con->prepare(“插入…”)$语句->绑定参数('ss',$Equipm,$stage);对于($i=0;$i<$length;$i++){$stage=$stages[$i];$equip=$equips[$i];$statement->execute();}
这很有效,谢谢。我必须更改一些特定于我的代码的位,但您可能不知道这些。非常感谢你的帮助。我也接受了@cyclone的建议。如果它允许我的话,每次都给它打电话——投赞成票似乎不是一个好主意。我没有足够的点来编辑anwser,因为它是一个好点,这就是为什么我说它可以优化。我只是想展示如何更改当前代码,该代码在循环中也有命令。这仍然包含一个错误,即将有n x n个插入,而不是使用
foreach
两次,因为他认为这是处理两个数组的方法。但是,每个数组中都有具有相同数组索引的成对值。它们可以在一个
foreach
中寻址,创建n行以发送到数据库。
$equipes = $_POST['equip'];
$stages = $_POST['stage'];

$query = $con->stmt_init();
$statement = $con->prepare( "INSERT INTO dgam_equip (equiplist,stage)  VALUES (?,?) ");

for ($i = 0, $total = count($equipes); $i < $total; $i = $i + 100) {
        $insertEquipe = array_slice($equipes, $i, 100);
        $insertStage = array_slice($stages, $i, 100);
        $conn->beginTransaction();

        foreach ($insertEquipe as $equipe) {
           foreach ($insertStage as $stage) {
               $stmt->bindValue(1, $equipe);
               $stmt->bindValue(2, $stage);
               $stmt->execute();
            }  
        }
        $conn->commit();
    }