Problème avec array_push

Résolu/Fermé
Rune188 Messages postés 67 Date d'inscription lundi 20 février 2017 Statut Membre Dernière intervention 29 janvier 2024 - 13 juin 2019 à 14:18
Rune188 Messages postés 67 Date d'inscription lundi 20 février 2017 Statut Membre Dernière intervention 29 janvier 2024 - 13 juin 2019 à 16:55
Bonjour,

je tente de réaliser un panier en me basant sur http://jmolline.free.fr/tutos/tuto_panier.html mais en adaptant ce panier a mes besoins et a récupéré les données via un formulaire.
Cependant il semble y avoir un problème avec les array_push car j'obtient comme message d’erreur:
"Warning: array_push() expects parameter 1 to be array, null given in D:\wamp64\www\exam_php\panier.php on line 33"
Voici mon code:
<?php
session_start();
include("header.php");
include("navbar.php");
include("connectdb.php");
include_once("fonctions-panier.php");

//récuperation id de l'utilisateur
$mail=$_SESSION['email'];
$req2 =$conn->prepare("SELECT id FROM utilisateurs WHERE email = :email");//tentative de recuperer l'id de l'utilisateur
$req2->execute(['email' => $mail]);
$idUser= $req2 -> fetch();

 /* Article exemple */ 
$select = array(); 
$select['id'] = $_POST['idArt']; 
$select['qte'] = $_POST['quantity']; 
$select['idUser'] = $idUser[0]; 
$select['prix'] = $_POST['prix'];

/* On vérifie l'existence du panier, sinon, on le crée */ 
if(!isset($_SESSION['panier'])) 
{ 
    /* Initialisation du panier */ 
    $_SESSION['panier'] = array(); 
    /* Subdivision du panier */ 
    $_SESSION['panier']['qte'] = array(); 
    $_SESSION['panier']['idUser'] = array(); 
    $_SESSION['panier']['prix'] = array(); 
} 

/* Ici, on sait que le panier existe, donc on ajoute l'article dedans. */ 
array_push($_SESSION['panier']['id_article'],$select['id']); 
array_push($_SESSION['panier']['qte'],$select['qte']); 
array_push($_SESSION['panier']['idUser'],$select['idUser']); 
array_push($_SESSION['panier']['prix'],$select['prix']); 

/* Affichons maintenant le contenu du panier : */ 
?> 
<pre> 
<?php 
var_dump($_SESSION['panier']); 
?> 
</pre>

1 réponse

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 4 650
13 juin 2019 à 15:53
Bonjour,

Ton erreur vient de la non déclaration de ta variable
$_SESSION['panier']['id_article']
en tant qu'array comme aux lignes 27,28,29
$_SESSION['panier']['id_article'] = array();


Mais la façon dont tu stockes les données n'est pas pas la bonne.....
Je t'invite plutôt à te base sur ce code :
https://codes-sources.commentcamarche.net/source/102874-php-panier-caddi-virtuel-en-session
1
Rune188 Messages postés 67 Date d'inscription lundi 20 février 2017 Statut Membre Dernière intervention 29 janvier 2024
13 juin 2019 à 16:55
ok sa fonctionne (je regarderai quand même ce lien)
Merci de m'avoir répondu :)
0