簡體   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