Poster un tableau dans un POST

Résolu
Bonjour,

J'ai une lecture de ma base que j'affiche avec des champs de formulaire afin de pouvoir modifier ceux ci.

l'affichage se fait correctement mais je n'arrive pas à poster et récupérer les données afin de mettre ma base à jour.

Pourriez vous m'aider SVP.

<?php 
 session_start();
 
 require_once ('../../connexion.php');

//------------------------  gestion de connexion  --------------------------------

 if(empty($_SESSION['id_utilisateur'])) {
   echo '<script language="Javascript"> document.location.replace("../index.php?erreur=connect"); </script>'; 
 } else { 
 
 $id_utilisateur = $_SESSION['id_utilisateur'];
 
mysql_select_db($database_dbprotect, $bdd);
  $query_users = "SELECT * FROM utilisateurs WHERE id_util = '$id_utilisateur'   "; 
  $users = mysql_query($query_users, $bdd) or die(mysql_error());
 while ( $row_users = mysql_fetch_assoc($users)) {
 
 $nom_utilisateur =  $row_users['nom'];
 $prenom_utilisateur = $row_users['prenom']; 
 $level_utilisateur = $row_users['level']; 
     }  
       }
//-----------------------------------------------------------------------------------

 if (empty($_GET['action'])) { $action = ''; } else { $action = $_GET['action']; }
 if (empty($_POST['classe'])) {$classe = "%"; } else { $classe = $_POST['classe']; } 

  ?>



<!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>Gestion PFMP</title>
<style type="text/css"></style>
<link href="../../css.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.Style20 {font-size: 10px}
</style>
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
</head>

<body>
<table width="1350" border="0" align="center">
  <tr>
    <td width="4%"> </td>
    <td width="93%"> </td>
    <td width="3%"> </td>
  </tr>
  <tr>
    <td height="171"> </td>
    <td class="header_site"><table width="100%" border="0">
      <tr>
        <td width="33%"> </td>
        <td width="28%"> </td>
        <td width="39%"> </td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td class="Style14"><?php echo $level_utilisateur ."<BR>".$prenom_utilisateur ." " .$nom_utilisateur; ?></td>
      </tr>
    </table></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td class="header_titre"><table width="100%" border="0">
      <tr>
        <td width="13%"> </td>
        <td width="70%"><ul id="MenuBar1" class="MenuBarHorizontal">
