I'm currently working on a sample system that can upload csv
files to my MySQL
database.
No data is being inserted in my database anymore.
My code used to work before, after a few uploads of data this started happening. Every time I upload my csv
, it gives me an error of:
Notice: Undefined variable: SQL in C:\xampp\htdocs\intern\orders.php on line 43
Upload source code:
$con = mysqli_connect('localhost','root','') or die (mysql_error());
mysqli_select_db($con, 'test');
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if (isset($_POST['submit'])) {
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
$num = 0;
if (in_array($_FILES['file']['type'],$mimes)) {
while (($fileop = fgetcsv($handle,2000,",")) !== false) {
if ($num == 0) {
$num++;
} else {
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
}
}
} else {
echo '<script language="javascript">';
echo 'alert("INVALID FILE FORMAT.")';
echo '</script>';
die(header("refresh:0;"));
}
if ($sql) {
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
} else {
echo "error";
}
}