尝试使用php和mysql将csv文件上传到mysql

时间:2016-06-24 11:25:19

标签: php html

大家好日子,我对php很新,我真的需要一些帮助。我正在尝试使用php和html表单将csv文件上传到mysql数据库,但它给了我以下错误:

注意:未定义的索引:第23行的C:\ xampp \ htdocs \ import csv \ importcsv.php中的temp_name

警告:fopen():第24行的C:\ xampp \ htdocs \ import csv \ importcsv.php中的文件名不能为空

警告:fgetcsv()期望参数1为资源,第26行的C:\ xampp \ htdocs \ import csv \ importcsv.php中给出布尔值

警告:fclose()要求参数1为资源,在第30行成功导入C:\ xampp \ htdocs \ import csv \ importcsv.php时给出布尔值

这是我的代码

       <?php 
         ini_set('display_errors', 1);
         error_reporting(E_ALL);
         $db_host = 'localhost';
         $db_user = 'root';
         $db_pwd = '';

         $db = 'mydb';
         $table = 'user';

        $conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Cant connect to database");


         if(!mysqli_select_db($conn,$db))
         die("Cant select to database");

         if(isset($_POST['submit'])){
         $fname = $_FILES['sel_file']['name'];
         echo 'upload file name: '.$fname.' ';
         $chk_ext = explode(".",  $fname);

         if(strtolower(end($chk_ext)) == "csv"){ 
          $filename = $_FILES['sel_file']['temp_name'];
          $handle = fopen($filename, "r");

          while(($data = fgetcsv($handle, 1000, ",")) != FALSE){
          $sql = "INSERT into user(name,email,phone)  values('$data[0]',   '$data[1]', '$data[2]')";
          mysql_query($sql) or die(mysql_error());
           }
           fclose($handle);
           echo "successfully imported";
          }
           else{
              echo "Invalid file";
            }
         }

      ?>

       <h1>Import csv file</h1>
       <form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'  enctype="multipart/form-data">
       Import File : <input type='file' name='sel_file' size='20'>
       <input type='submit' name='submit' value='submit'>
      </form>

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我已更新代码中的一些行。

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$db = 'mydb';
$table = 'user';

$conn = mysqli_connect($db_host, $db_user, $db_pwd, $db) or die("Cant connect to database");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (isset($_POST['submit'])) {
    $fname = $_FILES['sel_file']['name'];
    echo 'upload file name: ' . $fname . ' ';
    $chk_ext = explode(".", $fname);

    if (strtolower(end($chk_ext)) == "csv") {
        $filename = $_FILES['sel_file']['tmp_name'];
        $handle = fopen($filename, "r");

        while (($data = fgetcsv($handle, 1000, ",")) != FALSE) {
            $sql = "INSERT into user(name,email,phone)  values('".$data[0]."',   '".$data[1]."', '".$data[2]."')";
            mysqli_query($conn, $sql) or die(mysqli_error($conn));
        }
        fclose($handle);
        echo "successfully imported";
    } else {
        echo "Invalid file";
    }
}
?>

<h1>Import csv file</h1>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'  enctype="multipart/form-data">
    Import File : <input type='file' name='sel_file' size='20'>
    <input type='submit' name='submit' value='submit'>
</form>