php - MySQL 插入 - 来自使用名称数组的输入的值。 (PHP)

标签 php mysql arrays post insert

我目前正在通过 mysql/php 生成一个表单。我有超过 1500 个具有唯一值的输入,我想将它们插入到 mysql 表中(1 个值/行)

我以这种方式创建表单:

echo "<input type='text' name='combination[]' value='justsometext". $row['name'] ."'><br />";

我有大约 1500 个这样的输入,我想将它们插入到一列中,我该怎么做?

我使用以下代码进行插入,但它只插入 0 而不是实际值:

foreach ($_POST['combination'] as $combination) {
$sql="INSERT INTO alphabet_combination (combination)
VALUES
('$combination')";
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}

最佳答案

首先:How can I prevent SQL injection in PHP?

第二:1500 个输入?哇...您必须仔细检查您的 php.ini 配置是否可以处理它。

如果您确实想将 1500 个值放入一列中 - 也许您应该考虑序列化数组并保持这种方式?

在准备好的声明中,这将如下所示:

$combinations = serialize($_POST['combination']);

$q = mysqli_prepare($con, 'INSERT INTO alphabet_combination (combination) VALUES (?)');
mysqli_stmt_bind_param($q, 's', $combinations);
mysqli_stmt_execute($q);

如果您想要每个值单个INSERT,那么提交到数据库后将是接下来的1500行:

$q = mysqli_prepare($con, 'INSERT INTO alphabet_combination (combination) VALUES (?)');
mysqli_stmt_bind_param($q, 's', $combination);

foreach ($_POST['combination'] as $combination) {
    mysqli_stmt_execute($q);
}

关于php - MySQL 插入 - 来自使用名称数组的输入的值。 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19879935/

相关文章:

php - 删除已连接的表

php - 我如何将此 UTF-8 字符串编码为 ASCII 以将其插入 latin1 数据库?

mysql - 更新记录时如何在 mysql 中避免此错误?

c - 尝试将字符串转换为整数数组的艰难时期

php - 值得在我的服务器上运行 memcache 吗?

php - 比较 PHP 中的 Unicode 字符

php - 使用 PHP include 时奇怪的上边距

java - 通过 Java 应用程序连接到另一台机器上的数据库

java - 错误 : exception in thread 'main' java. lang.ArrayIndexOutOfBoundsException: 0

javascript - 如何循环遍历音频对象数组