Just reading Kevin Yank’s book PHP & MySQL: Novice to Ninja, so I thought I would try setting up a db for my students. I’m just a beginner. I’m using Ubuntu 18.04.3
As an exercise, I made createtable.html.php, it creates a table in the db allstudentsdb. It works and creates a new table according to the data I put in a form. No problem.
Next I want to populate the table from a csv file, so I made a form.html.php When I click the submit button I get this error:
Error importing /home/pedro/mycsvfiles/19BE1.csv’ This problem: SQLSTATE[HY000]: General error: 2000 LOAD DATA LOCAL INFILE is forbidden, check mysqli.allow_local_infile
my php.ini file has this:
; Allow accessing, from PHP’s perspective, local files with LOAD DATA statements
; http://php.net/mysqli.allow_local_infile
;changed by me to open
mysqli.allow_local_infile = On
However, when I go to a bash terminal and log in to mysql, I can do this:
LOAD DATA LOCAL INFILE ‘/home/pedro/mycsvfiles/17BE3.csv’ INTO TABLE 17bizeng CHARACTER SET utf8
FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘"’
LINES TERMINATED BY ‘\n’ IGNORE 1 LINES;
The above works perfectly. No error, the table is populated.
So I am a bit perplexed. Any tips please??
This is the code that is supposed to do the work (don’t laugh, I’m just learning this stuff!):
if (isset($_POST["tablename"])) {
include $_SERVER['DOCUMENT_ROOT'] . '/includes/studentdb.inc.php';
try
{
$pre = 'LOAD DATA LOCAL INFILE \'';
$fileName = '/home/pedro/mycsvfiles/' . $_POST['tablename'] . '.csv\'';
$tablename = $_POST['tablename'];
$intotable = ' INTO TABLE ';
$charset = ' CHARACTER SET utf8 ';
$rest = ' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' IGNORE 1 LINES;';
$sql = $pre . $fileName . $intotable . $tablename . $charset . $rest ;
echo $sql;
$s = $pdo->prepare($sql);
$pdo->exec($sql);
$output = 'The table "' . $tablename . '" was successfully created.';
include 'output.html.php';
exit();
}
catch (PDOException $e)
{
$output = 'Error importing ' . $fileName . 'table: ' . $e->getMessage();
include 'output.html.php';
exit();
}
}