probleme insertion bdd

yvesman13
Invité n'ayant pas de compte PHPfrance

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

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 9782 Messages

26 sept. 2021, 01:40

mysqli_query() : L'argument #1 ($mysql) doit être de type mysqli
Fais un var_dump() du 1er paramètre que tu passes à cette fonction, cela devrait te permettre d'avancer dans ton debugage.
Quand tout le reste a échoué, lisez le mode d'emploi...

yvesman131
Invité n'ayant pas de compte PHPfrance

28 sept. 2021, 15:27

voila ou j'en suis

j'ai essaye de tenir compte (au mieux de mes capacites) des differentes remarques glanées dans les différents forums et ce que j'en retire

- à l'activation du module d'enregistrement 1 j'ai ce retour :

Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, PDO given in /home/u320889525/domains/............../public_html/.........php:25 Stack trace: #0 /home/u320889525/domains/........../public_html/............php(25): mysqli_query() #1 {main} thrown in /home/u320889525/domains/................./public_html/...........php on line 25

qui correspond a la ligne :

" mysqli_query($conn,'LOAD DATA LOCAL INFILE \'amazon-fours.csv\' INTO TABLE produits ";

si je desactive cette ligne j'ai ce retour :


Parse error: syntax error, unexpected identifier "TERMINATED" in /home/u320889525/domains/.........../public_html/............php on line 27

- à l'activation du module d'enregistrement 2 j'ai ce retour :

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '':name' = ?, ':description' = ?, ':price' = ?' at line 3 in /home/u320889525/domains/.............../public_html/..................php:41 Stack trace: #0 /home/u320889525/domains/................fr/public_html/.................php(41): PDO->exec() #1 {main} thrown in /home/u320889525/domains/............../public_html/............php on line 41

qui correspond à la ligne :

$conn->exec($sql);


- à l'activation du module d'enregistrement 3 j'ai ce retour :

Fatal error: Uncaught Error: Object of class PDO could not be converted to string in /home/u320889525/domains/............../public_html/...........php:49 Stack trace: #0 /home/u320889525/domains/............/public_html/...............php(49): PDOStatement->execute() #1 {main} thrown in /home/u320889525/domains/................/public_html/.............php on line 49

qui correspond à la ligne

$req->execute([$conn]);

et j'ai du desactiver $price sinon j'ai cette erreur :

Warning: Uninitialized string offset 2 in /home/u320889525/domains/........../public_html/................php on line 47

répétée 10 fois

Code : Tout sélectionner

<?php error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); $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 include("db_connect.php"); while (($data = fgetcsv($h, 0, ";")) !== FALSE) { // Each individual array is being pushed into the nested array $the_big_array[] = $data; foreach ($the_big_array as $table) { foreach ($table as $contenu ) { /* // 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 /* $sql = <<<SQL INSERT INTO produits SET ':name' = $contenu[0], ':description' = $contenu[1], ':price' = $contenu[2] SQL ; $conn->exec($sql); */ // module d'enregistrement #3 $name = $contenu[0]; $description = $contenu[1]; $price = $contenu[2]; $req = $conn ->prepare('INSERT INTO produits (name,description,price) VALUES ($name,$description)'); $req->execute([$conn]); } //fin foreach #2 } // fin foreach #1 } //fin while} // fin if $sh/* echo '<br/>name : <br/> '; echo $name ; echo '<br/>description : <br/>'; echo $description ; echo '<br/>price : <br/>'; echo $price ; echo '<br/>req : <br/>'; print_r($req); */ /* // afficher le code echo "<pre>"; var_dump($the_big_array); echo "</pre>"; */?>[code]
contenu db_connect.php :
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

try
{
$host = 'mysql:dbname=................;host=127.0.0.1';
$dsn = '............................."
$user = '............................';
$password = '...................';
$conn = new PDO($host, $user, $password);
// Activation des erreurs PDO
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// mode de fetch par défaut : FETCH_ASSOC / FETCH_OBJ / FETCH_BOTH
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
die('Erreur : ' . $e->getMessage());
}
?>
[/code]