<li><a href="../accueil.php">Retour Accueil</a></li>
          <li><a href="../../index.php?erreur=logout">Deconnexion</a></li>
        </ul></td>
        <td width="17%" class="sous_titre">Gestion des Classes</td>
      </tr>
    </table></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td><table width="100%" border="0">
        <tr>
          <td width="12%"><img src="../../images/frise.jpg" width="130" height="613" /></td>
          <td width="84%" valign="top"><table width="100%" border="0" align="center">
              <tr>
                <td width="5%"> </td>
                <td width="91%"><span class="Style5"><div style="width:900px; height:550px; overflow:auto; border:solid 0px black;">
                
                 
                  
                    <table width="100%" border="0" align="center">
                      <tr>
                        <td width="91%"> </td>
                        <td width="9%">
                  </td>
                      </tr>
                    </table>
                    <p>
                    
          <form id="form1" name="form1" method="post" action="gest_classes.php?action=maj">    
            
          <input type="submit" name="button" id="button" value="Envoyer" />    
                               
                      <?php  
  
 
 $db= mysql_select_db( "gest_stages" );
 $sql = "SELECT * FROM classes WHERE classes LIKE '$classe' ORDER BY classes ASC";   

 $requete = mysql_query( $sql, $bdd ) or die( "ERREUR MYSQL numéro: ".mysql_errno()."<br>Type de cette erreur: ".mysql_error()."<br>\n" );

 echo( "<table border="1"   frame="hsides" rules="rows" cellpadding="5" cellspacing="0" align="center">\n" );
 echo( "<tr>

  <td width = '100'><div align="center"> <b> Classe </div></td></b>
  <td width = '600'><div align="center"> <b> Diplôme </div></td></b>
  <td width = '200'><div align="center"> <b> Prof Principal </div></td></b>
  <td width = '200'><div align="center"> <b> Prof Référent </div></td></b>
  <td ><div align="center"> <b> </div></td></b>
  

  </tr>" );
 
 while( $result = mysql_fetch_assoc( $requete ) )
 {
 echo( "<tr>\n" );
 echo( "<td><div align="center">".$result["classes"]."</div></td>\n" );
 echo( "<td><div align="center">".$result["titre"]."</div></td>\n" );
 echo( "<td><div align="center"> <input type="text" name="prof_prin"id="prof_prin" value = "".$result["prof_prin"] .""/>"."</div></td>\n" );
 echo( "<td><div align="center"> <input type="text" name="prof_ref"id="prof_ref" value = "".$result["prof_referent"] .""/>"."</div></td>\n" );
 echo( "<td><div align="center"> <input type="text" size="2" name="id_classe" id="id_classe" value = "".$result["id_classe"] .""/>"."</div></td>\n" );
 }
 
  echo( "</tr>\n" );

 echo( "</table>\n" );

  ?>
                    </p>
                  </form></td>
                <td width="4%" valign="top"> </td>
              </tr>
          </table></td>
          <td width="4%"> </td>
        </tr>
      </table></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td width="93%" valign="top" class="footer_site"><?php include("../../footer.php"); ?>      
 <br /></td>
    <td> </td>
    <td> </td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="10">
  <tr>
   
  </tr>
</table>

<table class="ds_box" cellpadding="0" cellspacing="0" id="ds_conclass" style="display: none;">
<tr><td id="ds_calclass">

</td></tr>
</table>

</body>
</html>

<?php

 //-----------------------   MAJ BDD -------------------------
if($action == "maj") {

  mysql_select_db($database_dbprotect, $bdd);
  $query_users = "SELECT count(*) AS id_classe FROM classes "; 
     $users = mysql_query($query_users, $bdd) or die(mysql_error());
  while ( $row_users = mysql_fetch_assoc($users)) {
     $nb_classe = $row_users['id_classe']; }

 //---------------------------
 $i = 0; 
     while($i < $nb_classe) 
     { 
 $id_classe = $_POST['id_classe'];
 $prof_referent = $_POST['prof_ref'];
 $prof_prin = $_POST['prof_prin'];
 

 mysql_query (" UPDATE classes SET  prof_referent = '$prof_referent', prof_prin = '$prof_prin'
       WHERE id_classe = '$id_classe' ") ;

     $i++;
  
/* echo '<script language="Javascript"> document.location.replace("gest_classes.php"); </script>';  */
     }
       
}
 ?>



4 réponses

  1. très content pour toi, par contre tu n'as pas tenu compte de ma remarque sur les " et ' dans les chaines echo
    0
    1. déja une remarque:

      si tu commences et finit une chaine dans un echo par des " il ne faut pas qu'il y ait des " à l'intérieur de ta chaine

      donc echo '<input name="xxxx" id="yyy" >';

      pour poster sous forme d'array tu mets le name avec des [] comme ceci:

      while( $result = mysql_fetch_assoc( $requete ) )
      {
      echo( '<tr>\n' );
      echo( '<td><div align="center">".$result["classes"]."</div></td>\n' );
      echo( '<td><div align="center">".$result["titre"]."</div></td>\n' );
      echo( '<td><div align="center"> <input type="text" name="prof_prin[]" id="prof_prin" value = "".$result["prof_prin"] .""/>"."</div></td>\n' );
      echo( '<td><div align="center"> <input type="text" name="prof_ref[]" id="prof_ref" value = "".$result["prof_referent"] .""/>"."</div></td>\n' );

      et pour récupérer du fait des [] le POST devient un array:

      //ci dessous les array qui contiennent les valeurs postées 
      $array_id_classe = $_POST['id_classe'];
      $array_prof_referent = $_POST['prof_ref'];
      $array_prof_prin = $_POST['prof_prin'];

      //tu les parcours par une boucle
      foreach($array_id_classe as $key=>$value_id){
      $id_classe=$value_id;
      //l'index $key te permet de retrouver les valeurs correspondantes des adeux autres array
      $prof_referend=$array_prof_referent[$key];
      $prof_prin=$array_prof_prin[$key];

      //et la tu mets ta BDD à jour à chaque passage de la boucle
      }
      0
      1. cela a l'air logique mais ne fonctionne pas

        message d'erreur: Undefined index: id_classe in E:\APP_DATA\wamp\www\Gest_PFMP_2014\fichiers\pfmp\gest_classes.php on line 203.

        j'ai rajouter une ligne
         echo( '<td><div align="center"> <input type="text" name="id_classe[]" id="id_classe" value = "".$result["id_classe"] .""/>"."</div></td>\n' );

        mais non plus
        0
      2. Youpii ca fonctionne
        le code est pour le formulaire
        	while( $result = mysql_fetch_assoc( $requete ) )
        	{
        	echo( "<tr>\n" );
        	echo( "<td><div align="center">".$result["classes"]."</div></td>\n" );
        	echo( "<td><div align="center">".$result["titre"]."</div></td>\n" );
        	 echo( "<td><div align="center"> <input type="text" name="prof_prin[]" id="prof_prin" value = "".$result["prof_prin"] .""/>"."</div></td>\n" );
        	echo( "<td><div align="center"> <input type="text" name="prof_referent[]" id="prof_referent" value = "".$result["prof_referent"] .""/>"."</div></td>\n" );	
        	echo( "<td><div align="center"> <input type="text" size="1" class="Style20" readonly="readonly" name="classe[]" id="classe" value = "".$result["id_classe"] .""/>"."</div></td>\n" );
        	
        	}
        	
         	echo( "</tr>\n" );
        
        	echo( "</table>\n" );


        pour l'insertion dans la base
        //ci dessous les array qui contiennent les valeurs postées 
          $array_id_classe = $_POST['classe'];
          $array_prof_referent = $_POST['prof_referent'];
          $array_prof_prin = $_POST['prof_prin'];
         
         //tu les parcours par une boucle
         foreach($array_id_classe as $key=>$value_id){
        	$id_classe=$value_id;
        	//l'index $key te permet de retrouver les valeurs correspondantes des adeux autres array
        	$prof_referent=$array_prof_referent[$key];
        	$prof_prin=$array_prof_prin[$key];
        	
           mysql_query (" UPDATE classes SET  prof_referent = '$prof_referent', prof_prin = '$prof_prin' WHERE id_classe = '$id_classe' ") ;
         }
         
           echo '<script language="Javascript"> document.location.replace("gest_classes.php?ok"); </script>';    
             }
        
        0
    2. la variable est ligne 115, mais cela fonctionne.
      l'aide se porte sur la facon de poster et recuperer les infos.

      pas sur le code qui fonctionne.
      0
      1. Bonsoir

        if($action == "maj")

        d'où vient cette variable $action

        il vaudrait mieux:

        if($_GET['action'] == "maj")
        0
        1. S IL VOUS PLAIT SOIYEZ CLAIRE
          0