I remember seeing something like this in various example codes I looked at online:
However, when I try to use one, it feeds back an Undefined Index
error.
Notice: Undefined index: userfile in C:\xampp\htdocs\ccrp\api\import.php on line 6
<?php
include('dbconnect.php');
if(isset($_POST['btn_upload'])){
$filename = $_FILES['userfile']['name'];
$file = fopen($filename,"r");
// Read and throw away the header row
fgetcsv($file);
$stmt = $pdo->prepare("INSERT INTO test_import (last_name, first_name, middle_name, suffix, residential_address, mailing_address, precinct, age, ethnicity, gender, party, race, phone) VALUES (:last_name, :first_name, :middle_name, :suffix, :residential_address, :mailing_address, :precinct, :age, :ethnicity, :gender, :party, :race, :phone)");
while (! feof($file)) {
$row = fgetcsv($file);
$stmt->bindParam(':last_name', $row[0]);
$stmt->bindParam(':first_name', $row[1]);
$stmt->bindParam(':middle_name', $row[2]);
$stmt->bindParam(':suffix', $row[3]);
$stmt->bindParam(':residential_address', $row[4]);
$stmt->bindParam(':mailing_address', $row[5]);
$stmt->bindParam(':precinct', $row[6]);
$stmt->bindParam(':age', $row[7]);
$stmt->bindParam(':ethnicity', $row[8]);
$stmt->bindParam(':gender', $row[9]);
$stmt->bindParam(':party', $row[10]);
$stmt->bindParam(':race', $row[11]);
$stmt->bindParam(':phone', $row[12]);
$stmt->execute();
}
fclose($file);
header("Location: ../members_list.php");
}
if(isset($_POST['btn_back'])) {
header("Location: ../members_list.php");
}
?>