我目前正在通过 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/