[RESOLU] UPDATE syntaxe

JVL
Eléphant du PHP | 76 Messages

11 oct. 2013, 16:27

Bonjour à tous,

J'ai cherche actuellement à faire fonctionner une requète de mise a jour. Mon problème vient du fait que les nouvels valeur ne sont pas prise en compte. C'est à dire que sur un update tout les champs maj deviennent vide. Donc j'effectue d'abord une requète de selection pour récupéré les valeurs à modifier, et les afficher. Et vient le problème lors de la requete UPDATE, les valeurs dans la bdd, sont vide (vraiment, les champs sont vides).


if(isset($_POST['modifier'])){ // fonction de modification


						$id = $_POST['id_suivi'];
							$select_modif = $connection->query('SELECT `Projet_suivi`, `Name_suivi`, `Date_suivi`, `Categorie_suivi`, `Duree_suivi`, `Commentaire` FROM `glpi_suivi_travail` WHERE `ID_suivi`= \''.$id.'\' ');

								$row = $select_modif->fetch(PDO::FETCH_OBJ);

										echo '<form action="action.php" method="POST"> <table class=tab_cadrehov> <tr> <th>Date du jour à modifier </th> <th> Projet sélectionné </th> <th> Catégorie </th> <th>temps travaillé en seconde </th> <th> Commentaire </th> </tr>
										<tr> <td> '.$row->Date_suivi.'<input type="text" name="update_date"/> </td>
										<td> '.$row->Projet_suivi.'<input type="text" name="update_projet"/> </td>
										<td> '.$row->Categorie_suivi.'<input type="text" name="update_categorie"  /> </td>
										<td> '.$row->Duree_suivi.'<input type="text" name="update_duree"  /> </td>
										<td> '.$row->Commentaire.' <input type="text" name="update_comm"/> </td>
										</tr> <tr> <td> Veuillez utiliser le format année/mois/jour </td> </tr> </table> </br> <center><input type="submit" value="Enregistrer la modification"> </center> </form>';
															
					$update = $connection->prepare('UPDATE `glpi_suivi_travail` SET `Projet_suivi`=:projet,`Date_suivi`=:date,`Categorie_suivi`=:cate,`Duree_suivi`=:duree,`Commentaire`=:comm WHERE `ID_suivi`=:id');
					$pj = $_POST['update_projet'];
					$da = $_POST['update_date'];
					$cat = $_POST['update_categorie'];
					$dur = $_POST['update_duree'];
					$com = $_POST['update_comm'];
					$update_travail = $update->execute(array(
								 'projet' => "$pj",
								 'date' => "$da",		
								 'cate' => "$cat",
								 'duree' => "$dur",
								 'comm' => "$com",		
								 'id' => "$id"		
										));
					Header('Location:teststagiaire.php');
					Html::footer(); 
							  }	
Merci beaucoup d'avance !

ViPHP
AB
ViPHP | 5818 Messages

11 oct. 2013, 17:53

Faudrait revoir la syntaxe de pdo http://www.php.net/manual/fr/pdostatement.execute.php. Si tu liste tes variables pour les lier une à une alors autant utiliser bindParam puis execute(). Si tu passe un array dans execute tu peux faire simplement :
$update->execute(array(':projet' => $_POST['update_projet'], ':date' => $_POST['update_date'], :cate => $_POST['update_categorie'], ...));
Ensuite pour voir tes erreurs utilises des blocs try/catch et configure pdo pour qu'il affiche les erreurs, pour vérifier le contenu de tes variables $_POST, faits les afficher pendant la phase debug.

JVL
Eléphant du PHP | 76 Messages

14 oct. 2013, 10:38

Ok merci AB,

un petit dernier coups de main pour faire fonctionner les messages d'erreur ? :priere:

