簡體   English   中英

使用php更新sql數據庫時出現問題

[英]Problems updating an sql database with php

我正在使用php更新sql數據庫。 這里有一個名為“ valider2”的按鈕

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

當用戶單擊它時,它將被提交並應處理更新請求,當它直接在數據庫中使用但與該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));
      }

在此先感謝您的幫助,這是頁面的完整代碼,雖然不是完美的代碼,但是在這里,我僅着眼於使此請求生效。 對不起,法語中的單詞數量太多,但是它們不應該阻止您了解代碼的工作方式。

<!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>

您的代碼有兩個表單元素。

從第93行開始,使用以下if語句觸發第一個表單的處理:

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

第二種形式在行#132處以if語句開始處理,如下所示:

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

這里的問題是,您已將第二個if語句放入第一個if語句中。 由於單擊兩個名稱不同的按鈕無法同時觸發兩種形式,因此在您的代碼中永遠不會到達第二個if語句。

因此,您的MySQL更新語句將永遠不會被處理。 看看並更新第二個if語句的位置。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM