Bonjour à tous,
j'ai une requête update qui ne se fait pas avec mon objet et en pdo.
Il considère mon id comme null alors que je pense l'avoir défini au niveau de mon formulaire
ce qui pose problème est ma classe dont j'initialise mes variables. Si je détermine ma variable id avec un chiffre: cela foncctionne, si je le laisse ainsi cela fonctionne pas.
l'insert fonctionne, la récupération des informations bd, et la récupération d'un article cliquer, la deconnexion et l'authentification aussi
Il faut regarder au niveau des declaration des variable et du constructor qui me semble juste. Eventuellement au niveau de la fonction update en bas...
ma classe

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
session_start();
class Emploi{
  // variables //
 
  /**
  * @var int The article ID from the database
  */
  public $id = null;
 
  /**
  * @var int When the article is to be / was first published
  */
  public $dateEmploi = null;
 
  /**
  * @var string Full title of the article
  */
  public $contrat =  null;
 
  /**
  * @var string A short summary of the article
  */
  public $description= null;
 
  /**
  * @var string The HTML content of the article
  */
  public $salaire =  null;
/**
  * @var string The HTML content of the article
  */
  public $titre =  null;
 
 /**
  * @var string The HTML content of the article
  */
public $identifiant = null;
 
 /**
  * @var string The HTML content of the article
  */
public $mdp = null;
 
 /**
  * @var string The HTML content of the article
  */
 
 public $experience = null;
 
 /**
  * @var string The HTML content of the article
  */
public $emploi = null;
 
 /**
  * @var string The HTML content of the article
  */
 public $postal = null;
 
 /**
  * @var string The HTML content of the article
  */
 public $ville=  null;
 
 /**
  * @var string The HTML content of the article
  */
 public $pays =  null;
 
 /**
  * @var string The HTML content of the article
  */
 
  // Contructeur //
  public function __construct( $data=array() ) {
      if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];
    if ( isset( $data['dateEmploi'] ) ) $this->dateEmploi = (int) $data['dateEmploi'];
    if ( isset( $data['contrat'] ) ) $this->contrat  = $data['contrat'] ;
    if ( isset( $data['titre'] ) ) $this->titre = $data['titre'] ;
    if ( isset( $data['description'] ) ) $this->description = $data['description'];
        if ( isset( $data['salaire'] ) ) $this->salaire  =  $data['salaire'] ;
            if ( isset( $data['identifiant'] ) ) $this->identifiant = $data['identifiant'] ;
                if ( isset( $data['mdp'] ) ) $this->mdp = $data['mdp'] ;
                  if ( isset( $data['experience'] ) ) $this->experience = $data['experience'] ;
                    if ( isset( $data['pays'] ) ) $this->pays = $data['pays'] ;
                      if ( isset( $data['postal'] ) ) $this->postal = $data['postal'] ;
                        if ( isset( $data['ville'] ) ) $this->ville = $data['ville'] ;
 
  }
 
  // fonction pour recuperer une id d un article et le vsualiser //
 
  public static function getById( $id ) {
    $conn = new PDO( DSN, USER, PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8') );
    $sql = "SELECT *, UNIX_TIMESTAMP(dateEmploi) AS dateEmploi FROM emplois WHERE id = :id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Emploi( $row );
  }
 
  // fonction pour recuperer les info bd //
 
  public static function getList() {
$db = new PDO(DSN, USER, PASSWORD);
 
$sql=$db->prepare("SELECT * FROM emplois"); // on prépare notre requête
$sql->execute();
while($lignes=$sql->fetch(PDO::FETCH_OBJ))
 
{  
$lignes->dateEmploi= date("d/m/Y",strtotime($lignes->dateEmploi));
$tableau[]=$lignes;
} 
return $tableau; 
 
}
 
  // fonction pour se connecter en admin bd //
 
public static function connexion($identifiant,$mdp){
  $db= new pdo(DSN,USER,PASSWORD);
    $sql = "SELECT * FROM utilisateurs WHERE identifiant = :identifiant AND mdp=:mdp";
    $st = $db->prepare( $sql );
    $st->bindValue( ":identifiant", $identifiant, PDO::PARAM_INT );
      $st->bindValue( ":mdp", $mdp, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
 
    if (isset($row['identifiant'])&&!empty($row['identifiant'])&& isset($row['mdp'])&&!empty($row['mdp'])&&$row['type']=='admin')
{
    $_SESSION['identifiant']=$row['identifiant'];  
   $_SESSION['mdp']=$row['mdp'];
     header('location:/oxylis/Core/utilisateurs/admin.php'); 
 
}
else {
  echo "Erreur de connexion" ; }
}
 
  // fonction se deconnecter //
public static function deconnexion (){
  session_destroy();
  header('location:../index.php'); }
 
 
  // fonction pour inserer les infos bd //
 
public function insert(){
  try
{
$db= new pdo(DSN,USER,PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$sql = "INSERT INTO emplois(contrat,dateEmploi,description,experience,pays,postal,salaire,titre,ville) VALUES(:contrat,:dateEmploi,:description,:experience,:pays,:postal,:salaire,:titre,:ville)";
$in=$db->prepare($sql);
  $in->bindValue( ":contrat", $_POST['contrat'], PDO::PARAM_STR );
 $in->bindValue( ":dateEmploi", $_POST['dateEmploi'], PDO::PARAM_INT );
   $in->bindValue( ":description", $_POST['description'], PDO::PARAM_STR );
     $in->bindValue( ":experience", $_POST['experience'], PDO::PARAM_STR );
       $in->bindValue( ":pays", $_POST['pays'], PDO::PARAM_STR);
         $in->bindValue( ":postal", $_POST['postal'], PDO::PARAM_INT );
           $in->bindValue( ":salaire", $_POST['salaire'], PDO::PARAM_STR );
    $in->bindValue( ":titre", $_POST['titre'], PDO::PARAM_STR );
      $in->bindValue( ":ville", $_POST['ville'], PDO::PARAM_STR );
 
    $in->execute();
 
 
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}
 
}
 
  // fonction pour updater un article //
 
public function update(){
 
  try
{
  if ( is_null( $this->id ) ) trigger_error ( "Emploi::update(): l'id est null", E_USER_ERROR );
$db= new pdo(DSN,USER,PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$sql = " UPDATE emplois SET contrat= :contrat, dateEmploi = :dateEmploi, description = :description, experience = :experience, pays = :pays,postal=:postal,salaire=:salaire,titre=:titre,ville=:ville WHERE id =:id";
$in=$db->prepare($sql);
  $in->bindValue( ":contrat", $this->contrat, PDO::PARAM_STR );
 $in->bindValue( ":dateEmploi", $this->dateEmploi, PDO::PARAM_INT );
   $in->bindValue( ":description", $this->description, PDO::PARAM_STR );
     $in->bindValue( ":experience", $this->experience, PDO::PARAM_STR );
       $in->bindValue( ":pays", $this->pays, PDO::PARAM_STR);
         $in->bindValue( ":postal",$this->postal, PDO::PARAM_INT );
           $in->bindValue( ":salaire",$this->salaire, PDO::PARAM_STR );
    $in->bindValue( ":titre", $this->titre, PDO::PARAM_STR );
      $in->bindValue( ":ville", $this->ville, PDO::PARAM_STR );
   $in->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $in->execute();
 
 
} 
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}
}
}
?>

et mon formulaire :
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
52
53
54
55
56
<html>
<head>
  <title>Administration Contrôle</title>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
  <link rel="stylesheet" type="text/css" href="../templates/style/styleBd.css">
</head>
<body>
<div class="container">
<?php
require("../config.php");
 isset( $_GET['id'] ) ? $_GET['id'] : "";
  $results = array();
  $results['article'] = Emploi::getById( (int) ($_GET['id'] )) ;
 
?>  
<form action="" method="POST" class="formInsertion">
  <input type="hidden" name="id" value="<?php if(isset($results['article'] ->id)) { echo $results['article'] ->id; }?>" />"
 
<p><label>Contrat</label>
    <input type="text" name="contrat" value="<?php if (isset( $results['article']->contrat )) echo htmlspecialchars($results['article'] ->contrat)?>" placeholder="contrat" required/></p>
<p><label>Date Emploi</label>
  <input type="date" name="dateEmploi" value="<?php if (isset( $results['article']->dateEmploi )) echo htmlspecialchars($results['article'] ->dateEmploi) ? date("d/m/Y",strtotime($results['article'] ->dateEmploi)) : "" ?>" placeholder="JJ-MM-AAAA" required/></p>
  <p><label>Description</label>
    <textarea name="description"  placeholder="description" required maxlength="1000" /><?php if (isset( $results['article']->description )) echo htmlspecialchars($results['article']->description)?>   </textarea></p>
    <p><label>Expérience</label>
      <input type="text" name="experience"  value="<?php if (isset( $results['article']->experience )) echo htmlspecialchars($results['article'] ->experience)?>" placeholder="experience" required/></p>
      <p><label>Pays</label>
        <input type="text" name="pays" value="<?php if (isset( $results['article']->pays )) echo htmlspecialchars($results['article']->pays)?>" placeholder="pays" required/></p>
       <p> <label>Code Postal</label>
          <input type="text" name="postal" value="<?php if (isset( $results['article']->postal )) echo htmlspecialchars($results['article']->postal)?>" placeholder="postal" required/></p>
          <p><label>Salaire</label>
            <input type="text" name="salaire" value="<?php if (isset( $results['article']->salaire )) echo htmlspecialchars($results['article']->salaire)?>" placeholder="salaire" required/></p>
            <p><label>Titre</label>
              <input type="text" name="titre" value="<?php if (isset( $results['article']->titre )) echo htmlspecialchars($results['article']->titre)?>" placeholder="titre" required/></p>
             <p> <label>Ville</label>
               <input type="text" name="ville" value="<?php if (isset( $results['article']->ville )) echo htmlspecialchars($results['article']->ville)?>" placeholder="ville" required/></p>
    <p><input type="submit" name="inserer" class="submitEditer" /></p>
 <p ><a href="admin.php" style="color:white">Retour à la liste</a></p>
 <?php
 
if ( isset( $_POST['inserer'] ) ) {
 if(  $results['article'] ){
$updater = new Emploi();
$updater->update();
  echo "update";
 }
 else{
$article = new Emploi();
 
$article->insert();
 
echo "<p style='color:#BCBCBC;margin-left:50% ;font-size:1.2em'>Insertion effectuée</p>";
}
 
} ?>
</form>
Au final il me met l'erreur que j'ai provoqué à la fonction update
Code : Sélectionner tout - Visualiser dans une fenêtre à part
if ( is_null( $this->id ) ) trigger_error ( "Emploi::update(): l'id est null", E_USER_ERROR );
Une derniere chose lorsque je recupere les information de ma bd dans mon formulaire pour le modifier, les informations sont bien présents sauf date Emploi. Pour moi c'est
Code : Sélectionner tout - Visualiser dans une fenêtre à part
? date("d/m/Y",strtotime($results['article'] ->dateEmploi)) : "" ?>"
qui pose probleme. Au niveau du formulaire, le input "date" Mais je cherche depuis pas mal de temps et je ne trouve pas ...
Merci d'avance..