简体   繁体   English

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

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

I am currently producing a form via mysql / php. 我目前正在通过mysql / php生成表单。 I have well over 1500 inputs with unique values and I would like to insert them into a mysql table (1 value / row) 我有超过1500个具有唯一值的输入,我想将它们插入到mysql表中(每行1个值)

I am creating the form this way: 我以这种方式创建表格:

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

I have around 1500 inputs like this and I would like to insert them into one column, how do I go about? 我大约有1500个这样的输入,我想将它们插入一列,我该怎么做?

I am using the following code to insert, but it is only inserting 0s instead of the actual values: 我正在使用以下代码进行插入,但是仅插入0而不是实际值:

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

First of all: How can I prevent SQL injection in PHP? 首先: 如何防止PHP中的SQL注入?

Second: 1500 inputs? 第二:1500个输入? Wow... You have to double check if your php.ini configuration will handle it. 哇...您必须仔细检查您的php.ini配置是否可以处理它。

If you really want to put 1500 values in one column - maybe you should consider to serialize array and keep it that way? 如果您真的想在一列中放入1500个值-也许您应该考虑serialize数组并保持这种状态?

In prepared statement this will look like this: 在准备好的语句中,它将如下所示:

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

If you want for each value single INSERT so after submit in database will be next 1500 rows: 如果您要为每个值单独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);
}

Try this code after submission of your form: 提交表单后,请尝试以下代码:

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

Another example for another idea along with the form: 另一个想法和形式的另一个示例:

<?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