PB d'utilisation de la boucle for

Fermé
msi79
Messages postés
500
Date d'inscription
lundi 24 août 2009
Statut
Membre
Dernière intervention
13 avril 2021
- 5 févr. 2012 à 12:12
salut . j'aimerai utiliser la boucle for pour inserer des données dans ma base de données mais j'arrive pas .
en effet la personne choisi le nombre données qu'elle voudra insérer . rempli les champs et insere . mais il se trouve que par exemple quand le personne veux insérer 2 données différentes , ça insere qu'une meme donnée .
merci de bien vouloir m'aider

voici mon code :
<?php
  if(isset($_GET['prejud_sid'])) $prejud_sid = urldecode($_GET['prejud_sid']);
if(isset($_POST['bouton_submit'])&& !empty($_POST['nbre'])){
 $cat = strip_tags(trim($_POST['cat'])); 
  
    for($i = 1; $i <= $_POST['nbre']; $i++){
//if(isset($_FILES['img'.$i]) && $_FILES['img'.$i]['error'] == 0 && isset($_POST['nbre'])){
 /*IMPORTANT*/ 

//include("connection.php"); 
// Connexion à la BDD
 mysql_connect("localhost","root","") or die("erreur de connexion");
 // Sélection de la base de données
mysql_select_db("DirectVictime2") or die("Erreur de selection de la BD");

/*faut securiser*/ 
foreach($_POST as $k => $v){ 
$v=mysql_real_escape_string(strip_tags($v)); 
$_POST[$k]=$v; 
} 

/*faut securiser*/ 
foreach($_GET as $k => $v){ 
$v=mysql_real_escape_string(strip_tags($v)); 
$_POST[$k]=$v; 
}

	
	$NumCode1=trim($_POST["NumCode1".$i]);
	$designation1=trim($_POST["designation1".$i]);
	$code1=trim($_POST["code1".$i]);
	$quantite1=trim($_POST["quantite1".$i]);
	$dateSurv1=trim($_POST["dateSurv1".$i]);
	$lieuSurv1=trim($_POST["lieuSurv1".$i]);
	$naturePrej1=trim($_POST["naturePrej1".$i]);
	$autrePrej1=trim($_POST["autrePrej1".$i]);

if (empty($code1)) {
		$msg="<font color=red>Veuillez saisir  le numero du questionnaire!!!!!!!!!</font>";
	}
	else {
		

 // on fait l'INSERT dans la base de données
		$sql="INSERT INTO prejudice1(id_prejudice1,NumCode1,designation1,code1,quantite1,dateSurv1,lieuSurv1,naturePrej1,autrePrej1,prejud_sid) VALUES('','".$NumCode1."','".$designation1."','".$code1."','".$quantite1."','".$dateSurv1."','".$lieuSurv1."','".$naturePrej1."','".$autrePrej1."','".$prejud_sid."')";
mysql_query($sql) or die(mysql_error());
echo 'Les informations sur les prejudices ont été ajoutées dans la base de données.</br>';

	}
 }
		
}

//}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
<style type="text/css">
<!--
.Style3 {
	color: #0000FF;
	font-weight: bold;
}
.Style4 {
	color: #990033;
	font-weight: bold;
}
.Style6 {color: #990000}
-->
</style>
</head>

<body>
<table width="200" border="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="1179" height="38" border="0" align="left">
  <tr>
    <td width="107"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload2.macromedia.com/get/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="107" height="18" title="IMAGES">
      <param name="movie" value="button9.swf" />
      <param name="quality" value="high" />
      <param name="bgcolor" value="#FFFFFF" />
      <embed src="button9.swf" quality="high" pluginspage="https://get.adobe.com/flashplayer/" type="application/x-shockwave-flash" width="107" height="18" bgcolor="#FFFFFF"></embed>
    </object></td>
    <td width="355"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload2.macromedia.com/get/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="107" height="18" title="VICTIMES">
      <param name="movie" value="button7.swf" />
      <param name="quality" value="high" />
      <embed src="button7.swf" quality="high" pluginspage="https://get.adobe.com/flashplayer/" type="application/x-shockwave-flash" width="107" height="18" ></embed>
    </object></td>
    <td width="34">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="34">&nbsp;</td>
    <td width="364">&nbsp;</td>
    <td width="107"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload2.macromedia.com/get/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="107" height="18" title="PREJUDICES">
      <param name="movie" value="button11.swf" />
      <param name="quality" value="high" />
      <embed src="button11.swf" quality="high" pluginspage="https://get.adobe.com/flashplayer/" type="application/x-shockwave-flash" width="107" height="18" ></embed>
    </object></td>
  </tr>
</table>
<h1 align="center" class="subfooter  Style3">Enregistrement de préjudices </h1>
<form action="" method="post" >
  <p>&nbsp;</p>
  <table width="570" height="95" border="0" align="center" bordercolor="#0000FF" bgcolor="#83E7FC"> 
  <tr> <td width="180"><span class="Style4">Saisir le nombre de préjudices:</span></td> <td width="380"><input name="nbre" type="text" size="1" maxlength="1" value="<?php if(isset($_POST['nbre']))echo $_POST['nbre'];?>"/></td> </tr> <tr> <td>&nbsp;</td> <td bordercolor="#0000FF"> <fieldset> <legend><span class="Style6">AJOUTER DES PREJUDICES</span></legend>	 
  <?php if(isset($_POST['bouton_submit']) && ($_POST['nbre'])){ extract($_POST); $nbre_photo = $nbre; for($j = 1; $j<= $nbre_photo; $j++){ echo'
                    <table width="1203" border="0">
                      <tr>
                        <td width="54"><strong>
                          <input name="NumCode1" type="text" size="8" />
                        </strong></td>
                        <td width="149"><strong>
                          <input type="text" name="designation1" />
                        </strong></td>
                        <td width="94"><strong>
                          <input name="code1" type="text" size="15" />
                        </strong></td>
                        <td width="146"><strong>
                          <input type="text" name="quantite1" />
                        </strong></td>
                        <td width="147"><strong>
                          <input type="text" name="dateSurv1" />
                        </strong></td>
                        <td width="144"><strong>
                          <input type="text" name="lieuSurv1" />
                        </strong></td>
                        <td width="116"><strong>
                          <select name="naturePrej1">
                            <option></option>
                            <option>Pillage</option>
                            <option>Expropriation</option>
                            <option>Réquisition</option>
                            <option>Destruction</option>
                          </select>
                        </strong></td>
                        <td width="319">Autre<strong>
                          <input type="text" name="autrePrej1" />
                        </strong></td>
                      </tr>
                    </table><br/>'; } } ?> </fieldset> </td> </tr> <tr> <td>&nbsp;</td> <td><input type="submit" name="bouton_submit" value="envoyer" ></input></td> </tr></table>
</form>
<p>
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload2.macromedia.com/get/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="50" height="37" title="ACCUEIL">
    <param name="movie" value="button4.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#FFFFFF" />
    <embed src="button4.swf" quality="high" pluginspage="https://get.adobe.com/flashplayer/" type="application/x-shockwave-flash" width="50" height="37" bgcolor="#FFFFFF"></embed>
  </object>
</p>
</body>
</html>