CSV数据导入问题

时间:2016-08-01 22:08:31

标签: php mysql csv

最近你好我已经下载了一个用于将数据导入数据库的脚本。我已经创建了一个表单,用户将上传文件并从中我希望将数据添加到数据库中我不确定我是否正确执行或因为我收到500内部服务器错误的错误可能是我的代码混乱起来。

require('../admin/includes/connection.php');

$file = $_FILES['data_email']['name'];
$file_temp = $_FILES['data_email']['tmp_name'];
move_uploaded_file($file_temp, '../admin/uploads/'.$file);
// path where your CSV file is located
define('CSV_PATH','http://wintroninformatics.com/admin/uploads/');
// Name of your CSV file
$csv_file = CSV_PATH . $file; 

$data = mysqli_query($connection, 'SELECT * FROM users WHERE uid = "'.$_SESSION['uid'].'"');
$user = mysqli_fetch_array($data);
if (($handle = fopen($csv_file, "r")) !== FALSE) {
   fgetcsv($handle);   
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        for ($c=0; $c < $num; $c++) {
          $col[$c] = $data[$c];
        }

    $col2 = $col[1];

// SQL Query to insert data into DataBase
$query = "INSERT INTO email_data(date_uploaded, email_id, username, status) VALUES('".date('d-m-Y')"','".$col2."','".$user['username']."', 'Approved')";
$s     = mysqli_query($connection, $query);
 }
    fclose($handle);
}

echo "File data successfully imported to database!!";

1 个答案:

答案 0 :(得分:-2)

不能这样调试。 500内部服务器错误意味着您没有启用错误报告。

在代码顶部添加以下行,以发现代码中的问题

error_reporting(E_ALL);