MySQL 插入 - 来自使用名称数组的输入的值.(菲律宾比索)


MySQL insert - Values coming from inputs using name array. (PHP)

我目前正在通过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));
}

首先:如何防止 PHP 中的 SQL 注入?

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

如果您真的想在一列中放置 1500 个值 - 也许您应该考虑serialize数组并保持这种状态?

在准备好的语句中,这将如下所示:

$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);
}

提交表单后尝试以下代码:

extract($_POST);
foreach ($combination as $comb) {
    //if your table has only one field inside:
    $query = mysql_query("INSERT INTO tablename VALUES('".$comb."')");
}

另一个想法以及表单的另一个示例:

<?php
if( isset($_POST['btnsubmit']) ){
    extract($_POST);
    foreach ($sample as $samp) {
        $query = mysql_query("INSERT INTO tablename VALUES('".$samp."')");
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <input type="text" name="sample[]" value="hello1"><br>
        <input type="text" name="sample[]" value="hello2"><br>
        <input type="text" name="sample[]" value="hello3"><br>
        <input type="text" name="sample[]" value="hello4"><br>
        <input type="text" name="sample[]" value="hello5"><br>
        <input type="submit" name="btnsubmit" value="Submit">
    </form>
</body>
</html>