Sert和更新问题PDO PHP MySQL

我是一个新的学生,我一直在寻找这个问题的解决方案。发现了各种各样的答案,但我似乎没有找到我的确切问题。

我正在为您的任务的一部分创建足球协会的管理页面。一切顺利,直到我试图更新分数(我重新开始零)。我获得了成功的消息,但DB未更新。

So I tried inserting the score instead, as part of a new fixture, everything else updated but the scores. So I know it’s the scores the problem but I don’t know why. I made them NOT NULL and then NULL to see if that was the problem, to no avail.

我最初填充了一些分数的灯具,但在“更新”之后,所有这些都在按照上面的更改时变​​为零或空。

What am I doing wrong?

       if (isset($_POST["Update"])) {
       updateScore($_POST["FixtureID"]);
    }
    function updateScore($FixtureID) {
       try {
          $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0912343_Fixtures", GLOBALS['username'], $GLOBALS['password']);
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
          $statement = $conn->prepare("
    UPDATE Fixtures f
         , Scores s 
       SET f.WeekNo = :WeekNo
         , f.Fixture_Date = :Fixture_Date
         , f.HomeTeam = :HomeTeam
         , f.AwayTeam = :AwayTeam
         , s.HT_score = :HT_score
         , s.AT_score = :AT_score 
     WHERE f.FixtureID =" . $FixtureID);
    
             
          $statement->bindValue(":WeekNo", $_POST["WeekNo"]);
          
          $statement->bindValue(":Fixture_Date", $_POST["Fixture_Date"]);
          
          $statement->bindValue(":HomeTeam", $_POST["HomeTeam"]);
          
          $statement->bindValue(":AwayTeam", $_POST["AwayTeam"]);
          
          $statement->bindValue(":HT_score", $_POST["HT_score"]);
          
          $statement->bindValue(":AT_score", $_POST["AT_score"]);
      
    
          $result = $statement->execute();
          
          
    
            if ($result) {
         
                $GLOBALS['message'] = "The score was updated successfully!";
          } 
          
          else {
             
              $GLOBALS['message'] = "The score was not updated!";
              
                    }
       }
       catch(PDOException $e) {
          echo "We are sorry. A problem occured: " . $e->getMessage();
       }
    
       $conn = null;
    }
    ```
    
    if (isset($_POST["Insert"])) {insertFixture();
    
    }
    
    function insertFixture() {
        try {
            $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0912343_Fixtures", $GLOBALS['username'], $GLOBALS['password']);
            
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            $statement = $conn->prepare("
    INSERT INTO Fixtures (FixtureID, WeekNo, Fixture_Date, HomeTeam, AwayTeam) VALUES (:FixtureID, :WeekNo, :Fixture_Date, :HomeTeam, :AwayTeam);
           
            INSERT INTO Scores (HT_score, AT_score) VALUES (:HT_score,:AT_score)");
    
            $statement->bindValue(":FixtureID", $_POST["FixtureID"]);
           
            $statement->bindValue(":WeekNo", $_POST["WeekNo"]);
            
            $statement->bindValue(":Fixture_Date", $_POST["Fixture_Date"]);
            
            $statement->bindValue(":HomeTeam", $_POST["HomeTeam"]);
           
            $statement->bindValue(":AwayTeam", $_POST["AwayTeam"]);
            
            $statement->bindValue(":HT_score", $_POST["HT_score"]);
            
            $statement->bindValue(":AT_score", $_POST["AT_score"]);
    
            $result = $statement->execute();
            
            if ($result) {
                
                $GLOBALS['message'] = "The fixture was inserted successfully!";           
            
            } else {  
            
                $GLOBALS['message'] = "The fixture was not inserted!";
            }
        }
        catch(PDOException $e) {
           
            echo "We are sorry. A problem occurred: " . $e->getMessage();
        }
        
        $conn = null;
    }
    ```
     ```    
    <header>
    <table>
    <tr>
          <td><h1>Football Association Admin Page</h1></td>
     </tr>
    </table>
    </header>
    
    <section>
       <h1>Welcome to the Football Association Admin Page</h1>
       <h2>Fixtures Maintenance</h2>
       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
       <table>  
     
     <tr>
    <td>Fixture ID: </td>
          <td><input type="text" name="FixtureID" value="<?php echo $fixtureid;?>"></td>
          <td><input type="submit" name="Get" value="Get"></td>  
     </tr>  
      
    <tr>        
       <td> Week No:</td>          
       <td><input type="text" name="WeekNo" value="<?php echo $weekNo;?>"></td>
       <td><input type="submit" name="New" value="New"></td> 
     </tr>
       
       <tr>
          <td>Fixture Date:</td>
          <td><input type="text" name="Fixture_Date" value="<?php echo $fixture_date;?>"></td>
            <td><input type="submit" name="Update" value="Update"></td> 
       </tr>
            
        <tr>
          
        <tr> 
         <td>Home Team:</td>
          <td><input type="text" name="HomeTeam" value="<?php echo $homeTeam;?>"></td>
          
          
        </tr>  
       
        <tr>     
           <td>Home Team Score:</td>
           <td><input type="text" name="Home_Team_Score" value="<?php echo $htscore;?>"></td> 
           <td><input type="submit" name="Insert" value ="Insert"></td>  
            </tr>            
        <tr>
           <td>Away Team:</td>
           <td><input type="text" name="AwayTeam" value="<?php echo $awayTeam;?>"></td>          
        </tr>
           
        <tr>     
           <td>Away Team Score:</td>
           <td><input type="text" name="Away_Team_Score" value="<?php echo $atscore;?>"></td>   
            </tr> 
         
         <tr>
          <td colspan=3><?php echo $message; ?></td>   
       </tr>
    
    </table>
    
       </form>
       
    </section>
    </body>
    </html>
     ```
加入表时,通常表示一张表与另一个表涉及另一个表的方式 - 除非目标是笛卡尔产品Strawberry
李草莓,你介意详细阐述“它常见的是表明一张表涉及另一个表的方式”,请?在评论或我呈现代码的方式中是均值? - 对不起,我有点困惑。
你的更新说UPDATE Fixtures f [JOIN] Scores s but you don't state the criteria for relating scores to fixtures, so 所有Scores加入所有fixtures(对应于$ fixtureid)Strawberry
谢谢你的草莓,便士掉了!一旦我实际上得到了明确的标准(这是闪烁的),就像应该更新的分数!遗憾的是,我无法与你分享正确的答案,而是在你和kiko之间,我不仅让它变成了,但我明白了 - 这让我疯了!再次感谢你!Nessy

回答 1

  1. 赞同 0

    在您使用的表格中Home_Team_ScoreAway_Team_Score但在发布这些变为后$_POST["HT_score"]$_POST["AT_score"].

    These are clearly not the same, and therefore it cannot work.

    Kiko软件
    谢谢kiko,我误解了这个的重要性!我改变了它,现在可以看到分数,但它填充了相同分数的所有灯具!我认为它只会影响我选择的ID?nessy
    @ nessy,您不使用分数表中的特定行WHERECondition,所以所有的行都可以设置。Kiko软件
    谢谢你这么多让我在正确的道路上!nessy