duan198811 2016-10-17 10:02
浏览 197

在mysql数据库中插入多行(以逗号分隔的项目)

need help...!!! i have 2000 number of values like (3458,1356,....n) i want to post them from html input field as $_POST['roll']; along with few other columns which has similar values like board (dhaka,dhaka,dhaka) .. i want to insert them into database with php at once not one by one..

NOTE: i know there is a way to insert multiple rows but it will be time consuming to create that query for 2000 values.. so i want to use 2000 values at once with comma..

result should be like this

  +---------+-------------+
  |  board  |    roll     |
  +---------+-------------+
  |   dhaka |    3456     |
  |   dhaka |    4574     |
  |   dhaka |    6357     |
  |   dhaka |    2467     |
  +---------+-------------+

i am using this query to post single row at a time

 $board = $_POST['board'];
 $roll = $_POST['roll'];    
 $query = "INSERT INTO `host`.`result` (`board`, `roll`) VALUES ('$board','$roll') "
  • 写回答

1条回答 默认 最新

  • dongzhu7329 2016-10-17 10:18
    关注

    At first, you can use php explode() function to make an php array. Then you INSERT your data using loop depending on Array size.

    Code Example :

    $roll = array();
    $board = array();
    
    $roll = (explode(",",$_POST['roll']));
    $board = (explode(",",$_POST['board']));
    
    $arraySize = sizeof($roll);
    
    for($i=0; $i<$arraySize ; $i++){
       $query = "INSERT INTO `host`.`result` (`board`, `roll`) VALUES ($board[$i],$roll[$i]) "
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答