繁体   English   中英

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

[英]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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM