Utilisation de for pour sauvegarde multiple

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Utilisation de for pour sauvegarde multiple

par modeste » 21 avr. 2007, 13:37

je ne comprends vraiment pas j'ai fait pas mal d' essais différents mais j'ai toujours la même erreur
peux tu aller plus loin dans ton explication stp
merci d'avance

par Ryle » 21 avr. 2007, 13:11

C'est peut être ta condition d'arrêt de la boucle qui ne convient pas, elle va s'exécuter tant que : $nb < $nb + 1

Et je pense que $nb restera longtemps inférieur à sa valeur + 1 ;)

par modeste » 21 avr. 2007, 13:06

et avec cela j'ai la même erreur

Code : Tout sélectionner

for ($nb=0 ;$nb<$nb+1 ; $nb++){
je suis paumé

par modeste » 21 avr. 2007, 13:01

je ne comprends pas
j'ai fait un essai avec ça

Code : Tout sélectionner

/////////////////////////////// function save_entrees() { /////////////////////////////// $date_entree=$_POST['date_entree']; $bl_entree=$_POST['bl_entree']; $id_fourn=$_POST['id_fourn']; $id_arti=$_POST['id_arti']; $nb=$_POST['nb']; $qte=$_POST['qte']; for ($nb<>0 ;$nb<$nb+1 ; $nb++){ $query_liste_arti = "SELECT ID_ARTI, MOUV_ARTI FROM arti_cryolor where ID_ARTI='$id_arti'"; $result_liste_arti = mysql_query($query_liste_arti) or die("Erreur dans la requête query_liste_arti "); while ($row_liste_arti = mysql_fetch_array($result_liste_arti, MYSQL_NUM)) { $id_arti=$row_liste_arti[0]; $mouv_arti=$row_liste_arti[1]; $new_mouv= "$mouv_arti+$qte"; //$query_update_articles="UPDATE arti_cryolor SET MOUV_ARTI= '$new_mouv' WHERE ID_ARTI= '$id_arti' " ; //mysql_query($query_update_articles)or die("Invalid query: " . mysql_error()); print"UPDATE arti_cryolor SET MOUV_ARTI= '$new_mouv' WHERE ID_ARTI= '$id_arti' " ; //$query_save_entree="INSERT INTO entree_cryolor VALUES ('0','$date_entree','$bl_entree','$id_fourn','0','$id_arti','$qte','$nb','','1','0')"; //mysql_query($query_save_entree)or die("Invalid query: " . mysql_error()); print"INSERT INTO entree_cryolor VALUES ('0','$date_entree','$bl_entree','$id_fourn','0','$id_arti','$qte','$nb','','1','0')"; } } print"pas cool"; }
comme résultat j'ai cela

Code : Tout sélectionner

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Documents and Settings\Modeste\Mes documents\projet\www\cde\php\acces_base.php on line 56

par Ryle » 21 avr. 2007, 12:04

Quelques explications sur la syntaxe d'une boucle for ne me semble pas superflues :
for ($i = 0 ; $i < $j ; $i++){ 
  ...
}
:arrow: Pour une valeur de $i égal à 0 ($i=0),
et tant que la valeur de $i est inférieure à celle de $j ($i<$j)
exécute le code suivant {}, puis incrémente $i de 1 ($i++).

On a donc dans ton cas :
for ($nb<>0 ;$nb==$nb ; $nb++){ 
  ...
}
:arrow: Pour (valeur de $nb différente de 0), ce qui correspon à un test évalué comme TRUE/FALSE
Tant que $nb est identique à lui même (ce qui risque pas de changer ;))
exécute le code suivant {}, puis incrémente $nb de 1.

Vois tu mieux ce qui cloche ? :)

Utilisation de for pour sauvegarde multiple

par modeste » 21 avr. 2007, 11:13

Bonjour,
j'espère que quelqu'un pourra m'aider sur ce coup là

j'ai un peu de mal avec l'utilisation du "FOR" pour ma sauvegarde
explication:
je reçois un bl avec 5 articles
1ere etape
j'entre la date, le n de bl, je choisit un fournisseur, j'entre le nombre d'article sur mle bl

Code : Tout sélectionner

<div id="first">*** Ajout d'une entrée *** <div id="ajout_entree"> <?php print "<form method=\"post\" action=\"index.php?op=fin_entrees\" >"; $date_entree= date("d/m/Y"); print"<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date entrée:&nbsp;"; print"<input type=\"text\" name=\"date_entree\" size=8 maxlength=10 value=\"$date_entree\">"; print"&nbsp;&nbsp;&nbsp;N° B.L.:&nbsp;"; print"<input type=\"text\" name=\"bl_entree\" size=10 maxlength=10 value=\"$bl_entree\"><br><br>"; print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fournisseur:&nbsp;"; print "<td><select name=\"id_fourn\">"; //Création de la liste des fournisseurs $query_choix_fourn="SELECT DISTINCT(ID_FOURN), NOM_FOURN FROM fourn_cryolor order by NOM_FOURN asc"; $result_choix_fourn=mysql_query($query_choix_fourn) or die("Erreur dans la requête query_choix_fourn"); $i=0; $test=0; while ($row_choix_fourn = mysql_fetch_array($result_choix_fourn, MYSQL_NUM)) { $choix_fourn="$row_choix_fourn[0]"; if ($i=="0" || ($fourn_post=="%" && $test==0)){ print "<option value=\"\" selected>Choississez ...</option>"; $test=1; } if ($fourn_post=="$choix_fourn"){ print "<option value=\"$choix_fourn\" selected>$row_choix_fourn[1]</option>"; }else{ print "<option value=\"$choix_fourn\">$row_choix_fourn[1]</option>"; } $i++; } $test=0; print "</select>"; print"<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Catégorie:&nbsp;"; print "<td><select name=\"id_cat\">"; //Création de la liste des proprietairess $query_choix_cat="SELECT DISTINCT(CAT_ARTI), NOM_CAT FROM cat_cryolor order by NOM_CAT asc"; $result_choix_cat=mysql_query($query_choix_cat) or die("Erreur dans la requête query_choix_cat"); $i=0; $test=0; while ($row_choix_cat = mysql_fetch_array($result_choix_cat, MYSQL_NUM)) { $choix_cat="$row_choix_cat[0]"; if ($i=="0" || ($cat_post=="%" && $test==0)){ print "<option value=\"\" selected>Choississez ...</option>"; $test=1; } if ($cat_post=="$choix_cat"){ print "<option value=\"$choix_cat\" selected>$row_choix_cat[1]</option>"; }else{ print "<option value=\"$choix_cat\">$row_choix_cat[1]</option>"; } $i++; } $test=0; print "</select>"; print"&nbsp;&nbsp;&nbsp;Nbres d'articles:&nbsp;"; print"<input type=\"text\" name=\"nb\" size=10 maxlength=10 value=\"$nb\"><br><br>"; ?> <p class="ajout"> <?php print"<input type=\"submit\" name=\"fin_entrees\" value=\"Sauvegarder\">";?> </p> <?php print"</form>"; ?> </div> <p class="info">Possibilité de modifier la date<br>Le nombre d'articles sert pour la suite</p> </div>
2eme étape
j'entre les articles

Code : Tout sélectionner

<div id="first">*** Ajout d'une entrée *** <div id="fin_entree"> <?php $date_entree=$_POST['date_entree']; $bl_entree=$_POST['bl_entree']; $id_fourn=$_POST['id_fourn']; $id_cat=$_POST['id_cat']; $nb=$_POST['nb']; print "<form method=\"post\" action=\"index.php?op=save_entree\" >"; $date_entree= date("d/m/Y"); print"&nbsp;&nbsp;&nbsp;&nbsp;Date entrée:&nbsp;"; print"<input type=\"text\" name=\"date_entree\" size=8 maxlength=10 value=\"$date_entree\">"; print"&nbsp;&nbsp;&nbsp;N° B.L.:&nbsp;"; print"<input type=\"text\" name=\"bl_entree\" size=10 maxlength=10 value=\"$bl_entree\">"; print"&nbsp;&nbsp;&nbsp;Fournisseur:&nbsp;"; print "<td><select name=\"id_fourn\">"; //Création de la liste des fournisseurs $query_choix_fourn="SELECT DISTINCT(ID_FOURN), NOM_FOURN FROM fourn_cryolor order by NOM_FOURN asc"; $result_choix_fourn=mysql_query($query_choix_fourn) or die("Erreur dans la requête query_choix_fourn"); $i=0; $test=0; while ($row_choix_fourn = mysql_fetch_array($result_choix_fourn, MYSQL_NUM)) { $choix_fourn="$row_choix_fourn[0]"; if ($i=="0" || ($fourn_post=="%" && $test==0)){ print "<option value=\"\" selected>Choississez ...</option>"; $test=1; } if ($id_fourn=="$choix_fourn"){ print "<option value=\"$choix_fourn\" selected>$row_choix_fourn[1]</option>"; }else{ print "<option value=\"$choix_fourn\">$row_choix_fourn[1]</option>"; } $i++; } $test=0; print "</select><br><br>"; $i=1; while ($i<>$nb+1){ if ($i<10){ print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*** 0$i ***&nbsp;"; } else{ print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*** $i ***&nbsp;"; } print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Article:&nbsp;"; print "<td><select name=\"id_arti[]\">"; //Création de la liste des Catégories $query_choix_arti="SELECT DISTINCT(ID_ARTI), REF_ARTI, MAT_ARTI, DIM_ARTI FROM arti_cryolor WHERE CAT_ARTI='$id_cat' order by MAT_ARTI, REF_ARTI asc"; $result_choix_arti=mysql_query($query_choix_arti) or die("Erreur dans la requête query_choix_arti"); $e=0; $test=0; while ($row_choix_arti = mysql_fetch_array($result_choix_arti, MYSQL_NUM)) { $choix_arti="$row_choix_arti[0]"; if ($e=="0" || ($arti_post=="%" && $test==0)){ print "<option value=\"\" selected>Choississez ...</option>"; $test=1; } if ($id_arti=="$choix_arti"){ print "<option value=\"$choix_arti\" selected>$row_choix_arti[1] - $row_choix_arti[3] - $row_choix_arti[2]</option>"; }else{ print "<option value=\"$choix_arti\">$row_choix_arti[1] - $row_choix_arti[3] - $row_choix_arti[2]</option>"; } $e++; } $test=0; print "</select>"; print"&nbsp;&nbsp;&nbsp;Quantité:&nbsp;"; print"<input type=\"text\" name=\"qte[]\" size=5 maxlength=5 value=\"$qte\"><br>"; $i++; } ?> <p class="ajout"> <?php print"<input type=\"submit\" name=\"save_entree\" value=\"Sauvegarder\">";?> </p> <?php print"</form>"; ?>
3eme étape
je sauvegarde le tout et c'est là que j'ai un problème

Code : Tout sélectionner

/////////////////////////////// function save_entrees() { /////////////////////////////// $date_entree=$_POST['date_entree']; $bl_entree=$_POST['bl_entree']; $id_fourn=$_POST['id_fourn']; $id_arti=$_POST['id_arti']; $nb=$_POST['nb']; $qte=$_POST['qte']; for ($nb<>0 ;$nb==$nb ; $nb++){ $query_liste_arti = "SELECT ID_ARTI, MOUV_ARTI FROM arti_cryolor where ID_ARTI=$id_arti"; $result_liste_arti = mysql_query($query_liste_arti) or die("Erreur dans la requête query_liste_arti "); while ($row_liste_arti = mysql_fetch_array($result_liste_arti, MYSQL_NUM)) { $id_arti=$row_liste_arti[0]; $mouv_arti=$row_liste_arti[1]; $new_mouv= "$mouv_arti+$qte"; //$query_update_articles="UPDATE arti_cryolor SET MOUV_ARTI= '$new_mouv' WHERE ID_ARTI= '$id_arti' " ; //mysql_query($query_update_articles)or die("Invalid query: " . mysql_error()); print"UPDATE arti_cryolor SET MOUV_ARTI= '$new_mouv' WHERE ID_ARTI= '$id_arti' " ; //$query_save_entree="INSERT INTO entree_cryolor VALUES ('0','$date_entree','$bl_entree','$id_fourn','0','$id_arti','$qte','$nb','','1','0')"; //mysql_query($query_save_entree)or die("Invalid query: " . mysql_error()); print"INSERT INTO entree_cryolor VALUES ('0','$date_entree','$bl_entree','$id_fourn','0','$id_arti','$qte','$nb','','1','0')"; } print"pas cool"; }