Я пытаюсь импортировать данные из файла CSV в MySQL, используя php. Я запустил этот код, и я не уверен, почему он не записывает информацию. Я проверил свою рекомендацию $ q, и она работает в phpadmin.
Я получаю сообщение об ошибке "Импорт не выполнен"
Не могли бы вы взглянуть и сказать, где я пошло не так? Спасибо.
<?php
class csv extends mysqli
{
private $state_CSV = false; //Validate the status of the CSV file
public function __construct()
{
parent::__construct("localhost","root","*******","db");
if ($this->connect_error) {
echo "Fail to connect to Database : ". $this->connect_error;
}
}
//import file in to read all data one by one
public function import ($file)
{
if (($file = fopen ($file,'r')) !==FALSE) //r = read
{
while (($row = fgetcsv($file,1000,",")) !== FALSE) {
//process data from each column
$serial = $row[1] ;
$status = $row[2];
$title = $row[3];
$category = $row[4];
$synopsis = $row[5];
//Insert value into the database
$q = " INSERT INTO movie (title,synopsis,category)
VALUES ('$title','$synopsis','$category');
INSERT INTO inventory (serialnumber,status,movie_id)
VALUES ('$serial','$status',LAST_INSERT_ID())
";
//echo $q; //open here to show what the quary look like
echo $q;
if ( $this->query($q)){
$this->state_CSV = true; // function will now return the ID instead of true.
}else{
$this->state_CSV = false;
echo "<br/>error INSERT ". $title. "<br/>"; //if is not run perfectly
}
}
//showing notification, problem
if ($this->state_CSV) {
echo "Your data has been successfully imported";
}else{
//echo "Something went wrong while importing your data";
echo "<br/> <br/> Import is not successful";
}
}
}
}
?>