probleme insertion bdd
Posté : 23 sept. 2021, 14:05
bonjour
j'ai retouche un code pour inserer un .csv dans du sql mais quand je tente de les rentrer dans la bdd j'ai des erreurs incomprehensibles
par contre lorsque je désactive les modules de la base de données , j'ai un retour de tableau correct
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "ggg"
[1]=>
string(3) "ttt"
[2]=>
string(2) "54"
}
[1]=>
array(3) {
[0]=>
string(4) "bbbb"
[1]=>
string(3) "fff"
[2]=>
string(2) "21"
}
[2]=>
array(3) {
[0]=>
string(3) "ggg"
[1]=>
string(3) "kkk"
[2]=>
string(2) "78"
}
[3]=>
array(3) {
[0]=>
string(3) "jjj"
[1]=>
string(3) "sss"
[2]=>
string(2) "44"
}
}
voici le code :
<?php
$filename = 'amazon-fours.csv';
// The nested array to hold all the arrays
$the_big_array = [];
// Open the file for reading
if (($h = fopen("{$filename}", "r")) !== FALSE)
{
// Each line in the file is converted into an individual array that we call $data
// The items of the array are comma separated
while (($data = fgetcsv($h, 0, ";")) !== FALSE)
{
// Each individual array is being pushed into the nested array
$the_big_array[] = $data;
}
// Close the file
fclose($h);
}
include("db_connect.php");
// module d'enregistrement #1
mysqli_query($conn,'LOAD DATA LOCAL INFILE \'amazon-fours.csv\' INTO TABLE produits
FIELDS TERMINATED BY \';\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\' ')
or die("Erreur mise à jour table : ".mysqli_error($conn));
/*
// module d'enregistrement #2
$data = [
'name' => $the_big_array[1],
'description' => $the_big_array[2],
'prix' => $the_big_array[3]
];
$sql = "INSERT INTO file VALUES (NULL,:name,:description,:prix)";
$stmt= $conn->prepare($sql);
$stmt->execute($data);
// afficher le code
echo "<pre>";
var_dump($the_big_array);
echo "</pre>";
*/
?>
db_connect.php
<?php
$server = "localhost";
$username = "*******";
$password = "*******";
$db = "*********";
$conn = mysqli_connect($server, $username, $password, $db);
session_start();
$_SESSION[`connect`] = $conn ;
?
j'ai essayé par le module d'enregistrement #1 mais j'ai cette erreur :
Erreur fatale : Uncaught TypeError : mysqli_query() : L'argument #1 ($mysql) doit être de type mysqli, PDO donné dans /home/u320889525/domains/*********/public_html/importcsv.php:24 Stack trace : # 0 /home/u320889525/domains/*********/public_html/importcsv.php(24): mysqli_query() #1 {main} jeté dans /home/u320889525/domains/*********/public_html/importcsv. php à la ligne 24
j'ai donc essayé la module d'enregistrement #2
mais la aussi j'ai un message d'erreur :
Avertissement : Conversion de tableau en chaîne dans /home/u320889525/domains/*********/public_html/importcsv.php à la ligne 41
Erreur fatale : Uncaught PDOException : SQLSTATE[21S01] : La liste des valeurs d'insertion ne correspond pas à la liste des colonnes : 1136 Colonne le nombre ne correspond pas au nombre de valeurs à la ligne 1 dans /home/u320889525/domains/********/public_html/importcsv.php:41 Trace de pile : #0 /home/u320889525/domains/********/public_html /importcsv.php(41): PDOStatement->execute() #1 {main} jeté dans /home/u320889525/domains/*********/public_html/importcsv.php à la ligne 41
j'ai verifié db_connect la connection est bonne
version php 8.0
j'ai retouche un code pour inserer un .csv dans du sql mais quand je tente de les rentrer dans la bdd j'ai des erreurs incomprehensibles
par contre lorsque je désactive les modules de la base de données , j'ai un retour de tableau correct
array(4) {
[0]=>
array(3) {
[0]=>
string(6) "ggg"
[1]=>
string(3) "ttt"
[2]=>
string(2) "54"
}
[1]=>
array(3) {
[0]=>
string(4) "bbbb"
[1]=>
string(3) "fff"
[2]=>
string(2) "21"
}
[2]=>
array(3) {
[0]=>
string(3) "ggg"
[1]=>
string(3) "kkk"
[2]=>
string(2) "78"
}
[3]=>
array(3) {
[0]=>
string(3) "jjj"
[1]=>
string(3) "sss"
[2]=>
string(2) "44"
}
}
voici le code :
<?php
$filename = 'amazon-fours.csv';
// The nested array to hold all the arrays
$the_big_array = [];
// Open the file for reading
if (($h = fopen("{$filename}", "r")) !== FALSE)
{
// Each line in the file is converted into an individual array that we call $data
// The items of the array are comma separated
while (($data = fgetcsv($h, 0, ";")) !== FALSE)
{
// Each individual array is being pushed into the nested array
$the_big_array[] = $data;
}
// Close the file
fclose($h);
}
include("db_connect.php");
// module d'enregistrement #1
mysqli_query($conn,'LOAD DATA LOCAL INFILE \'amazon-fours.csv\' INTO TABLE produits
FIELDS TERMINATED BY \';\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\' ')
or die("Erreur mise à jour table : ".mysqli_error($conn));
/*
// module d'enregistrement #2
$data = [
'name' => $the_big_array[1],
'description' => $the_big_array[2],
'prix' => $the_big_array[3]
];
$sql = "INSERT INTO file VALUES (NULL,:name,:description,:prix)";
$stmt= $conn->prepare($sql);
$stmt->execute($data);
// afficher le code
echo "<pre>";
var_dump($the_big_array);
echo "</pre>";
*/
?>
db_connect.php
<?php
$server = "localhost";
$username = "*******";
$password = "*******";
$db = "*********";
$conn = mysqli_connect($server, $username, $password, $db);
session_start();
$_SESSION[`connect`] = $conn ;
?
j'ai essayé par le module d'enregistrement #1 mais j'ai cette erreur :
Erreur fatale : Uncaught TypeError : mysqli_query() : L'argument #1 ($mysql) doit être de type mysqli, PDO donné dans /home/u320889525/domains/*********/public_html/importcsv.php:24 Stack trace : # 0 /home/u320889525/domains/*********/public_html/importcsv.php(24): mysqli_query() #1 {main} jeté dans /home/u320889525/domains/*********/public_html/importcsv. php à la ligne 24
j'ai donc essayé la module d'enregistrement #2
mais la aussi j'ai un message d'erreur :
Avertissement : Conversion de tableau en chaîne dans /home/u320889525/domains/*********/public_html/importcsv.php à la ligne 41
Erreur fatale : Uncaught PDOException : SQLSTATE[21S01] : La liste des valeurs d'insertion ne correspond pas à la liste des colonnes : 1136 Colonne le nombre ne correspond pas au nombre de valeurs à la ligne 1 dans /home/u320889525/domains/********/public_html/importcsv.php:41 Trace de pile : #0 /home/u320889525/domains/********/public_html /importcsv.php(41): PDOStatement->execute() #1 {main} jeté dans /home/u320889525/domains/*********/public_html/importcsv.php à la ligne 41
j'ai verifié db_connect la connection est bonne
version php 8.0