I made a function in PHP to write into a SQL table the data from a bunch of csv files that are present in a folder.
The function perfectly works on the XAMPP server on my PC, it creates the table and it writes thousands of lines taken from dozens of CSV files, but when I exported the function on the online-server the table is correctly created, the CSV files are correctly opened (I checked it with print_r), but the CSV data are not written into the and SQL table and no error messages are displayed.
This is my code:
<?PHP
include('sys/dbconnect.php');
$sql1 = "CREATE TABLE IF NOT EXISTS
milk
(
milk_record int(10) NOT NULL,
panel_number int(3) NOT NULL,
...etc.... (41 columns in total)
PRIMARY KEY (milk_record)
)";
if($conn->query($sql1) === TRUE) {echo "Table ready <br>";}
else { echo "Error creating database: " . $conn->error . "<br>";}
$conn->close();
$dir = 'csvfiles/';
$files = scandir($dir);
$fileCount = 0;
foreach($files as $fn)
{
if($fileCount < 2) { $fileCount++; continue;}
$filename=$dir.$fn;
if ($fn)
{
$directory = 'csvfiles/';
// echo "- file: ".$directory.$fn." <br> ";
include('sys/dbconnect.php');
ini_set('auto_detect_line_endings',TRUE);
$file = fopen($filename, "r");
set_time_limit (240);
while ($getData = fgetcsv($file, 0, ',', '"'))
{
// print_r($getData);
$sql = "INSERT INTO milk
(
milk_record,
panel_number,
....etc.... (41 columns in total)
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
$stmt = mysqli_prepare($conn, $sql);
$stmt->bind_param
( "sssssssssssssssssssssssssssssssssssssssss",
$getData[0], $getData[1], $getData[2], $getData[3], $getData[4], $getData[5], $getData[6], $getData[7], $getData[8], $getData[9],
$getData[10], $getData[11], $getData[12], $getData[13], $getData[14], $getData[15], $getData[16], $getData[17], $getData[18], $getData[19],
$getData[20], $getData[21], $getData[22], $getData[23], $getData[24], $getData[25], $getData[26], $getData[27], $getData[28], $getData[29],
$getData[30], $getData[31], $getData[32], $getData[33], $getData[34], $getData[35], $getData[36], $getData[37], $getData[38], $getData[39],
$getData[40]
);
$sql = "INSERT into milk() values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stmt->execute();
}
fclose($file);
$delete_first_line = "DELETE FROM milk WHERE milk_record =0;";
$delete = mysqli_query($conn, $delete_first_line);
mysqli_close($conn);
?>