IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Passage en PDO modification d'une autre requête [MySQL]


Sujet :

PHP & Base de données

  1. #1
    Membre régulier
    Homme Profil pro
    Educateur spécialisé
    Inscrit en
    Janvier 2012
    Messages
    208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Educateur spécialisé
    Secteur : Santé

    Informations forums :
    Inscription : Janvier 2012
    Messages : 208
    Points : 118
    Points
    118
    Par défaut Passage en PDO modification d'une autre requête
    Bonjour à tous et à toutes.
    Je suis encore en train de modifier mes requêtes pour passer en PDO et j'en ai une qui fonctionne pas mais je ne sais pas pourquoi.
    J'ai besoin d'un coup de main.
    Voici la requête en question.
    D’abord l'ancienne qui fonctionne puis la nouvelle qui fonctionne pas.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     
    <?php
    $titre_edit = mysql_real_escape_string(trim($_POST['titre']));
    $description = mysql_real_escape_string(trim($_POST['contenu']));
     
    //On entre le topic dans la base de donnée en laissant
    //le champ topic_last_post à 0
    mysql_query("INSERT INTO forum_topic (forum_id, topic_titre, topic_createur, topic_vu, topic_time, topic_genre, topic_last_post, topic_post, validation_annonce) 
    VALUES('".$forum."', '".$titre_edit."', '".intval($_SESSION['id_utilisateur'])."', '1', '".$temps."','".$statut_annonce."', '0', '0', '0'  )") 
    or die ("Un problème est survenu lors de l'envoi du message");
    //Je récuper le dernier id 
    $nouveautopic = mysql_insert_id();
     
    $stmt = $bdd->prepare('INSERT INTO forum_topic(forum_id, topic_titre, topic_createur, topic_vu, topic_time, topic_genre, topic_last_post, topic_post, validation_annonce) 
    VALUES(:id,:titre,:createur,:vu,:time,:genre,:last_post,:post,:valid_annonce)');
    $stmt->bindValue('id', $forum, PDO::PARAM_INT);
    $stmt->bindValue('titre', $titre_edit, PDO::PARAM_STR);
    $stmt->bindValue('createur', $_SESSION['id_utilisateur'], PDO::PARAM_INT);
    $stmt->bindValue('vu', 1, PDO::PARAM_INT);
    $stmt->bindValue('time', $temps, PDO::PARAM_INT);
    $stmt->bindValue('genre', $statut_annonce, PDO::PARAM_STR);
    $stmt->bindValue('last_post', 0, PDO::PARAM_INT);
    $stmt->bindValue('post', 0, PDO::PARAM_INT);
    $stmt->bindValue('valid_annonce', 0, PDO::PARAM_INT);
    $stmt->execute();
    //Je récuper le dernier id 
    $nouveautopic = $bdd->lastInsertId;
    ?>
    Merci de votre aide.

  2. #2
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Bonjour, c'est quoi le message d'erreur ?

    Sinon, dans l'ancienne tu passe que des string, dans la nouvelle tu fais la différence entre string et entier ?

    Et peut-être ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    $stmt = $bdd->prepare('INSERT INTO forum_topic(forum_id, topic_titre, topic_createur, topic_vu, topic_time, topic_genre, topic_last_post, topic_post, validation_annonce) 
    VALUES(:id,:titre,:createur,:vu,:time,:genre,:last_post,:post,:valid_annonce)');
    $stmt->bindValue(':id', $forum, PDO::PARAM_INT);
    $stmt->bindValue(':titre', $titre_edit, PDO::PARAM_STR);
    $stmt->bindValue(':createur', $_SESSION['id_utilisateur'], PDO::PARAM_INT);
    $stmt->bindValue(':vu', 1, PDO::PARAM_INT);
    $stmt->bindValue(':time', $temps, PDO::PARAM_INT);
    $stmt->bindValue(':genre', $statut_annonce, PDO::PARAM_STR);
    $stmt->bindValue(':last_post', 0, PDO::PARAM_INT);
    $stmt->bindValue(':post', 0, PDO::PARAM_INT);
    $stmt->bindValue(':valid_annonce', 0, PDO::PARAM_INT);
    $stmt->execute();

  3. #3
    Membre régulier
    Homme Profil pro
    Educateur spécialisé
    Inscrit en
    Janvier 2012
    Messages
    208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Educateur spécialisé
    Secteur : Santé

    Informations forums :
    Inscription : Janvier 2012
    Messages : 208
    Points : 118
    Points
    118
    Par défaut
    Merci de ton aide, j'avais déjà essayé ça, je l'ai fait à nouveau au cas ou mais ça fonctionne toujours pas.
    Comment je fait pour voir les messages d'erreur ?
    J'ai ça pour me connecter à ma bdd et normalement ils devraient s'afficher ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <?php
    try
    	{
    		$bdd = new PDO('mysql:host=***;dbname=***;charset=utf8', '***', '***', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
    	}
    catch (Exception $e)
    	{
    		trigger_error($e->getMessage(), E_USER_ERROR);
    	}
    ?>
    Mais non.

  4. #4
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Cela dit juste que tu es connecté, donc si tu as une erreur à la connexion, ok tu auras un retour.

    Tu dois faire de même pour ta requête.

    Regarde aussi dans ton php.ini si les erreurs sont à On, ainsi que la possibilité de visualiser sur l'écran.

    N'oublies pas relancer Apache.

  5. #5
    Membre régulier
    Homme Profil pro
    Educateur spécialisé
    Inscrit en
    Janvier 2012
    Messages
    208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Educateur spécialisé
    Secteur : Santé

    Informations forums :
    Inscription : Janvier 2012
    Messages : 208
    Points : 118
    Points
    118
    Par défaut
    Merci pour votre aide, voici la requête qui fonctionne et comment j'affiche les erreurs au cas ou quelqu'un cherche.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    <?php
    $stmt = $bdd->prepare('INSERT INTO forum_topic(forum_id, topic_titre, topic_createur, topic_vu, topic_time, topic_genre, topic_last_post, topic_post, validation_annonce) 
    								VALUES(:id,:titre,:createur,:vu,:time,:genre,:last_post,:post,:valid_annonce)');
    								$stmt->bindValue('id', $forum, PDO::PARAM_INT);
    								$stmt->bindValue('titre', $titre_edit, PDO::PARAM_STR);
    								$stmt->bindValue('createur', $_SESSION['id_utilisateur'], PDO::PARAM_INT);
    								$stmt->bindValue('vu', '5', PDO::PARAM_INT);
    								$stmt->bindValue('time', $temps, PDO::PARAM_INT);
    								$stmt->bindValue('genre', $statut_annonce, PDO::PARAM_STR);
    								$stmt->bindValue('last_post', '0', PDO::PARAM_INT);
    								$stmt->bindValue('post', '0', PDO::PARAM_INT);
    								$stmt->bindValue('valid_annonce', '0', PDO::PARAM_INT);
    								try {
    									$stmt->execute();
    									$nouveautopic = $bdd->lastInsertId;
    								}
    								catch (PDOException $e) {
    								   echo 'Error : ' . $e->getMessage();
    								   die();
    								}
    ?>
    Je fait ça $nouveautopic = $bdd->lastInsertId; pour récupérer le numéro de la dernière entré dans la table mais ça fonctionne pas.
    Merci de votre aide.

  6. #6
    Membre régulier
    Homme Profil pro
    Educateur spécialisé
    Inscrit en
    Janvier 2012
    Messages
    208
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Educateur spécialisé
    Secteur : Santé

    Informations forums :
    Inscription : Janvier 2012
    Messages : 208
    Points : 118
    Points
    118
    Par défaut
    Voila, tous fonctionne.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    $stmt = $bdd->prepare('INSERT INTO forum_topic(forum_id, topic_titre, topic_createur, topic_vu, topic_time, topic_genre, topic_last_post, topic_post, validation_annonce) 
    								VALUES(:id,:titre,:createur,:vu,:time,:genre,:last_post,:post,:valid_annonce)');
    								$stmt->bindValue('id', $forum, PDO::PARAM_INT);
    								$stmt->bindValue('titre', $titre_edit, PDO::PARAM_STR);
    								$stmt->bindValue('createur', $_SESSION['id_utilisateur'], PDO::PARAM_INT);
    								$stmt->bindValue('vu', '5', PDO::PARAM_INT);
    								$stmt->bindValue('time', $temps, PDO::PARAM_INT);
    								$stmt->bindValue('genre', $statut_annonce, PDO::PARAM_STR);
    								$stmt->bindValue('last_post', '0', PDO::PARAM_INT);
    								$stmt->bindValue('post', '0', PDO::PARAM_INT);
    								$stmt->bindValue('valid_annonce', '0', PDO::PARAM_INT);
    								try {
    									$stmt->execute();
    									$nouveautopic = $bdd->lastInsertId();
    								}
    								catch (PDOException $e) {
    								   echo 'Error : ' . $e->getMessage();
    								   die();
    								}
    Merci.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Passage en PDO modification d'une requête
    Par Sergio_zero dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 23/07/2015, 09h58
  2. Insert à partir d'une autre requête
    Par DjyDjy dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 06/03/2007, 20h48
  3. [debutant]passage d'un tableau sur une autre page
    Par sissi25 dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 05/02/2007, 09h43
  4. requête qui boucle sur une autre requête
    Par super_dz dans le forum Langage SQL
    Réponses: 2
    Dernier message: 02/09/2006, 20h02
  5. Réponses: 3
    Dernier message: 14/02/2006, 21h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo