I've been working on a script that will import data from a CSV file to phpMyAdmin database table.
Using this code was working ok:
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE c170720
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
But if I use this, it doesn't work:
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE '$m_cfile'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
This is the entire code:
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "smposf";
$con = mysqli_connect($host,$user,$password,$db);
$message = "";
if (isset($_POST['submit3']))
{
$m_date=date($sdate);
$m_yy=substr($m_date,2,2);
$m_mm=substr($m_date,5,2);
$m_dd=substr($m_date,8,2);
$m_cfile='c'.$m_yy.$m_mm.$m_dd;
$allowed = array('csv');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed))
{
// show error message
$message = 'Invalid file type, please use .CSV file!';
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "CSV/" .
$_FILES['file']['name']);
$file = "CSV/" . $_FILES['file']['name'];
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE '$m_cfile'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
if (!$result = mysqli_query($con, $query))
{
exit(mysqli_error($con));
}
$message = "CSV file successfully imported!";
}
}
?>
If I use
LOAD DATA LOCAL INFILE '$file' INTO TABLE c170720 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (ccode,custarea,ttlpkt)
it works totally fine, but I need variable $m_cfile
. This is because the user will create a file per day with a specific date. What is wrong?
将INTO TABLE '$m_cfile'
更改为INTO TABLE $m_cfile
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.