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

Langage PHP Discussion :

Uncaught exception 'PDOException'


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Inscrit en
    Mai 2010
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 17
    Points : 15
    Points
    15
    Par défaut Uncaught exception 'PDOException'
    Bonjour,

    Je tente désespérément d'insérer des informations en base de données. En vain.

    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
    <?php
    	global $current_user; 
    	$current_user = wp_get_current_user();
    	if(isset($_POST['submit'])){
    		/* Set Personal Information */
    		$arrayPersonalInfo = setRoommatePersonalInformation('userID', $current_user);
    		if(!empty($_POST['birthdate-MM']) AND
    		   !empty($_POST['birthdate-DD']) AND
    		   !empty($_POST['birthdate-YYYY']))	$arrayPersonalInfo = setRoommatePersonalInformation('birthdate', array(htmlspecialchars($_POST['birthdate-MM']), htmlspecialchars($_POST['birthdate-DD']), htmlspecialchars($_POST['birthdate-YYYY'])));
    		if(!empty($_POST['mailingAddress']))	$arrayPersonalInfo = setRoommatePersonalInformation('mailingAddress', htmlspecialchars($_POST['mailingAddress']));
    		if(!empty($_POST['city']))				$arrayPersonalInfo = setRoommatePersonalInformation('city', htmlspecialchars($_POST['city']));
    		if(!empty($_POST['state']))				$arrayPersonalInfo = setRoommatePersonalInformation('state', htmlspecialchars($_POST['state']));
    		if(!empty($_POST['zipCode']))			$arrayPersonalInfo = setRoommatePersonalInformation('zipCode', htmlspecialchars($_POST['zipCode']));
    		if(!empty($_POST['phoneNumber']))		$arrayPersonalInfo = setRoommatePersonalInformation('phoneNumber', htmlspecialchars($_POST['phoneNumber']));
    		if(!empty($_POST['emergencyName']))		$arrayPersonalInfo = setRoommatePersonalInformation('emergencyName', htmlspecialchars($_POST['emergencyName']));
    		if(!empty($_POST['emergencyPhone']))	$arrayPersonalInfo = setRoommatePersonalInformation('emergencyPhone', htmlspecialchars($_POST['emergencyPhone']));
    		if(!empty($_POST['school']))			$arrayPersonalInfo = setRoommatePersonalInformation('school', htmlspecialchars($_POST['school']));
     
            /* Insertion BDD */
    	sqlInsertIntoRoommatePersonalInformation($arrayPersonalInfo, $current_user);
    Et voici mes 2 fonctions :

    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    <?php
    function setRoommatePersonalInformation($field, $value){
    	try{
    		if(is_array($value)) $value = $value[2].'-'.$value[0].'-'.$value[1]; /* Birth Date */
    		$personalInfo[$field][] = $value;
    		return $personalInfo;
    	} catch (Exception $e){die('Error : ' . $e->getMessage());}
    }
     
    function sqlInsertIntoRoommatePersonalInformation($data, $user){
    	$bdd = initDB();
    	$req = $bdd->prepare('INSERT INTO sdsh_roommate_personalinfo(
    							userID,
    							birthdate,
    							mailingAddress,
    							city,
    							state,
    							zipCode,
    							phoneNumber,
    							emergencyName,
    							emergencyPhone,
    							school,
    							passport,
    							proof) VALUES(
    							:userID,
    							:birthdate,
    							:mailingAddress,
    							:city,
    							:state,
    							:zipCode,
    							:phoneNumber,
    							:emergencyName,
    							:emergencyPhone,
    							:school,
    							:passport,
    							:proof)');
    	$req->execute(array(
    		'userID'		 => $user->ID,
    		'birthdate' 	 => $data['birthdate'],
    		'mailingAddress' => $data['mailingAddress'],
    		'city' 			 => $data['city'],
    		'state' 		 => $data['state'],
    		'zipCode' 		 => $data['zipCode'],
    		'phoneNumber' 	 => $data['phoneNumber'],
    		'emergencyName'  => $data['emergencyName'],
    		'emergencyPhone' => $data['emergencyPhone'],
    		'school' 		 => $data['school'],
    		'passport' 		 => $data['passport'],
    		'proof' 		 => $data['proof'],
    	));
    }
    L'exception levée est la suivante :

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'birthdate' cannot be null' in C:\wamp\www\SDSH\wp-content\themes\sdsh\functions.php on line 296
    Je dois merder avec mon array, mais où ? Merci pour votre aide.

    PS: Je travaille avec le CMS WordPress, la variable $current_user contient toutes les informations du membre connecté. Simple information, juste au cas où ;-)

    Bonne journée tout le monde (même si vous êtes de Toulouse..)

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    fait un var_dump($data);

  3. #3
    Membre éprouvé Avatar de Marc3001
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2008
    Messages
    829
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Février 2008
    Messages : 829
    Points : 1 275
    Points
    1 275
    Par défaut
    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
    <?php
    	global $current_user; 
    	$current_user = wp_get_current_user();
    	if(isset($_POST['submit'])){
    		/* Set Personal Information */
    		$arrayPersonalInfo = setRoommatePersonalInformation('userID', $current_user);
    		if(!empty($_POST['birthdate-MM']) AND
    		   !empty($_POST['birthdate-DD']) AND
    		   !empty($_POST['birthdate-YYYY']))	$arrayPersonalInfo = setRoommatePersonalInformation('birthdate', array(htmlspecialchars($_POST['birthdate-MM']), htmlspecialchars($_POST['birthdate-DD']), htmlspecialchars($_POST['birthdate-YYYY'])));
    		if(!empty($_POST['mailingAddress']))	$arrayPersonalInfo = setRoommatePersonalInformation('mailingAddress', htmlspecialchars($_POST['mailingAddress']));
    		if(!empty($_POST['city']))				$arrayPersonalInfo = setRoommatePersonalInformation('city', htmlspecialchars($_POST['city']));
    		if(!empty($_POST['state']))				$arrayPersonalInfo = setRoommatePersonalInformation('state', htmlspecialchars($_POST['state']));
    		if(!empty($_POST['zipCode']))			$arrayPersonalInfo = setRoommatePersonalInformation('zipCode', htmlspecialchars($_POST['zipCode']));
    		if(!empty($_POST['phoneNumber']))		$arrayPersonalInfo = setRoommatePersonalInformation('phoneNumber', htmlspecialchars($_POST['phoneNumber']));
    		if(!empty($_POST['emergencyName']))		$arrayPersonalInfo = setRoommatePersonalInformation('emergencyName', htmlspecialchars($_POST['emergencyName']));
    		if(!empty($_POST['emergencyPhone']))	$arrayPersonalInfo = setRoommatePersonalInformation('emergencyPhone', htmlspecialchars($_POST['emergencyPhone']));
    		if(!empty($_POST['school']))			$arrayPersonalInfo = setRoommatePersonalInformation('school', htmlspecialchars($_POST['school']));
     
            /* Insertion BDD */
    	sqlInsertIntoRoommatePersonalInformation($arrayPersonalInfo, $current_user);
    Pour moi, là à chaque fois que tu avances dans tes conditions, tu écrases ta variable arrayPersonalInfo avec un nouveau tableau ne contenant que la dernière valeur.
    Au final, ton arrayPersonalInfo il ne contient que school si $_POST['school'] n'est pas vide.
    Le logiciel, c'est comme le sexe, c'est meilleur quand c'est libre.

    Linus Torvalds

Discussions similaires

  1. [MySQL] Fatal error: Uncaught exception PDOException
    Par momototo38 dans le forum PHP & Base de données
    Réponses: 5
    Dernier message: 27/08/2015, 19h33
  2. Réponses: 2
    Dernier message: 30/03/2015, 11h18
  3. [MySQL] Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000
    Par okoweb dans le forum PHP & Base de données
    Réponses: 10
    Dernier message: 03/12/2011, 08h32
  4. Fatal error: Uncaught exception 'PDOException'
    Par mina13 dans le forum Zend_Db
    Réponses: 2
    Dernier message: 09/06/2011, 09h24
  5. [MySQL] Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
    Par -Fly- dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 11/04/2011, 11h18

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