Requêtes non actives

Bonjour,

J'ai un problème dans mon code. Les 2 requêtes UPDATE ne fonctionnent pas

$sql = 'SELECT id,nom,prenom,actif FROM adherent WHERE nom LIKE \''.$nom_adh_r.'%\'  ORDER BY nom';	
			
			try{
				$requete = $pdo->prepare($sql);
				$requete->execute();
				$id = 0;
				$num = 0;

				echo '<tbody>';
				while($var = $requete->fetch()){	
					$nom = stripcslashes($var['nom']);
					$prenom = prenom_double($var['prenom']);
					if($var['actif'] == '0'){
						echo '<tr bgcolor="red">';
							echo '<th scope="row">'.(++$num).'</th>';
							echo '<td>'.$nom.'</td>';
							echo '<td>'.$prenom.'</td>';
							echo '<td class="center">';
							echo '<img src="img/b_drop.png" />';
							echo '</td>';
							echo "<form action='adherent.php' method='post' enctype='multipart/form-data'>";
							echo '<td class="center">';
							?>
							<input type="image" name="change" src="img/b_edit.png" value="changer" onclick="if(!confirm('Etes-vous sûr de vouloir reactiver <?php echo $prenom.' '.$nom ?>')) return false;" />
							<input type="hidden" name="id" value="<?php echo $var['id']; ?>" />			
							<?php
							echo '</td>';
							echo '</form>';
						echo "</tr>";
					}else{
						echo "<tr>";
							echo '<th scope="row">'.(++$num).'</th>';
							echo '<td>'.$nom.'</td>';
							echo '<td>'.$prenom.'</td>';
							echo '<td class="center">';
							echo '<a href="gestion_adherent.php?id='.$var['id'].'&action=edit"><img src="img/b_edit.png" /></a>';
							echo '</td>';
							echo "<form action='adherent.php' method='post' enctype='multipart/form-data'>";
							echo '<td class="center">';
							//echo '<a href="gestion_adherent.php?id='.$var['id'].'&action=del"><img src="img/b_drop.png"  /></a>';
							?>
							<input type="image" name="supp" src="img/b_drop.png" value="Supprimer" onclick="if(!confirm('Etes-vous sûr de vouloir supprimer <?php echo $prenom.' '.$nom ?>')) return false;" />
							<input type="hidden" name="id" value="<?php echo $var['id']; ?>" />			
							<?php
							echo '</td>';
							echo '</form>';
						echo "</tr>";
					}
				}
			}catch(Exception $e){
				echo " Erreur ! ".$e->getMessage();
				echo " Les datas : " ;
				print_r($data);
			}  

Les requêtes

if(isset($_POST['supp'])){
				$reqdel = "UPDATE adherent SET actif = :actif WHERE id = :id";
				$id = $_POST['id'];
				$actif = 0;		
				$data = array(':id'=>$id, ':actif'=>$actif);
				try{
					$requet = $pdo->prepare($reqdel);
					$requet->execute($data);
				}catch(Exception $e){
					echo " Erreur ! ".$e->getMessage();
					echo " Les datas : " ;
					print_r($data);
				}	
			}

			if(isset($_POST['change'])){
				$reqchange = "UPDATE adherent SET actif = :actif WHERE id = :id";
				$id = $_POST['id'];
				$actif = 1;	
				$datas = array(':id'=>$id, ':actif'=>$actif);	
				try{
					$reque = $pdo->prepare($reqchange);
					$reque->execute($datas);
				}catch(Exception $e){
					echo " Erreur ! ".$e->getMessage();
					echo " Les datas : " ;
					print_r($datas);
				}		
			}

Merci d'avance pour votre aide


Macintosh / Chrome 105.0.0.0


Bien PHPment et MySQLment
Delaville