ty so much.
try{								
							$update = $connection->prepare('UPDATE `glpi_suivi_travail` SET `Projet_suivi`=:projet,`Date_suivi`=:date,`Categorie_suivi`=:cate,`Duree_suivi`=:duree,`Commentaire`=:comm WHERE `ID_suivi`=:id');

							$update->bindParam(':projet',$_POST['update_projet'],PDO::PARAM_STR);
							$update->bindParam(':date',$_POST['update_date'],PDO::PARAM_DATE);
							$update->bindParam(':cate',$_POST['update_categorie'],PDO::PARAM_STR);
							$update->bindParam(':duree',$_POST['update_duree'],PDO::PARAM_INT);
							$update->bindParam(':comm',$_POST['update_comm'],PDO::PARAM_STR);
							$update->bindParam(':id',$id ,PDO::PARAM_INT);
							
					}
				catch (PDOException $e) {
					echo 'Error : ' . $e->getMessage();
					die();
}

JVL
Eléphant du PHP | 76 Messages

14 oct. 2013, 14:42

Yop, voila ou j'en suis, pas de message d'erreur, mais pas de modification dans la bdd
			if(isset($_POST['modifier'])){ // fonction de modification

								define('GLPI_ROOT', '..');

								include (GLPI_ROOT . "/inc/includes.php");

								Session::checkRight("reports", "r");

								Html::header($LANG['Menu'][6],$_SERVER['PHP_SELF'],"utils","report"); // appel de l'entête de GLPI

								echo 'FONCTION DE MODIFICATION EN CONSTRUCTION</br>';

									$id = $_POST['id_suivi'];
										$select_modif = $connection->query('SELECT `Projet_suivi`, `Name_suivi`, `Date_suivi`, `Categorie_suivi`, `Duree_suivi`, `Commentaire` FROM `glpi_suivi_travail` WHERE `ID_suivi`= \''.$id.'\' ');

											$row = $select_modif->fetch(PDO::FETCH_OBJ);

													echo '<form action="action.php" method="POST"> <table class=tab_cadrehov> <tr> <th>Date du jour à modifier </th> <th> Projet sélectionné </th> <th> Catégorie </th> <th>temps travaillé en seconde </th> <th> Commentaire </th> </tr>
													<tr> <td> '.$row->Date_suivi.'<input type="text" name="update_date"/> </td>
													<td> '.$row->Projet_suivi.'<input type="text" name="update_projet"/> </td>
													<td> '.$row->Categorie_suivi.'<input type="text" name="update_categorie"  /> </td>
													<td> '.$row->Duree_suivi.'<input type="text" name="update_duree"  /> </td>
													<td> '.$row->Commentaire.' <input type="text" name="update_comm"/> </td>
													</tr> <tr> <td> Veuillez utiliser le format année-mois-jour </td> </tr> </table> </br> <center><input type="submit" name="modif" value="Enregistrer la modification"> </center> </form>';
													Html::footer();
										}				
							if(isset($_POST['modif'])){
								try {						
										$update = $connection->prepare('UPDATE `glpi_suivi_travail` SET `Projet_suivi`=:projet,`Date_suivi`=:date,`Categorie_suivi`=:cate,`Duree_suivi`=:duree,`Commentaire`=:comm WHERE `ID_suivi`=:id');

										$update->bindParam(':projet',$_POST['update_projet'], PDO::PARAM_STR);
										$update->bindParam(':date',$_POST['update_date'], PDO::PARAM_STR);
										$update->bindParam(':cate',$_POST['update_categorie'], PDO::PARAM_STR);
										$update->bindParam(':duree',$_POST['update_duree'], PDO::PARAM_STR);
										$update->bindParam(':comm',$_POST['update_comm'], PDO::PARAM_STR);
										$update->bindParam(':id',$id, PDO::PARAM_STR);
											
										$update->execute();
										header('Location:teststagiaire.php');
										
									}
									catch (PDOException $e) {
																echo 'Error : ' . $e->getMessage();
																die();
															}

 
													} 

Code : Tout sélectionner

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\n.php on line 52
Modifié en dernier par JVL le 14 oct. 2013, 15:10, modifié 1 fois.

Eléphant du PHP | 116 Messages

14 oct. 2013, 15:10

Hellow,

Commente :
//  header('Location:teststagiaire.php'); 
En évitant la redirection, tu auras peut être une erreur.
@+,
Computarelier

