使用一个PHP表单在MySQL中插入多条记录

使用一个PHP表单在MySQL中插入多条记录,php,mysql,bulkinsert,Php,Mysql,Bulkinsert,使用一个PHP表单在MySQL中插入多条记录 简单形式 <form action="process.php" method="post"> <p><label>Beamline ID</label> <input type="text" name="bline_id[][bline_id]" /> <label>Flow</label> <input type="text" name="flow[][flo

使用一个PHP表单在MySQL中插入多条记录

简单形式

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<input name="Submit" type="submit" />
</form>

插入结果当然是5行,但没有为$bline_id或$flow插入数据。但是看看数组,这是正确的数据。

改用PDO或Mysqli,这些扩展具有prepare选项,因此您需要ony来传递查询一次,并且使用while循环来更改数据

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]"  value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>

改为使用PDO或Mysqli,这些扩展具有prepare选项,因此您需要ony来传递查询一次,并使用while循环来更改数据

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]"  value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>

好的。根据你告诉我的,这是我想出的解决办法。我不会给你密码;它违背了你写它的意义

我会让用户在两个输入文本字段bline_id和flow中输入一些值

当他们点击提交按钮时,这些值被添加到PHP代码中的

然后将数组转换为字符串,并存储为字符串

在输入每个值时,您需要修改cookie,以便将其转换为数组,将新值添加到数组中,然后重复

当用户点击另一个按钮时,存储在数据库中。此选项将通过数组中的每个元素和数据库中的每个值取消cookie序列化

还要记住,您有两个值:bline_id和flow。这些可以存储在两个数组中,并作为两个cookie存储。这意味着您要对两个不同的阵列而不是一个阵列执行此过程


最后,您应该使用PDO对象。我已经通过商店链接将您链接到它。这是推荐的方法。

好的。根据你告诉我的,这是我想出的解决办法。我不会给你密码;它违背了你写它的意义

我会让用户在两个输入文本字段bline_id和flow中输入一些值

当他们点击提交按钮时,这些值被添加到PHP代码中的

然后将数组转换为字符串,并存储为字符串

在输入每个值时,您需要修改cookie,以便将其转换为数组,将新值添加到数组中,然后重复

当用户点击另一个按钮时,存储在数据库中。此选项将通过数组中的每个元素和数据库中的每个值取消cookie序列化

还要记住,您有两个值:bline_id和flow。这些可以存储在两个数组中,并作为两个cookie存储。这意味着您要对两个不同的阵列而不是一个阵列执行此过程


最后,您应该使用PDO对象。我已经通过商店链接将您链接到它。这是推荐的方法。

OMG!!!!我终于明白了!那是我愚蠢的表现

更正如下----


天哪!!!!我终于明白了!那是我愚蠢的表现

更正如下----



那么您想在数据库中保存相同的数据30次?相同类型的数据。相同的字段,不同的值。因此,用户每次单击按钮都会将多个值加载到一个数组中,然后单击一个按钮,您的代码会将它们全部发送到数据库?为了更清楚。。。每一行都有一个id bline_id,该id与另一个表中的id相对应。请在表单中输入数据,其中包含30条记录和提交的值。要在数据库中保存30次相同的数据,请输入相同类型的数据。相同的字段,不同的值。因此,用户每次单击按钮都会将多个值加载到一个数组中,然后单击一个按钮,您的代码会将它们全部发送到数据库?为了更清楚。。。每一行都有一个id bline_id,该id与另一个表中的id相对应。以包含30条记录的值的形式输入数据,然后提交@perry。这太好了,我还没有找到一个我可以理解的示例或教程……你忘了这个值中的冒号:value1,value2,value3,我必须修复它。@perry,那太好了,我还没有找到一个我可以理解的例子或教程……你忘了这个值中的冒号:value1,value2,value3,我必须修复它。好的,谢谢!还有一个问题。。。当输入此数据时,internet连接并非始终可用。它是否仍然有效?它将存储在会话cookie中。关闭浏览器时,数据将丢失。另一种选择是将数据存储在持久cookie中。这个曲奇饼能吃多久就吃多久。看看我链接到您的setcookie方法。它告诉你如何制作它们。如果这回答了您的问题,请务必将其标记为正确答案:我需要一段时间才能知道这是否回答了我的问题,因为我必须能够将其全部放在一起!非常感谢您抽出时间:没关系:祝您好运,如果您再次陷入困境,请随时提问!经过大量的阅读、尝试和错误,我有点接近了,但此刻我陷入了困境,需要更多的输入!好的,谢谢!一米
矿石问题。。。当输入此数据时,internet连接并非始终可用。它是否仍然有效?它将存储在会话cookie中。关闭浏览器时,数据将丢失。另一种选择是将数据存储在持久cookie中。这个曲奇饼能吃多久就吃多久。看看我链接到您的setcookie方法。它告诉你如何制作它们。如果这回答了您的问题,请务必将其标记为正确答案:我需要一段时间才能知道这是否回答了我的问题,因为我必须能够将其全部放在一起!非常感谢您抽出时间:没关系:祝您好运,如果您再次陷入困境,请随时提问!经过大量的阅读、尝试和错误,我有点接近了,但此刻我陷入了困境,需要更多的输入!
<?php

// pdo example

$sql = 'INSERT INTO `table` (field1, field2, field3) VALUES (:value1, :value2, :value3)';

// $dbh is pdo connection
$insertTable = $dbh->prepare($sql);

$countArray = count($array);
$i = 0;

while ($i < $countArray) {
   $insertTable->bindParam(':value1', $array[1][$i], PDO::PARAM_INT); // if value is int
   $insertTable->bindParam(':value2', $array[2][$i], PDO::PARAM_STR); // if value is str
   $insertTable->bindParam(':value3', $array[3][$i], PDO::PARAM_STR);
   $insertTable->execute();

   $i++;
}

?>
<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]"  value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>