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.
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!!";