dox19458 2013-11-09 17:36
浏览 15
已采纳

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

I am currently producing a form via 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)

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?

I am using the following code to insert, but it is only inserting 0s instead of the actual values:

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

2条回答 默认 最新

  • douling8772 2013-11-09 18:43
    关注

    First of all: How can I prevent SQL injection in PHP?

    Second: 1500 inputs? Wow... You have to double check if your php.ini configuration will handle it.

    If you really want to put 1500 values in one column - maybe you should consider to serialize array and keep it that way?

    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:

    $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?