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]) "
    }
    
    评论

报告相同问题?