如何将csv文件数据插入数据库?

时间:2014-10-01 05:57:59

标签: php mysql csv

目前我正在用PHP上传一个项目,用于上传CSV文件并将数据插入数据库。上传CSV文件后,它会显示文件中带有复选框的数据。单击复选框后,值不会插入到表格中。

我使用的编码

  if(isset($_POST['upload']))
  {
  if (is_uploaded_file($_FILES['filename']['tmp_name']))
  {
      echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
  }

  $handle = fopen($_FILES['filename']['tmp_name'], "r");
  echo("<table border='1'>");
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  {
    echo("<tr>\r\n");
    foreach ($data as $index=>$val)
    {
        echo("\t<td><input type='checkbox' name='chk1[]' value='$val'>$val</td>\r\n");
    }

  }
  echo("</tr>\r\n");
  echo("</table>");
  echo("<input type='submit' name='insert' id='insert' value='Submit' />");
  fclose($handle);
  }

在上传文件时,会显示值,但点击提交按钮不会执行任何操作。

提交代码按钮是:

if(isset($_POST['insert']))
{
for($i=0;$i < sizeof($checkbox1);$i++)
{
$query = "INSERT INTO uploadmail (name,email) VALUES ('$name','".$checkbox1[$i]."')";
$result = mysql_query($query);
}
}

显示的值如下     enter image description here

请提出你的建议。提前了。

2 个答案:

答案 0 :(得分:0)

您可以从此网站http://sourceforge.net/projects/phpexcelreader/

下载reader.php和oleread.inc zip文件夹

以及在数据库中插入数据的其余代码

require_once'Excel / reader.php';

// ExcelFile($filename, $encoding);

$data = new Spreadsheet_Excel_Reader();

// Set output Encoding.

$data->setOutputEncoding('CP1251');



$data->read($_FILES['excel_upload']['tmp_name']);



error_reporting(E_ALL ^ E_NOTICE);



for ($i=1;$i<=$data->sheets[0]['numRows'];$i++) 

{   
          $EmailAdd   = $data->sheets[0]['cells'][$i][1];

          $NameAdd    = $data->sheets[0]['cells'][$i][2];

          $Phone      = $data->sheets[0]['cells'][$i][3];

          $Address    = mysql_real_escape_string($data->sheets[0]['cells'][$i][4]);


             if(filter_var($EmailAdd, FILTER_VALIDATE_EMAIL))

                  {

                    $currentId = mysql_query("INSERT INTO `Student` (`StudentEmail_email`,StudentEmail_Name`,`StudentEmail_Mobile`,`StudentEmail_Address`) VALUES ('$EmailAdd','$NameAdd','$ContactAdd','$Address')");

                  }


}

答案 1 :(得分:0)

说明:

  • 我在/* */引用的代码中包含了一些解释。
  • 我将您的插入查询更改为 MySQLi Prepared Statement 而不是 弃用了MySQL

这是您可以使用的代码。

if(isset($_POST['upload']))
  {
  if (is_uploaded_file($_FILES['filename']['tmp_name']))
  {
      echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
  }

  $handle = file_get_contents($_FILES['filename']['tmp_name']); /* GET THE CONTENT OF UPLOADED FILE */

  $val = explode(',',$handle);
  $count = count($val); /* COUNT THE TOTAL EXPLODED RESULT */

  echo("<table border='1'>");

    for($x=0;$x<=$count;$x++){ /* START THE LOOP BASED FROM THE COUNTED RESULT */

    echo("<tr>\r\n");

        echo("\t<td><input type='checkbox' name='chk1[$x]' value='$val[$x]'>$val[$x]</td>\r\n"); /* GET THE VALUE AND STORE IT INTO THE chk1 ARRAY */

    echo("</tr>\r\n");

    } /* END OF FOR LOOP */

  echo("</table>");
  echo("<input type='submit' name='insert' id='insert' value='Submit' />");

} /* END OF ISSET UPLOAD */

我刚注意到你没有move_uploaded_file。或者您只是没有将其包含在示例代码中?

选中/选中复选框并提交表格后:

if(isset($_POST['insert']))
{
  $comp=count($_POST['chk1']); /* COUNT THE TOTAL SUBMITTED CHECKBOX */
  $checkbox1=$_POST['chk1'];

  for($i=0;$i<=$comp;$i++)
  {
    if(!empty($checkbox1[$i])){ /* IF SUBMITTED DATA IS CHECKED */
      /* WHERE DOES YOUR $name VARIABLE CAME FROM? */
      $stmt = $YourSQLConnection->prepare("INSERT INTO uploadmail (name,email) VALUES (?, ?)");
      $stmt->bind_param('ss', $name,$checkbox1[$i]); /* BIND THE VALUE TO STRINGS */
    }
  }
}