I'm trying to import a CSV file into my database using PHP and HTML. I used this code but I got an error:
Warning: mysqli::query(): Empty query
<?php
extract(filter_input_array(INPUT_POST));
$fichier=$_FILES["userfile"]["name"];
if ($fichier) {
$fp = fopen($_FILES["userfile"]["tmp_name"], "r");
} else {
?>
<p align="center">-> successful import</p>
<p align="center">-import fails</p>
<?php
exit();
}
$cpt=0;
?>
<p align="center">successful import</p>
<?php
while(!feof($fp)) {
$ligne = fgets($fp,4096);
$liste = explode(";", $ligne);
$table = filter_input(INPUT_POST, 'userfile');
$liste[0] = isset($liste[0]) ? $liste[0] : Null;
$liste[1] = isset($liste[1]) ? $liste[1] : Null;
$liste[2] = isset($liste[2]) ? $liste[2] : Null;
$liste[3] = isset($liste[3]) ? $liste[3] : Null;
$liste[4] = isset($liste[4]) ? $liste[4] : Null;
$liste[5] = isset($liste[5]) ? $liste[5] : Null;
$liste[6] = isset($liste[6]) ? $liste[6] : Null;
$liste[7] = isset($liste[7]) ? $liste[7] : Null;
$liste[8] = isset($liste[8]) ? $liste[8] : Null;
$liste[9] = isset($liste[9]) ? $liste[9] : Null;
$liste[10] = isset($liste[10]) ? $liste[10] : Null;
$liste[11] = isset($liste[11]) ? $liste[11] : Null;
$liste[12] = isset($liste[12]) ? $liste[12] : Null;
$champs1 = $liste[0];
$champs2 = $liste[1];
$champs3 = $liste[2];
$champs4 = $liste[3];
$champs5 = $liste[4];
$champs6 = $liste[5];
$champs7 = $liste[6];
$champs8 = $liste[7];
$champs9 = $liste[8];
$champs10 = $liste[9];
$champs11 = $liste[10];
$champs12 = $liste[11];
$champs13 = $liste[12];
if ($champs1 != ''){
$cpt++;
$db = new mysqli('localhost','root','', 'BD_Conference');
$sql = mysql_query("INSERT INTO tbl_conference (pid, name, chairs, keynote, abstract, speaker, affilation, ville, pays, salle, date, time, session, image_url) VALUES ('','$champs1','$champs2','$champs3','$champs4','$champs5','$champs6','$champs7','$champs8','$champs9','$champs10','$champs11','$champs12','$champs13')");
$result = $db-> query($sql) ;
}
}
fclose($fp);
?>
<h2>sum of inserted row:</h2> <b><?php echo $cpt;?></b>
and here is the HTML code I used. When I try to add a CSV file, an error displays:
Warning: mysqli::query(): Empty query
<h3>choisir un CSV :</h3>
<form method="post" enctype="multipart/form-data"
action="import.php">
<input name="userfile" type="file" value="table">
<input name="submit" type="submit" value="Importer">
</form>