7 réponses

  1. Ne fonctionne toujours pas. je ne vois pas l'erreur

    Le var_dump : 

    Juste le temps des tests :
    array(3) { ["change_x"]=> string(2) "10" ["change_y"]=> string(1) "6" ["id"]=> string(1) "8" }
    Notice: Undefined variable: nom_adh_r in /home/**************/adherent.php on line 43

    Le code complet

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    
    	session_start();
    	if(isset($_SESSION['log']) and $_SESSION['sa'] == 1)
    	{
    	include ('entete.php');	
    
    	echo "Juste le temps des tests :<br>";
    var_dump($_POST);
    
    	if (isset($_POST['supp'])) {
    		$reqdel = "UPDATE adherent SET actif = :actif WHERE id = :id";
    		$id = $_POST['id'];
    		$actif = 0;
    		$data = array(':id' => $id, ':actif' => $actif);
    		try {
    		  $requet = $pdo->prepare($reqdel);
    		  $requet->execute($data);
    		} catch (Exception $e) {
    		  echo " Erreur ! " . $e->getMessage();
    		  echo " Les datas : ";
    		  print_r($data);
    		}
    	}
    	  
    	if (isset($_POST['change'])) {
    	$reqchange = "UPDATE adherent SET actif = :actif WHERE id = :id";
    	$id = $_POST['id'];
    	$actif = 1;
    	$datas = array(':id' => $id, ':actif' => $actif);
    		try {
    			$reque = $pdo->prepare($reqchange);
    			$reque->execute($datas);
    		} catch (Exception $e) {
    			echo " Erreur ! " . $e->getMessage();
    			echo " Les datas : ";
    			print_r($datas);
    		}
    	}
    	$sql = "SELECT id,nom,prenom,actif FROM adherent WHERE nom LIKE '" . $nom_adh_r . "%'  ORDER BY nom";
    
    
    	try {
    		$requete = $pdo->prepare($sql);
    		$requete->execute();
    	} catch (Exception $e) {
    		echo " Erreur ! " . $e->getMessage();
    		echo " Les datas : ";
    	print_r($data);
    	}
    	
    ?>
    	<div class="row">
    		<div class="col-md-8">	
    			<?php
    			echo "<form action='adherent.php' method='post'>";
    				if(isset($_POST['rechercher']))	{		
    					$nom_adh_r = $_POST['nom_adh_r'];			
    				}else{
    					$nom_adh_r = '';
    				}				
    					echo "<table border='0'>";
    					echo "<tr>";
    					echo "<td>Nom :</td>";
    					echo "<td><input type='text' name='nom_adh_r' size='20' value=\"" . $nom_adh_r . "\" /></td>";
    					echo "<td><input class='button_v' type='submit' name='rechercher' size='20' value='Rechercher'></td>";
    					echo "</tr>";
    					echo "</table>";
    			echo "</form>";	
    			?>
    			<hr>
    			<a href="gestion_adherent.php"><img src="img/adds.png" /></a>  Ajouter un adhérent
    			<hr>
    			
    			
    			<div align='left'><b>Liste des adhérents</b></div>		
    			<br />
    			<table class="table table-hover">
    			<thead>
    				<tr>
    				<th scope="col">Numéro</th>
    				<th scope="col">Nom</th>
    				<th scope="col">Prénom</th>
    				<th scope="col" class="center">Modifier</th>
    				<th scope="col" class="center">Supprimer</th>
    				</tr>
    			</thead>
    			<?php
    				$id = 0;
    				$num = 0;
    
    				echo '<tbody>';
    				while($var = $requete->fetch()){	
    					$nom = stripcslashes($var['nom']);
    					$prenom = prenom_double($var['prenom']);
    					if($var['actif'] == '0'){
    						?>
    						<tr style="background-color:red;">
    							<th scope="row"><?php echo (++$num); ?></th>
    							<td><?php echo $nom; ?></td>
    							<td><?php echo $prenom; ?></td>
    							<td class="center">
    							<img src="img/b_drop.png" />
    							</td>
    							<td class="center">
    							<form action='' method='post'>
    								<input type="image" name="change" src="img/b_edit.png" value="changer" onclick="if(!confirm('Etes-vous sûr de vouloir reactiver <?php echo $prenom.' '.$nom ?>')) return false;" />
    								<input type="hidden" name="id" value="<?php echo $var['id']; ?>">			
    							</form>
    							</td>
    						</tr>
    						<?php
    					}else{
    						?>
    						<tr>
    						<th scope="row"><?php echo (++$num); ?></th>
    							<td><?php echo $nom; ?></td>
    							<td><?php echo $prenom; ?></td>
    							<td class="center">
    							<a href="gestion_adherent.php?id=<?php echo $var['id']; ?>&action=edit"><img src="img/b_edit.png" /></a>
    							</td>
    							<td class="center">
    							<form action='' method='post'>
    								<input type="image" name="supp" src="img/b_drop.png" value="Supprimer" onclick="if(!confirm('Etes-vous sûr de vouloir supprimer <?php echo $prenom.' '.$nom ?>')) return false;" />
    								<input type="hidden" name="id" value="<?php echo $var['id']; ?>">	
    							</form>	
    							</td>
    						</tr>
    						<?php
    					}
    				}
    			?>
    			</tbody>
    			</table>
    		</div>
    	</div>
    <?php
    	include ('footer.php');
    	}else{		
    		//il  n'existe pas de connexion, on revoie la page erreur
    		header('location:erreur.php');
    	}
    ?>

    1. Modérateur

      Tu ne vois pas l'erreur grâce au des bugs que je t'ai ajouté ??

      Le nom de la variable que tu transmets dans ton formulaire ne semble pas être celle que tu essayes de récupérer dans ton script

  2. Modérateur
    <?php
    //fichier adherent.php
    //------------------------------//
    
    //affichage des erreurs PHP
    // voir : https://forums.commentcamarche.net/forum/affich-37584947-php-gestion-des-erreurs-debogage-et-ecriture-du-code
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    
    echo "Juste le temps des tests :<br>";
    var_dump($_POST);
    
    //Traitement des formulaires
    if (isset($_POST['supp'])) {
      $reqdel = "UPDATE adherent SET actif = :actif WHERE id = :id";
      $id = $_POST['id'];
      $actif = 0;
      $data = array(':id' => $id, ':actif' => $actif);
      try {
        $requet = $pdo->prepare($reqdel);
        $requet->execute($data);
      } catch (Exception $e) {
        echo " Erreur ! " . $e->getMessage();
        echo " Les datas : ";
        print_r($data);
      }
    }
    
    if (isset($_POST['change'])) {
      $reqchange = "UPDATE adherent SET actif = :actif WHERE id = :id";
      $id = $_POST['id'];
      $actif = 1;
      $datas = array(':id' => $id, ':actif' => $actif);
      try {
        $reque = $pdo->prepare($reqchange);
        $reque->execute($datas);
      } catch (Exception $e) {
        echo " Erreur ! " . $e->getMessage();
        echo " Les datas : ";
        print_r($datas);
      }
    }
    
    //Requete pour liste les adhérents
    $sql = "SELECT id,nom,prenom,actif FROM adherent WHERE nom LIKE '" . $nom_adh_r . "%'  ORDER BY nom";
    
    try {
      $requete = $pdo->prepare($sql);
      $requete->execute();
    } catch (Exception $e) {
      echo " Erreur ! " . $e->getMessage();
      echo " Les datas : ";
      print_r($data);
    }
    ?>
    <div class="row">
      <div class="col-md-8">
        <?php
        echo "<form action='adherent.php' method='post'>";
        if (isset($_POST['rechercher'])) {
          $nom_adh_r = $_POST['nom_adh_r'];
        } else {
          $nom_adh_r = '';
        }
        echo "<table border='0'>";
        echo "<tr>";
        echo "<td>Nom :</td>";
        echo "<td><input type='text' name='nom_adh_r' size='20' value=\"" . $nom_adh_r . "\" /></td>";
        echo "<td><input class='button_v' type='submit' name='rechercher' size='20' value='Rechercher'></td>";
        echo "</tr>";
        echo "</table>";
        echo "</form>";
        ?>
        <hr>
        <a href="gestion_adherent.php"><img src="img/adds.png" /></a>  Ajouter un adhérent
        <hr>
    
    
        <div align='left'><b>Liste des adhérents</b></div>
        <br />
        <table class="table table-hover">
          <thead>
            <tr>
              <th scope="col">Numéro</th>
              <th scope="col">Nom</th>
              <th scope="col">Prénom</th>
              <th scope="col" class="center">Modifier</th>
              <th scope="col" class="center">Supprimer</th>
            </tr>
          </thead>
          <?php
          $id = 0;
          $num = 0;
    
          echo '<tbody>';
          while ($var = $requete->fetch()) {
            $nom = stripcslashes($var['nom']);
            $prenom = prenom_double($var['prenom']);
            if ($var['actif'] == '0') {
              ?>
              <tr style="background-color:red;">
                <th scope="row"><?php echo (++$num); ?></th>
                <td><?php echo $nom; ?></td>
                <td><?php echo $prenom; ?></td>
                <td class="center">
                  <img src="img/b_drop.png" />
                </td>
                <td class="center">
                  <form action='' method='post'>
                    <input type="image" name="change" src="img/b_edit.png" value="changer" onclick="if (!confirm('Etes-vous sûr de vouloir reactiver <?php echo $prenom . ' ' . $nom ?>'))
                          return false;" />
                    <input type="hidden" name="id" value="<?php echo $var['id']; ?>">
                  </form>
                </td>
              </tr>
              <?php
            } else {
              ?>
              <tr>
                <th scope="row"><?php echo (++$num); ?></th>
                <td><?php echo $nom; ?></td>
                <td><?php echo $prenom; ?></td>
                <td class="center">
                  <a href="gestion_adherent.php?id=<?php echo $var['id']; ?>&action=edit"><img src="img/b_edit.png" /></a>
                </td>
                <td class="center">
                  <form action='' method='post'>
                    <input type="image" name="supp" src="img/b_drop.png" value="Supprimer" onclick="if (!confirm('Etes-vous sûr de vouloir supprimer <?php echo $prenom . ' ' . $nom ?>'))
                          return false;" />
                    <input type="hidden" name="id" value="<?php echo $var['id']; ?>">
                  </form>
                </td>
              </tr>
              <?php
            }
          }
          ?>
          </tbody>
        </table>
      </div>
    </div>

    1. Bonjour j'ai fait comme ça, ça ne fait rien

      <div class="row">
      		<div class="col-md-8">	
      			<?php
      			echo "<form action='adherent.php' method='post'>";
      				if(isset($_POST['rechercher']))	{		
      					$nom_adh_r = $_POST['nom_adh_r'];			
      				}else{
      					$nom_adh_r = '';
      				}				
      					echo "<table border='0'>";
      					echo "<tr>";
      					echo "<td>Nom :</td>";
      					echo "<td><input type='text' name='nom_adh_r' size='20' value=\"".$nom_adh_r."\" /></td>";
      					echo "<td><input class='button_v' type='submit' name='rechercher' size='20' value='Rechercher'></td>";
      					echo "</tr>";
      					echo "</table>";
      			echo "</form>";	
      			?>
      			<hr>
      			<a href="gestion_adherent.php"><img src="img/adds.png" /></a>  Ajouter un adhérent
      			<hr>
      			
      			
      			<div align='left'><b>Liste des adhérents</b></div>		
      			<br />
      			<table class="table table-hover">
      			<thead>
      				<tr>
      				<th scope="col">Numéro</th>
      				<th scope="col">Nom</th>
      				<th scope="col">Prénom</th>
      				<th scope="col" class="center">Modifier</th>
      				<th scope="col" class="center">Supprimer</th>
      				</tr>
      			</thead>
      			<?php
      			//la requet			
      			$sql = 'SELECT id,nom,prenom,actif FROM adherent WHERE nom LIKE \''.$nom_adh_r.'%\'  ORDER BY nom';	
      			
      			try{
      				$requete = $pdo->prepare($sql);
      				$requete->execute();
      				$id = 0;
      				$num = 0;
      
      				echo '<tbody>';
      				while($var = $requete->fetch()){	
      					$nom = stripcslashes($var['nom']);
      					$prenom = prenom_double($var['prenom']);
      					if($var['actif'] == '0'){
      						?>
      						<tr style="background-color:red;">
      							<th scope="row"><?php echo (++$num); ?></th>
      							<td><?php echo $nom; ?></td>
      							<td><?php echo $prenom; ?></td>
      							<td class="center">
      							<img src="img/b_drop.png" />
      							</td>
      							<td class="center">
      							<form action='adherent.php' method='post'>
      								<input type="image" name="change" src="img/b_edit.png" value="changer" onclick="if(!confirm('Etes-vous sûr de vouloir reactiver <?php echo $prenom.' '.$nom ?>')) return false;" />
      								<input type="hidden" name="id" value="<?php echo $var['id']; ?>">			
      							</form>
      							</td>
      						</tr>
      						<?php
      					}else{
      						?>
      						<tr>
      						<th scope="row"><?php echo (++$num); ?></th>
      							<td><?php echo $nom; ?></td>
      							<td><?php echo $prenom; ?></td>
      							<td class="center">
      							<a href="gestion_adherent.php?id=<?php echo $var['id']; ?>&action=edit"><img src="img/b_edit.png" /></a>
      							</td>
      							<td class="center">
      							<form action='adherent.php' method='post'>
      								<input type="image" name="supp" src="img/b_drop.png" value="Supprimer" onclick="if(!confirm('Etes-vous sûr de vouloir supprimer <?php echo $prenom.' '.$nom ?>')) return false;" />
      								<input type="hidden" name="id" value="<?php echo $var['id']; ?>">	
      							</form>	
      							</td>
      						</tr>
      						<?php
      					}
      				}
      			}catch(Exception $e){
      				echo " Erreur ! ".$e->getMessage();
      				echo " Les datas : " ;
      				print_r($data);
      			}     
      			?>
      			</tbody>
      			</table>
      			<?php
      				if(isset($_POST['supp'])){
      					$reqdel = "UPDATE adherent SET actif = :actif WHERE id = :id";
      					$id = $_POST['id'];
      					$actif = 0;		
      					$data = array(':id'=>$id, ':actif'=>$actif);
      					try{
      						$requet = $pdo->prepare($reqdel);
      						$requet->execute($data);
      					}catch(Exception $e){
      						echo " Erreur ! ".$e->getMessage();
      						echo " Les datas : " ;
      						print_r($data);
      					}	
      				}
      
      				if(isset($_POST['change'])){
      					$reqchange = "UPDATE adherent SET actif = :actif WHERE id = :id";
      					$id = $_POST['id'];
      					$actif = 1;	
      					$datas = array(':id'=>$id, ':actif'=>$actif);	
      					try{
      						$reque = $pdo->prepare($reqchange);
      						$reque->execute($datas);
      					}catch(Exception $e){
      						echo " Erreur ! ".$e->getMessage();
      						echo " Les datas : " ;
      						print_r($datas);
      					}		
      				}
      			?>
      		</div>
      	</div>

      Merci d'avance pour ton aide


      1. Ok , je regardera ça mais pas avant lundi. Je te tiendrai au courant.

        Merci

        Bon WE


        1. Modérateur

          Donc ton formulaire n'est pas envoyé.. ou alors tu l'as mal placé.

          Où l'as tu mis ?

          Avant le  if(isset($_POST['supp'])){   j'espère...

          A noter également que tu devrais placer le form dans le td ..et non autour

          Il faut aussi éviter d'ouvrir/fermer des balises php inutilement..

          et le  multipart/form-data n'est utile que pour envoyer des fichiers sur le serveur.. ce qui n'est pas le cas ici

          En gros, ton code devrait ressembler simplement à ça :

          <td class="center">
             <form action='adherent.php' method='post'>
          	<input type="image" name="supp" src="img/b_drop.png" value="Supprimer" onclick="if(!confirm('Etes-vous sûr de vouloir supprimer <?php echo $prenom.' '.$nom ?>')) return false;" />
          	<input type="hidden" name="id" value="<?php echo $var['id']; ?>" />			
            </form>
          </td>
          
          1. Modérateur

            Bonjour,

            Ajoute un var_dump de $_POST au début de ton script et montre nous ce qu'il contient.

            Y retrouves tu tes variable sup ou change ?