I've just started out programming in PHP & MYSQL. I'm wanting to create a CSV file importer that then writes entries to a database, but with various validations. I wanted to add a current time and date to the datetime column in which any products that are discontinued when the database says "Yes" will issue a date and time of the product being discontinued. I also want to add entries to a PDF file/ report format of various error conditions. The errors would include any entries that did not pass the if conditions such as regular expressions and were not added to the database.
<?php
include_once('connection.php');
date_default_timezone_set('Europe/London');
$date = date('d/m/y h:i:s a', time());
$filetxt = "./errors.txt";
$var1 = 5;
$var2 = 1000;
$var3 = 10;
if(isset($_POST["Import"]))
{
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while(($emapData = fgetcsv($file, 10000, ",")) !==FALSE)
{
// adds data to the sql database
if($var1 <= $emapData[3] && $var3 <= $emapData[4] && $var2 >= $emapData[4] && preg_match("/^[a-zA-Z0-9]+$/", $value) == $emapData[1] && preg_match("[a-zA-Z]", $value) == $emapData[2] && preg_match("[a-zA-Z]", $value) == $emapData[6]){
$sql = "INSERT INTO tblproductdata(strProductCode, strProductName, strProductDesc, intStock, intPrice, dtmAdded, dtmDiscontinued) VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$date','$date')";
echo "test 1";
}
else if($var1 <= $emapData[3] && $var3 <= $emapData[4] && $var2 >= $emapData[4] && preg_match("/^[a-zA-Z0-9]+$/", $value) == $emapData[1] && preg_match("[a-zA-Z]", $value) == $emapData[2] && preg_match("[\s]", $value) == $emapData[6]){
$sql = "INSERT INTO tblproductdata(strProductCode, strProductName, strProductDesc, intStock, intPrice, dtmAdded, dtmDiscontinued) VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','null','null')";
echo "test 2";
}
else{
$write = "$emapData[0], $emapData[1], $emapData[2], $emapData[3], $emapData[4], $emapData[5], $emapData[6], $emapData[7]
";
file_put_contents($filetxt , $write , FILE_APPEND);
}
$res=$conn->query($sql);
}
echo "$sql";
fclose($file);
echo "CSV File has successfully been Imported";
}
else
{
echo "Invalid File: Please Upload a Valid CSV File";
}
}
header("Location: index.php");
?>
I especially need help with adding the unsuccessful database entries to a PDF.