错误批量插入PHP!我该怎么办?

时间:2014-08-21 08:10:03

标签: php bulkinsert

我使用此代码

$values = explode("\n", $_POST['textarea_name']);
mysqli_query($con,"INSERT INTO `table` (`col`) VALUES ($values)");

数据应该是字符串。我怎么能把它转换成字符串?我想让每一行都成为一个记录!让我的代码完整!

print_r($values);

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 0 [10] => 1 [11] => 2 [12] => 3 [13] => 4 [14] => 5 [15] => 6 [16] => 7 [17] => 8 [18] => 9 [19] => 0 )

我想知道如何将此值插入Mysql数据库。

2 个答案:

答案 0 :(得分:1)

foreach($values as $value){
 mysqli_query($con,"INSERT INTO `table` (`col`) VALUES ($value)");
}

编辑:

   <form action="test.php" method="post">
     <textarea name="textarea_name">test content......</textarea>
     <input type="submit" />
   </form>

在test.php中

 include('dbconnection.php');
 if(isset($_POST['textarea_name'])){
  $values=explode("\n",$_POST['textarea_name'])
  foreach($values as $value){
   mysqli_query($con,"INSERT INTO `table` (`col`) VALUES ($value)");
  }
 }
你想这样吗?

答案 1 :(得分:0)

$values = explode("\r\n", $_POST['textarea_name']);

//echo "<pre>";print_r($values);exit;

foreach($values as $value){
    mysqli_query($con,"INSERT INTO `table` (`col`) VALUES ('$value')");
}