JVL
Eléphant du PHP | 76 Messages

14 oct. 2013, 15:16

Yep, bon j'ai un peut modifier le code, toujours même soucis, pas d'erreur, mais pas de modification dans la bdd non plus
	if(isset($_POST['modifier'])){ // fonction de modification

								define('GLPI_ROOT', '..');

								include (GLPI_ROOT . "/inc/includes.php");

								Session::checkRight("reports", "r");

								Html::header($LANG['Menu'][6],$_SERVER['PHP_SELF'],"utils","report"); // appel de l'entête de GLPI

								echo 'FONCTION DE MODIFICATION EN CONSTRUCTION</br>';

									$id = $_POST['id_suivi'];
										$select_modif = $connection->query('SELECT `Projet_suivi`, `Name_suivi`, `Date_suivi`, `Categorie_suivi`, `Duree_suivi`, `Commentaire` FROM `glpi_suivi_travail` WHERE `ID_suivi`= \''.$id.'\' ');

											$row = $select_modif->fetch(PDO::FETCH_OBJ);

													echo '<form action="action.php" method="POST"> <table class=tab_cadrehov> <tr> <th>Date du jour à modifier </th> <th> Projet sélectionné </th> <th> Catégorie </th> <th>temps travaillé en seconde </th> <th> Commentaire </th> </tr>
													<tr> <td> '.$row->Date_suivi.'<input type="text" name="update_date"/> </td>
													<td> '.$row->Projet_suivi.'<input type="text" name="update_projet"/> </td>
													<td> '.$row->Categorie_suivi.'<input type="text" name="update_categorie"  /> </td>
													<td> '.$row->Duree_suivi.'<input type="text" name="update_duree"  /> </td>
													<td> '.$row->Commentaire.' <input type="text" name="update_comm"/> </td>
													</tr> <tr> <td> Veuillez utiliser le format année-mois-jour </td> </tr> </table> </br> <center><input type="submit" name="modif" value="Enregistrer la modification"> </center> </form>';
													Html::footer();
										}				
			if(isset($_POST['modif'])){
			
					try {						
							$update = $connection->prepare('UPDATE `glpi_suivi_travail` SET `Projet_suivi`=:upprojet,`Date_suivi`=:update,`Categorie_suivi`=:upcate,`Duree_suivi`=:upduree,`Commentaire`=:upcomm WHERE `ID_suivi`=:upid');

							$update->bindParam(':upprojet',$_POST['update_projet'], PDO::PARAM_STR);
							$update->bindParam(':update',$_POST['update_date'], PDO::PARAM_STR);
							$update->bindParam(':upcate',$_POST['update_categorie'], PDO::PARAM_STR);
							$update->bindParam(':upduree',$_POST['update_duree'], PDO::PARAM_STR);
							$update->bindParam(':upcomm',$_POST['update_comm'], PDO::PARAM_STR);
							$update->bindParam(':upid',$id, PDO::PARAM_STR);
											
							$update->execute();
										
						}
					catch (PDOException $e) {
												echo 'Error : ' . $e->getMessage();
												die();
											}

 
									} 

JVL
Eléphant du PHP | 76 Messages

14 oct. 2013, 16:03

Pardon de multi poster.

Mais je commence à voir le bout du tunnel ! donc c'est ma variable $id qui ne passe pas de la requete de selection vers la requete d'update.

ViPHP
AB
ViPHP | 5818 Messages

17 oct. 2013, 02:37

Alors c'est résolu ?

C'est bizarre que cela ne retournait pas d'erreur, peut-être tu n'avais pas configuré pdo pour attraper les exceptions.
Dans mon premier message je te disais de configurer pdo pour qu'il affiche les message d'erreur et d'utiliser des blocs try/catch. Pour ce dernier point ton code à l'air ok, pour configurer pdo si tu ne l'avais pas fais c'est ici http://php.net/manual/fr/pdo.error-handling.php.

JVL
Eléphant du PHP | 76 Messages

17 oct. 2013, 09:25

Alors mettant rendu compte que c'était l'id j'ai changer la variable, et du coup ca fonctionne nikel !