简体   繁体   中英

Problems updating an sql database with php

I'm using php to update an sql database. Here I have a button named "valider2"

input type="submit" value="Valider" name="valider2">

When the user clicks on it, it is submitted and should process the update request, which works when it is directly used in the database but doesn't when used with this php code. No error is returned, but nothing changes, any idea why? I suspect that the button doesn't send a proper signal to execute the command, but I don't know why it wouldn't work.

if (isset($_POST['valider2'])) {

          $req2 = $linkpdo->prepare('UPDATE Joueur SET Prenom = :Prenom, Nom = :Nom, Taille = :Taille, Poids = :Poids, PostePrefere = :PostePrefere, Notes = :Notes, Statut = :Statut WHERE NumLicence = :NumLicence');

          $req2->execute(array('Prenom' => $Prenom, 'Nom' => $Nom, 'Taille' => $Taille, 'Poids'=> $Poids, 'PostePrefere' => $PostePrefere, 'Notes' => $Notes, 'Statut' => $Statut, 'NumLicence' => $NumLicence));
      }

Thank you in advance for your help, here is the integral code of the page, not perfect but here I'm only focusing on getting this request to work. Sorry for the amount of words in French, but they shouldn't prevent you from understanding how the code works.

<!doctype HTML>
    <!-- http://r-info-nacre/~mnm2788a/Site_PHP/index.php -->
    <html lang="fr" class="basic">
    <head>
        <?php header('Content-Type: text/html; charset=utf-8'); ?>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="index.css"/>

    <title>Deutscher Fussball-Bund</title>

    <script
        src="https://code.jquery.com/jquery-3.3.1.js">
    </script>
    <script> 
    $(function(){
      $("#header").load("header.html"); 
      $("#footer").load("footer.html"); 
    });
    </script> 

    </head>

    <body>
        <div id="header"></div>
    <center>
        <div class="buttons">
            <ul>
              <li><a href="./index.php">Accueil</a></li>
              <li><a href="./AjouterJoueur.php">Ajouter un joueur</a></li>
              <li><a href="./AjouterMatch.php">Ajouter un match</a></li>
              <li><a href="./Affecter.php">Affecter des joueurs</a></li>
              <li><a class = "active" href="./ModifierJoueur.php">Modifier un joueur</a></li>
              <li><a href="./ModifierMatch.php">Modifier un match</a></li>
              <li><a href="./SupprimerJoueur.php">Supprimer un joueur</a></li>
              <li><a href="./SupprimerMatch.php">Supprimer un match</a></li>
            </ul>
        </div>
    </center>
    <center>
    <div class="images_index">
            <img class="container" src="Images/Deutscher.png" width="45%" height="45%">
    </div>
</center>
    <div id="footer"></div>

<center>

  <!-- Formulaire pour saisir le numlicence du joueur à modifier -->

  <form class = "form" method="post" action="ModifierJoueur.php">

    <p class = "p_margin_bottom">Entrez le numéro de licence du joueur à modifier</p>
    <center>
        <label><input type="text" name="NumLicence" class="input_form" placeholder="Numéro de Licence"><br></label>
        <input type="reset" value="Remise à zero" name = "raz">
        <input type="submit" value="Valider" name="valider">
    </center>
    </form>

    <!-- Formulaire pour modifier les champs -->


</center>

<div class="php">
    <?php

    session_start();
        if ($_SESSION['id'] != "ReinhardGrindel" && $_SESSION['pwd'] != "RG") {
                header('Location: identification.php');
        }

        $login = "HiddenForObviousReasons";
        $mdp = "HiddenForObviousReasons";
        $server = "HiddenForObviousReasons";
        $db = "HiddenForObviousReasons";

        // CONNEXION
        try {
            $linkpdo = new PDO("mysql:host=$server;dbname=$db", $login, $mdp);
        }
        catch (Exception $e) {
            echo "<br>Connexion à la base de données : Echec.";
            die('Erreror: '. $e -> getMessage());
        }
        $link = mysqli_connect($server, $login, $mdp, $db) or die("Error ".mysqli_error($link)); 
      if (mysqli_connect_errno()) {
          print("Connect failed: \n". mysqli_connect_error()); 
          exit(); 
      }

      // APPUI DU BOUTON DE SELECTION DE L'ID MATCH
    if (isset($_POST['valider']) && $_POST['NumLicence'] != "") {

      $NumLicence = $_POST['NumLicence'];

      // On sélectionne les valeurs déjà placées pour le joueur
      $req1 = "SELECT * FROM Joueur WHERE NumLicence = $NumLicence"; 
            ///Execution de la requête sur le serveur
            if(!$resquery=mysqli_query($link,$req1) ){
                die("Error:".mysqli_errno($link).":".mysqli_error($link)); 
            } else {
              $row = mysqli_fetch_assoc($resquery);
              $Prenom = $row['Prenom'];
              $Nom = $row['Nom'];
              $Taille = $row['Taille'];
              $Poids = $row['Poids'];
              $PostePrefere = $row['PostePrefere'];
              $Notes = $row['Notes'];
              $Statut = $row['Statut'];
            }

      ?>
      <center>
       <form class = "form" method="post">
    <p class = "p_margin_bottom">Veuillez entrer les modifications correspondantes au joueur</p>
    <center>
        <label><input value="<?php echo $Prenom; ?>" type="text" name="Prenom" class="input_form"><br></label> 
        <label><input value="<?php echo $Nom; ?>" type="text" name="Nom" class="input_form"><br></label> 
        <label><input value="<?php echo $Taille; ?>" type="number" name="Taille" class="input_form"><br></label>
        <label><input value="<?php echo $Poids; ?>" type="number" name="Poids" class="input_form"><br></label>
        <label><input value="<?php echo $PostePrefere; ?>" type="text" name="PostePrefere" class="input_form"><br></label>
        <label><input value="<?php echo $Notes; ?>" type="text" name="Notes" class="input_form"><br></label>
        <label><input value="<?php echo $Statut; ?>" type="text" name="Statut" class="input_form"><br></label>

        <input type="submit" value="Valider" name="valider2">
    </center>
    </form>
    </center>
      <?php

      if (isset($_POST['valider2'])) {

          $req2 = $linkpdo->prepare('UPDATE Joueur SET Prenom = :Prenom, Nom = :Nom, Taille = :Taille, Poids = :Poids, PostePrefere = :PostePrefere, Notes = :Notes, Statut = :Statut WHERE NumLicence = :NumLicence');

          $req2->execute(array('Prenom' => $Prenom, 'Nom' => $Nom, 'Taille' => $Taille, 'Poids'=> $Poids, 'PostePrefere' => $PostePrefere, 'Notes' => $Notes, 'Statut' => $Statut, 'NumLicence' => $NumLicence));
      }
    }

    ?>

<br><br><br><br><br><br><br><br>

</div>

</body>
</html>

Your code has two form elements.

First form is fired processed starting at line # 93 with the below if statement:

if (isset($_POST['valider']) && $_POST['NumLicence'] != "")

The second form is processed at line # 132 beginning with the if statement as below:

if (isset($_POST['valider2']))

The issue here is, you have put the second if statement inside the first if statement. Since no two forms can be fired at same time by clicking two buttons with different names, the second if statement will never be reached in your code.

So, your MySQL update statement will never be processed. Have a look and update the position of the second if statement. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM