Mark Alan Mark Alan - 1 year ago 119
MySQL Question

CSV data importing issue

Hello recently I have downloaded a script for importing data to the database. I have created a form where user will upload the file and from that I would like the data to added to the database I am not sure I am doing correctly or not as I am getting an error of 500 internal server error might be my code messed up.


$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
// 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) {
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);

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

Answer Source

Cant debug this way. 500 Internal server error means you have no error reporting enabled.

Add the following line at the top of your code to spot the problem in your code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download