[RESOLU] Insert bdd

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : [RESOLU] Insert bdd

Re: Insert bdd

par yoann38 » 30 sept. 2019, 18:44

Ok, cool c'était simple pour le coup, j'ouvre un autre post pour mon second problème issu du même code

Re: Insert bdd

par Saian » 30 sept. 2019, 18:15

Sauf que la il manque le execute sur la première requête.

Re: Insert bdd

par yoann38 » 30 sept. 2019, 18:05

Just comme ça :
// Insert entreprise     
            $req = $bdd->prepare("INSERT INTO entreprise (`nom_etablissement`) VALUES (:nom_etablissement"); 
            $req->bindParam(':nom_etablissement', $_POST['nom_etablissement'], PDO::PARAM_STR);
            $id_entreprise = $bdd->lastInsertId();

            // Insert Horaires 
            $req2 = $bdd->prepare("INSERT INTO hoaires (`id_entreprise`, jour, opening, ouverture, fermeture) VALUES (:id_entreprise, :jour, :opening, :ouverture, :fermeture");  
            $req2->bindParam(':id_entreprise', $id_entreprise, PDO::PARAM_STR);
            $req2->bindParam(':jour', $jour, PDO::PARAM_STR);
            $req2->bindParam(':opening', $_POST['opening'], PDO::PARAM_STR);
            $req2->bindParam(':ouverture', $_POST['ouverture'], PDO::PARAM_STR);   
            $req2->bindParam(':fermeture', $_POST['fermeture'], PDO::PARAM_STR);   
            $req2->execute();    

Re: Insert bdd

par Saian » 30 sept. 2019, 17:46

Apparemment tu utilises PDO, tu peux donc utiliser la méthode lastInsertId pour obtenir l'id de la dernière ligne insérée.
https://www.php.net/manual/fr/pdo.lastinsertid.php

PS : il faut bien entendu que tu exécutes le premier insert avant de faire le deuxième.

Insert bdd

par yoann38 » 30 sept. 2019, 17:20

Bonjour tout le monde.
Voilà je réalisé un insert en bdd mais je pense m'embrouiller dans mon INSERT car j'ai une petite problématique c'est en fait un double INSERT, sur 2 tables donc entreprise et horaire
Au cours du premier insert j'auto incrémente mon id de ma table entreprise mais j'ai besoin de le récupérer mon ensuite l'intégrer dans mon 2ème insert car effectivement cet id correspond à id_entreprise de mon 2ème insert


J'ai réduit le code PHP pour en facilité sa compréhension
// Insert entreprise     
            $req = $bdd->prepare("INSERT INTO entreprise (`nom_etablissement`) VALUES (:nom_etablissement"); 
            $req->bindParam(':nom_etablissement', $_POST['nom_etablissement'], PDO::PARAM_STR);
         

            // Insert Horaires 
            $req2 = $bdd->prepare("INSERT INTO hoaires (`id_entreprise`, jour, opening, ouverture, fermeture) VALUES (:id_entreprise, :jour, :opening, :ouverture, :fermeture");  
            $req2->bindParam(':id_entreprise', $_POST['id_bar'], PDO::PARAM_STR);
            $req2->bindParam(':jour', $jour, PDO::PARAM_STR);
            $req2->bindParam(':opening', $_POST['opening'], PDO::PARAM_STR);
            $req2->bindParam(':ouverture', $_POST['ouverture'], PDO::PARAM_STR);   
            $req2->bindParam(':fermeture', $_POST['fermeture'], PDO::PARAM_STR);   
            $req2->execute();