使用PHP MySQL从前端CSV INPUT

时间:2017-04-18 14:33:52

标签: php mysql phpmyadmin

我想使用来自网站前端的CSV文件将数据插入phymyadmin表,该文件必须更新我表格中的所有记录。

此代码根本不起作用。请找出错误。

    <?php

   $servername = "localhost";
   $username = "";
             $password = "";
             $dbname = "";

             $conn = mysql_connect($servername, $username , $password );  

              if(! $conn ) {
      die('Could not Find Some Error Occured: ' . mysql_error());
                }

                if(isset($_POST["submit"]))
             {
              if($_FILES['file']['name'])
              {
               $filename = explode(".", $_FILES['file']['name']);
               if($filename[1] == 'csv')
               {
                $handle = fopen($_FILES['file']['tmp_name'], "r");
                while($data = fgetcsv($handle))
                {
                 $uname = mysqli_real_escape_string($connect, $data[0]);  
                 $pass = mysqli_real_escape_string($connect, $data[1]);

                 mysql_select_db('desiresq_record');

            $query = "INSERT into login (username, password)
                               VALUES        ('$uname','$pass')";
                   mysqli_query($connect, $query);
      }
      fclose($handle);
      echo "<script>alert('Import done');</script>";
     }
    }
   }
   ?> 

1 个答案:

答案 0 :(得分:0)

您在脚本中同时使用MySQL和MySQLi扩展程序。

我建议just sticking to MySQLi,因为MySQL已被弃用,因安全问题而无法使用。

祝你好运!