[PHP] Mise à jours table pour chaque ligne de l'array en une fois

Petit nouveau ! | 9 Messages

16 mai 2008, 08:11

Bonjour,

Je cherche à mettre à jours une table SQL via un formulaire PHP.

Je connais la commande SQL pour le faire mais je n'arrive pas à mettre en place ce que je veut :

Dans mon tableau il y a des liste déroulantes (1 par ligne) je souhaiterais que la table se mette à jours lorsqu'on change une valeur d'une liste (ou de plusieurs) et qu'on clique sur le bouton enregistrer situé en bas du tableau.

Voici le Code :

Code : Tout sélectionner

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="content-language" content="fr"> <title>Administration</title> <style type="text/css"> body { margin: 0; font-family: Arial; color: #000000; font-size: 10pt; } a:link {text-decoration: none;} a:visited {text-decoration: none;} a:active {text-decoration: none;} a:hover {text-decoration: none;} a {color: #333399; font-weight: bold;} a:hover {color: #FF0000;} table td {background-color: #CCCCCC; margin: 0; border-width: 0; font-family: Arial; color: #000000; font-size: 10pt;} .title {background-color: #33CCCC; margin: 0; border-width: 0; font-family: Arial; color: #000000; font-size: 11pt;} </style> </head> <body> <center><h2>Reste à faire</h2></center> <?php include ('include/connexion.php'); $connexion=connexion(); //WHERE etat = 'En cours' OR etat = 'Verification' OR etat = 'Autorisation' $sql = "SELECT * FROM formulaire ORDER BY `formulaire`.`id` DESC"; $result=mysql_query($sql); echo "<table border=\"1\" width=\"100%\" align=\"center\">"; echo "<tr>"; echo "<td class=\"title\" align=\"center\">&nbsp;N°&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Identifiant&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Nom&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Prénom&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Service&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Téléphone&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Nom du site&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;URL&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Catégorie&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Motivation de la demande&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Date&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Etat&nbsp;</td>"; echo "<td class=\"title\" align=\"center\">&nbsp;Commentaire&nbsp;</td>"; echo "</tr>"; //$tableau = mysql_fetch_array($result); while ($tableau = mysql_fetch_row($result)) { $id=$tableau[0]; $identifiant=$tableau[1]; $ndemandeur=$tableau[2]; $prenom=$tableau[3]; $service=$tableau[4]; $telephone=$tableau[5]; $nsi=$tableau[6]; $url=$tableau[7]; $categorie=$tableau[8]; $mdld=$tableau[9]; $date=$tableau[10]; $etat=$tableau[11]; $com=$tableau[12]; ?> <form method="post" action="administration_test.php"> <?php echo "<tr>"; echo "<td align=\"center\">$id&nbsp;</td>"; echo "<td>$identifiant&nbsp;</td>"; echo "<td>$ndemandeur&nbsp;</td>"; echo "<td>$prenom&nbsp;</td>"; echo "<td>$service&nbsp;</td>"; echo "<td>$telephone&nbsp;</td>"; echo "<td>$nsi&nbsp;</td>"; echo "<td><a href=\"$url\" target=\"_blank\">$url</a>&nbsp;</td>"; echo "<td>$categorie&nbsp;</td>"; echo "<td>$mdld&nbsp;</td>"; echo "<td align=\"center\">$date&nbsp;</td>"; echo "<td align=\"center\"><select size=\"1\" name=\"etat\" value=\"$etat\" tabindex=\"3\"> <option value=\"$etat\">$etat</option> <option value=\"Verification\">Verification</option> <option value=\"Autorisation\">Autorisation</option> <option value=\"Accepter\">Accepter</option> <option value=\"Refuser\">Refuser</option> </select></td>"; echo "<td align=\"center\"><textarea rows='5' cols='15' name='comen'>$com</textarea></td>"; echo "</tr>"; $upd = 'UPDATE formulaire SET etat="'.$etat.'" WHERE id="'.$id.'"'; mysql_query($upd); } echo "</table>"; echo "<br><center><input type=\"submit\" value=\"Enregistrer\"></center>"; ?> </form> <br><br> </body> </html>
La mise à jours ne fonctionne pas pour la table.

J'ai essayé avec une boucle For et une boucle foreach mais rien à faire je n'y arrive pas

Merci par avance

Eléphant du PHP | 175 Messages

16 mai 2008, 10:36

c'est moi ou on a pas le code du fichier "administration_test.php" ?

Petit nouveau ! | 9 Messages

16 mai 2008, 10:36

c'est toi ;)

Car administration_test.php est le fichier pour lequel je vous ai fourni le code

ViPHP
ViPHP | 4039 Messages

16 mai 2008, 10:56

c'est un feux d'artifice de "echos" disdonc.. tu as fait du vbscript ?

Tu peux tranquillement faire ceci:
echo "<table border=\"1\" width=\"100%\" align=\"center\">
    <tr>
    <td class=\"title\" align=\"center\">&nbsp;N°&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Identifiant&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Nom&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Prénom&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Service&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Téléphone&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Nom du site&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;URL&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Catégorie&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Motivation de la demande&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Date&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Etat&nbsp;</td>
    <td class=\"title\" align=\"center\">&nbsp;Commentaire&nbsp;</td>
    </tr>"; 
plutôt que ceci:
echo "<table border=\"1\" width=\"100%\" align=\"center\">";
   echo "<tr>";
   echo "<td class=\"title\" align=\"center\">&nbsp;N°&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Identifiant&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Nom&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Prénom&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Service&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Téléphone&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Nom du site&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;URL&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Catégorie&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Motivation de la demande&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Date&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Etat&nbsp;</td>";
   echo "<td class=\"title\" align=\"center\">&nbsp;Commentaire&nbsp;</td>";
   echo "</tr>"; 
Mais qu'importe. (je suis ici - dernier petit projet)
Berze going social.

Petit nouveau ! | 9 Messages

16 mai 2008, 11:04

Ok merci pour les echo et non je n'ai pas fait de VBScript
<?php
	include ('include/connexion.php');
	$connexion=connexion();
	//WHERE etat = 'En cours' OR etat = 'Verification' OR  etat = 'Autorisation'
	$sql = "SELECT * FROM formulaire ORDER BY `formulaire`.`id` DESC";
	$result=mysql_query($sql);
	
	echo "<table border=\"1\" width=\"100%\" align=\"center\">
	<tr>
	<td class=\"title\" align=\"center\">&nbsp;N°&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Identifiant&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Nom&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Prénom&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Service&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Téléphone&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Nom du site&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;URL&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Catégorie&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Motivation de la demande&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Date&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Etat&nbsp;</td>
	<td class=\"title\" align=\"center\">&nbsp;Commentaire&nbsp;</td>
	</tr>";
	
	//$tableau = mysql_fetch_array($result);
	
	while ($tableau = mysql_fetch_row($result))
		{
			$id=$tableau[0];
			$identifiant=$tableau[1];
			$ndemandeur=$tableau[2];
			$prenom=$tableau[3];
			$service=$tableau[4];
			$telephone=$tableau[5];
			$nsi=$tableau[6];
			$url=$tableau[7];
			$categorie=$tableau[8];
			$mdld=$tableau[9];
			$date=$tableau[10];
			$etat=$tableau[11];
			$com=$tableau[12];
?>


<form method="post" action="administration_test.php">
<?php
	

			echo "<tr>
		 	<td align=\"center\">$id&nbsp;</td>
		 	<td>$identifiant&nbsp;</td>
		 	<td>$ndemandeur&nbsp;</td>
		 	<td>$prenom&nbsp;</td>
		 	<td>$service&nbsp;</td>
		 	<td>$telephone&nbsp;</td>
		 	<td>$nsi&nbsp;</td>
		 	<td><a href=\"$url\" target=\"_blank\">$url</a>&nbsp;</td>
		 	<td>$categorie&nbsp;</td>
		 	<td>$mdld&nbsp;</td>
		 	<td align=\"center\">$date&nbsp;</td>
			<td align=\"center\"><select size=\"1\" name=\"etat\" value=\"$etat\" tabindex=\"3\">
		   		<option value=\"$etat\">$etat</option>
				<option value=\"Verification\">Verification</option>
		       	<option value=\"Autorisation\">Autorisation</option>
				<option value=\"Accepter\">Accepter</option>
				<option value=\"Refuser\">Refuser</option>
			</select></td>
			<td align=\"center\"><textarea rows='5' cols='15' name='comen'>$com</textarea></td>
			</tr>";
			
			$upd = 'UPDATE formulaire SET etat="'.$etat.'" WHERE id="'.$id.'"';
			mysql_query($upd);
			
		}
	echo "</table>
	<br><center><input type=\"submit\" value=\"Enregistrer\"></center>";
	?>
</form>
Modifié en dernier par Sylv@in le 16 mai 2008, 11:24, modifié 1 fois.

Eléphant du PHP | 175 Messages

16 mai 2008, 11:24

c'est toi ;)

Car administration_test.php est le fichier pour lequel je vous ai fourni le code
:shock: ah oui jolie structure de fichier, heureusement que j'ai pas trop mangé ce matin... :shock:

l'update en plein milieu de la structure html ... et la a chaque chargement de page ...

si on ne gere que les etats, on peut faire un truc du genre:
    include ('include/connexion.php'); 
    $connexion=connexion(); 
	if ($_POST){
		if (is_array($_POST['etat'])){
			foreach ($_POST['etat'] as $cle=>$valeur){
				$upd = 'UPDATE formulaire SET etat="'.$valeur.'" WHERE id="'.$cle.'"'; 
				mysql_query($upd); 
			}
                               //idealement on fais une redirection sur soit meme... pour eviter de refaire un update a chaque refresh...
		}
	}
    //WHERE etat = 'En cours' OR etat = 'Verification' OR  etat = 'Autorisation' 
    $sql = "SELECT * FROM formulaire ORDER BY `formulaire`.`id` DESC"; 
    $result=mysql_query($sql); 
echo '<td align="center">select size="1" name="etat['.$id.']" tabindex="3">';

Eléphant du PHP | 422 Messages

16 mai 2008, 11:28

Ton problème est là
$upd = 'UPDATE formulaire SET etat="'.$etat.'" WHERE id="'.$id.'"'; 
La variable $etat provient du <select name="etat" >, mais avec un ancien paramétrage de PHP qui transformait automatiquement les données de formulaire HTML en variables PHP. Cela n'est plus vrai maintenant. Il faut auparavant faire
$etat = $_POST["etat"];
cela dit, mélanger du HTML et du PHP, ce n'est pas terrible. Et puis, personnellement, je n'aime pas trop mélanger la saisie d'un formulaire et la mise à jour d'une base : je préfère une page pour chaque, quitte à ce que la page de mise à jour de la base soit redirigée automatiquement vers la page du formulaire.
Modifié en dernier par caroube le 16 mai 2008, 11:34, modifié 2 fois.

ViPHP
ViPHP | 5924 Messages

16 mai 2008, 11:31

c'est un feux d'artifice de "echos" disdonc.. tu as fait du vbscript ?
Rho l'attaque gratuite, c'est moche :D

Petit nouveau ! | 9 Messages

16 mai 2008, 11:33

Ton problème est là
$upd = 'UPDATE formulaire SET etat="'.$etat.'" WHERE id="'.$id.'"'; 
La variable $etat provient du <select name="etat" >, mais avec un ancien paramétrage de PHP qui transformait automatiquement les données de formulaire HTML en variables PHP. Cela n'est plus vrai maintenant. Il faut auparavant faire
$etat = $_POST["etat"];
cela dit, mélanger du HTML et du PHP, ce n'est pas terrible. Et puis, personnellement, je n'aime pas trop mélanger la saisie d'un formulaire et la mise à jour d'une base : je préfère une page pour chaque, quitte à ce que la page de mise à jour de la base soit redirigée automatiquement vers la page du formulaire.
Ce n'est pas moi qui veux faire ça ;) c'est un collègue. Je lui ai dis que ce serait plus propre de faire comme tu le propose mais il préfère modifier tout d'un coup
c'est toi ;)

Car administration_test.php est le fichier pour lequel je vous ai fourni le code
:shock: ah oui jolie structure de fichier, heureusement que j'ai pas trop mangé ce matin... :shock:

l'update en plein milieu de la structure html ... et la a chaque chargement de page ...

si on ne gere que les etats, on peut faire un truc du genre:
    include ('include/connexion.php'); 
    $connexion=connexion(); 
	if ($_POST){
		if (is_array($_POST['etat'])){
			foreach ($_POST['etat'] as $cle=>$valeur){
				$upd = 'UPDATE formulaire SET etat="'.$valeur.'" WHERE id="'.$cle.'"'; 
				mysql_query($upd); 
			}
                               //idealement on fais une redirection sur soit meme... pour eviter de refaire un update a chaque refresh...
		}
	}
    //WHERE etat = 'En cours' OR etat = 'Verification' OR  etat = 'Autorisation' 
    $sql = "SELECT * FROM formulaire ORDER BY `formulaire`.`id` DESC"; 
    $result=mysql_query($sql); 
echo '<td align="center">select size="1" name="etat['.$id.']" tabindex="3">';
ça ne fonctionne pas

Eléphant du PHP | 422 Messages

16 mai 2008, 11:36

Je n'avais pas vu. tu as $etat qui est positionné avec ce qui est lu dans la base de données. Tu mets à jour avec ce qui est lu dans la base de données, donc ça ne bouge pas. Je mettrais plutôt
$newetat = $_POST["etat"];
if ($newetat) {
  $upd = 'UPDATE formulaire SET etat="'.$newetat.'" WHERE id="'.$id.'"'; 
  mysql_query($upd); 
}
et je remonterais tout ça en haut de la page, avant le SELECT, parce que sinon, tu vas afficher l'ancienne valeur
Modifié en dernier par caroube le 16 mai 2008, 11:37, modifié 1 fois.

Petit nouveau ! | 9 Messages

16 mai 2008, 11:37

Ton problème est là
$upd = 'UPDATE formulaire SET etat="'.$etat.'" WHERE id="'.$id.'"'; 
La variable $etat provient du <select name="etat" >, mais avec un ancien paramétrage de PHP qui transformait automatiquement les données de formulaire HTML en variables PHP. Cela n'est plus vrai maintenant. Il faut auparavant faire
$etat = $_POST["etat"];
cela dit, mélanger du HTML et du PHP, ce n'est pas terrible. Et puis, personnellement, je n'aime pas trop mélanger la saisie d'un formulaire et la mise à jour d'une base : je préfère une page pour chaque, quitte à ce que la page de mise à jour de la base soit redirigée automatiquement vers la page du formulaire.
fonctionne pas non plus

Eléphant du PHP | 175 Messages

16 mai 2008, 11:37

c'est meme encore plus rigolo que ca, a aucun moment ca ne peut prendre en compte la veleur postée.

$etat est settée par la requete de select
$etat=$tableau[11];

dans le select la velur est settee alors que ca doit etre dans les options:

Code : Tout sélectionner

<select size=\"1\" name=\"etat\" value=\"$etat\" tabindex=\"3\">
donc plutot:

Code : Tout sélectionner

<select size=\"1\" name=\"etat\" tabindex=\"3\">
et ensuite pour selectionner l'opion en cours utilisation de :

Code : Tout sélectionner

<option value=\"$etat\">$etat</option>
au lieu de tester chaque option:
echo '<option value="Verification"',
if ($etat=="Verification") echo ' selected="selected";
echo '>Verification</option>

Petit nouveau ! | 9 Messages

16 mai 2008, 11:44

c'est meme encore plus rigolo que ca, a aucun moment ca ne peut prendre en compte la veleur postée.

$etat est settée par la requete de select
$etat=$tableau[11];

dans le select la velur est settee alors que ca doit etre dans les options:

Code : Tout sélectionner

<select size="1" name="etat" value="$etat" tabindex="3">
donc plutot:

Code : Tout sélectionner

<select size="1" name="etat" tabindex="3">
et ensuite pour selectionner l'opion en cours utilisation de :

Code : Tout sélectionner

<option value="$etat">$etat</option>
au lieu de tester chaque option:
echo '<option value="Verification"',
if ($etat=="Verification") echo ' selected="selected";
echo '>Verification</option>
heu donc je fait quoi là :roll:

<select size="1" name="etat" tabindex="3">
puis
<option value="Verification"',
if ($etat=="Verification") echo ' selected="selected";
echo '>Verification</option>

et j'enlève $etat=$tableau[11]

EDIT : J'avoue que je suis perdu là entre les deux solutions

Eléphant du PHP | 175 Messages

16 mai 2008, 11:55

voila le fichier complet avec les modifs:
(pour ma part vous aurez remarque que les echo ne me genent pas plus que ca :twisted: )
<?php 
    include ('include/connexion.php'); 
    $connexion=connexion(); 
	//en cas de soumission du formulaire on va modifier chaque id...
	if ($_POST){
		if (is_array($_POST['etat'])){
			foreach ($_POST['etat'] as $cle=>$valeur){
				$upd = 'UPDATE formulaire SET etat="'.$valeur.'" WHERE id="'.$cle.'"'; 
				mysql_query($upd); 
			}
		}
	}
	
	//on recupere les valeurs de la base:
    $sql = "SELECT * FROM formulaire ORDER BY `formulaire`.`id` DESC"; 
    $result=mysql_query($sql); 
     
    echo "<table border=\"1\" width=\"100%\" align=\"center\"> 
    <tr> 
    <td class=\"title\" align=\"center\">&nbsp;N°&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Identifiant&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Nom&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Prénom&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Service&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Téléphone&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Nom du site&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;URL&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Catégorie&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Motivation de la demande&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Date&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Etat&nbsp;</td> 
    <td class=\"title\" align=\"center\">&nbsp;Commentaire&nbsp;</td> 
    </tr>"; 
     
    //$tableau = mysql_fetch_array($result); 
     
    while ($tableau = mysql_fetch_row($result)) 
        { 
            $id=$tableau[0]; 
            $identifiant=$tableau[1]; 
            $ndemandeur=$tableau[2]; 
            $prenom=$tableau[3]; 
            $service=$tableau[4]; 
            $telephone=$tableau[5]; 
            $nsi=$tableau[6]; 
            $url=$tableau[7]; 
            $categorie=$tableau[8]; 
            $mdld=$tableau[9]; 
            $date=$tableau[10]; 
            $etat=$tableau[11]; 
            $com=$tableau[12]; 
?> 


<form method="post" action="administration_test.php"> 
<?php 
     

	echo "<tr> 
		 <td align=\"center\">$id&nbsp;</td> 
		 <td>$identifiant&nbsp;</td> 
		 <td>$ndemandeur&nbsp;</td> 
		 <td>$prenom&nbsp;</td> 
		 <td>$service&nbsp;</td> 
		 <td>$telephone&nbsp;</td> 
		 <td>$nsi&nbsp;</td> 
		 <td><a href=\"$url\" target=\"_blank\">$url</a>&nbsp;</td> 
		 <td>$categorie&nbsp;</td> 
		 <td>$mdld&nbsp;</td> 
		 <td align=\"center\">$date&nbsp;</td> 
		<td align=\"center\">";
		echo '<td align="center">select size="1" name="etat['.$id.']" tabindex="3">';
                                   echo '<option value="">Selectionnez un etat</option>';
			echo '<option value="Verification"', 
			if ($etat=="Verification") echo ' selected="selected"'; 
			echo '>Verification</option>';
			
			echo '<option value="Autorisation"', 
			if ($etat=="Autorisation") echo ' selected="selected"'; 
			echo '>Autorisation</option>';
			
			echo '<option value="Accepter"', 
			if ($etat=="Accepter") echo ' selected="selected"'; 
			echo '>Accepter</option>';
			
			echo '<option value="Refuser"', 
			if ($etat=="Refuser") echo ' selected="selected"'; 
			echo '>Refuser</option>';
		echo '</select>';
			
		echo "<td align=\"center\"><textarea rows='5' cols='15' name='comen'>$com</textarea></td> 
            </tr>"; 
             
    } 
    echo "</table> 
    <br><center><input type=\"submit\" value=\"Enregistrer\"></center>"; 
    ?> 
</form>

Petit nouveau ! | 9 Messages

16 mai 2008, 11:59

Parse error: syntax error, unexpected T_IF in D:\gmf\apps\homol\webwasher\administration_test.php on line 95

Ligne 95 :

if ($etat=="Verification") echo ' selected="selected"';