从表单输入多行时缺少或未对齐的值 - PHP/MYSQLI

Posted

技术标签:

【中文标题】从表单输入多行时缺少或未对齐的值 - PHP/MYSQLI【英文标题】:Values missing or misaligned when entering multiple rows from form - PHP/MYSQLI 【发布时间】:2019-06-01 12:02:44 【问题描述】:

我正在尝试使用 php 将表单中的多行提交到 mysql 数据库中(使用 Insert multiple rows to database from html form using MySQLi 和其他多篇文章作为参考)。

我遇到的问题是第一行提交正常,但后续行提交了一些我没想到的值。

我尝试了多种循环遍历行的方法(见注释行)来解决这个问题,但还没有找到解决方案。

// $number = count($_POST["ps_id"]);
  // echo $number;
  $ps_id     = $_POST["ps_id"];
  $ds_id = $_POST["ds_id"];
  $hole_id = $_POST["hole_id"];
  $strokes = $_POST["strokes"];
  $hole_strokes  = $_POST["hole_strokes"];
  $par = $_POST["hole_par"];
  $pkey = $_POST["pkey"];
  $c_id  = $_POST["c_id"];
  $this_hole = $_POST["holeno"];

  $stmt = $con->prepare("INSERT INTO strokes (players_id, days_id, hole_id, strokes, points, net_strokes, par, pkey, course_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");

  // for ($i=0; $i<count($ds_id); $i++) 
  // for ($i=0; $i<$number; $i++) 
  foreach ($_POST['ps_id'] as $i => $ps_id)
      $ds_id = $ds_id[$i];
      $hole_id = $hole_id[$i];
      $strokes = $strokes[$i];
      $hole_strokes = $hole_strokes[$i];
      $par = $par[$i];
      $pkey = $pkey[$i];
      $c_id = $c_id[$i];
      // $ps_id = $ps_id[$i];
      $net_strokes += ((int)$strokes-(int)$hole_strokes);
      $pts += ((int)$par-(int)$net_strokes)+2;
      if ($pts < 0) 
        $points = 0;
       else 
        $points = $pts;
      

      $stmt->bind_param('iiiiiiiii', $ps_id, $ds_id, $hole_id, $strokes, $points, $net_strokes, $par, $pkey, $c_id);
      // echo "ds_id: ".$ds_id." and hole_id: ".$hole_id." and strokes: ".$strokes." and points: ".$points." and net_strokes: ".$net_strokes." and pts: ".$pts." and pkey: ".$pkey." and par: ".$par." and c_id: ".$c_id." and ps_id: ".$ps_id."<br><br><br>";

      $stmt->execute();

      // setcookie('g_holeno', $nexthole, 3600, '/');
      // header("Location: ../open/index.php?s=1");

  
      // echo "Success";
      $nexthole += (int)$this_hole[0]+1;
      // echo "Next hole: ".$nexthole;
  $stmt->close(); 

从表单提交时,后续行的值格式正确:

ps_id[]: 1
strokes[]: 4
ds_id[]: 7
pkey[]: 5
holeno[]: 1
hole_id[]: 19
hole_par[]: 4
c_id[]: 4
hole_strokes[]: 1
ps_id[]: 2
strokes[]: 4
ds_id[]: 7
pkey[]: 5
holeno[]: 1
hole_id[]: 19
hole_par[]: 4
c_id[]: 4
hole_strokes[]: 1
ps_id[]: 3
strokes[]: 4
...

...但是从第 2 行开始,只有 ps_id (players_id) 和 net_strokes 正确提交给 db:

id, days_id, hole_id, strokes, points, net_strokes, par, pkey, course_id, players_id
'103','7','19','4','3','3','4','5','4','1'
'104','0','9','0','2','3','0','0','0','2'
'105','0','0','0','1','3','0','0','0','3'
'106','0','0','0','0','3','0','0','0','4'

我怀疑该错误与计算的变量有关,但我自己无法找到解决方案。

【问题讨论】:

【参考方案1】:

您每次迭代都覆盖您的数据:

$ds_id = $ds_id[$i];     // array `$ds_id` becomes SINGLE value
$hole_id = $hole_id[$i]; // same here and so on
// etc

【讨论】:

以上是关于从表单输入多行时缺少或未对齐的值 - PHP/MYSQLI的主要内容,如果未能解决你的问题,请参考以下文章

Ant Design of Vue中a-form-model多行表单对齐和验证

Ant Design of Vue中a-form-model多行表单对齐和验证

JCheckBox:多行文本的垂直对齐

在 Laravel 5 中将多行从动态表单保存到数据库

根据在 cakephp 中选择的值向数据库添加多行

在展开操作期间遇到无效或未对齐的堆栈