簡體   English   中英

使用PHP / PDO和mySQL更新數據庫記錄

[英]Updating database record with PHP/PDO and mySQL

我有一個數據庫“ Telefoon”和一個表“ Telefoonnummers”。 該表具有“ naam”,“ number”和“ mobiel”列

我正在嘗試制作一個.php頁面,我可以在其中更新記錄。 我很確定這是正確的,但是無論如何都不會改變。

在這里,您可以填寫更新的記錄,然后單擊按鈕,將轉到下一個對其進行更新的頁面。

<!DOCTYPE HMTL>
<html>

<head>
    <title>Wijzigen telefoonnummer</title>
</head>

<body>

    <?php

        $record_name = $_GET["naam"];

        $user = "root";
        $pass = "root";

        $dbh = new PDO(
            'mysql:host=localhost; port=8473; dbname=telefoon',
            $user,
            $pass
        );

        $sth = $dbh -> prepare("

            SELECT *
            FROM Telefoonnummers
            WHERE naam = :record_name

        ");

        $sth -> bindValue( ":record_name", $record_name, PDO::PARAM_STR );

        $sth -> execute();

        $printRecord = $sth -> fetchAll(PDO::FETCH_ASSOC);

        /*
        //dit record als array weergeven
        print("<pre>");
        print_r($printRecord);
        print("</pre>");
        */

        //gegevens in variabelen zetten
        $printRecordRecord = $printRecord[0];
        $huidigeNaam = $printRecordRecord["naam"];
        $huidigeNummer = $printRecordRecord["telefoonnummer"];
        $huidigeMobiel = $printRecordRecord["mobiel"];

        //niet meer nodig door bovenstaande
        /*
        foreach( $printRecord AS $printRecordIndex => $printRecordRecord ) {

            $huidigeNaam = $printRecordRecord["naam"];
            $huidigeNummer = $printRecordRecord["telefoonnummer"];
            $huidigeMobiel = $printRecordRecord["mobiel"];

        }
        */

        print("

            <form action='wijzig.php' method='POST'>
                <table>

                    <tr>
                        <td bgcolor='green'><b>Naam</b></td>
                        <td bgcolor='green'><b>Telefoonnummer</b></td>
                        <td bgcolor='green'><b>Mobiel</b></td>
                        <td bgcolor='green'></td>
                    </tr>

                    <tr>
                        <td><input type='text' name='naam' value='$huidigeNaam' disabled='TRUE' /></td>
                        <td><input type='text' name='nummer' value='$huidigeNummer' /></td>
                        <td><input type='text' name='mobiel' value='$huidigeMobiel' /></td>
                        <td><input type='submit' value='Wijzig' /></td>
                    </tr>

                </table>
            </form>

        ");

    ?>

</body>

這是下一個實際更改(至少是我想要的)記錄的頁面:

<!DOCTYPE HTML>
<html>

<head>
    <title>Gewijzigd</title>
</head>

<body>

    <?php

        //geupdate gegevens ophalen
        $newNaam = $_POST["naam"];
        $newNumber = $_POST["nummer"];
        $newMobile = $_POST["mobiel"];

        //gegevens updaten als ALLES is ingevuld
        if ( ($newNaam != "") && ($newNumber != "") && ($newMobile != "") ) {

            $user = "root";
            $pass = "root";

            $dhb = new PDO(
                'mysql:host=localhost; port=8473; dbname=telefoon',
                $user,
                $pass
            );

            $sth = $dbh -> prepare("

                UPDATE Telefoonnummers
                SET naam = :naam,
                telefoonnummer = :nummer,
                mobiel = :mobiel
                WHERE naam = :naam

            ");

            $sth -> bindValue( ":naam", $newNaam, PDO::PARAM_STR );
            $sth -> bindValue( ":telefoonnummer", $newNumber, PDO::PARAM_STR );
            $sth -> bindValue( ":mobiel", $newMobile, PDO::PARAM_STR );

            $sth -> execute();

            $sthCheck = $dbh -> prepare("

                SELECT *
                FROM Telefoonnummers
                WHERE naam = :naam

            ");

            $sthCheck -> bindValue( ":naam", $newNaam, PDO::PARAM_STR );

            $sthCheck -> execute();

        }

    ?>

</body>

這有什么問題?

我使用您的代碼設置了快速測試頁。 我發現有兩件事不能正常工作。

  1. 初始表單 - 表單中的“ naam”字段已禁用。 這意味着該參數不會進入Wijzig.php頁面。 由於“ naam”始終為null,因此等於“”,因此它不會超出if語句。 為了使其正常工作,我在第一種形式中重新啟用了“ naam”字段。
  2. UPDATE語句中的參數 -在您的UPDATE語句中,它嘗試訪問名為“:telefoonnummer”的參數,但在參數中使用了“:nummer”。 這將導致PDO在執行時引發異常。

快速說明 -我在wijzig.php中添加了echo語句,以便成功運行將產生某種可見的結果。

這樣,我將兩個文件更新為:

的index.php

<head>
<title>Wijzigen telefoonnummer</title>
</head>

<body>

<?php

    $record_name = $_GET["naam"];

    $user = "root";
    $pass = "root";

    $dbh = new PDO(
        'mysql:host=localhost; port=8473; dbname=telefoon',
        $user,
        $pass
    );

    echo $record_name;

    $sth = $dbh -> prepare("

        SELECT *
        FROM Telefoonnummers
        WHERE naam = :record_name

    ");

    $sth -> bindValue( ":record_name", $record_name, PDO::PARAM_STR );

    $sth -> execute();

    $printRecord = $sth -> fetchAll(PDO::FETCH_ASSOC);

    /*
    //dit record als array weergeven
    print("<pre>");
    print_r($printRecord);
    print("</pre>");
    */

    //gegevens in variabelen zetten
    $printRecordRecord = $printRecord[0];
    $huidigeNaam = $printRecordRecord["naam"];
    $huidigeNummer = $printRecordRecord["telefoonnummer"];
    $huidigeMobiel = $printRecordRecord["mobiel"];

    //niet meer nodig door bovenstaande
    /*
    foreach( $printRecord AS $printRecordIndex => $printRecordRecord ) {

        $huidigeNaam = $printRecordRecord["naam"];
        $huidigeNummer = $printRecordRecord["telefoonnummer"];
        $huidigeMobiel = $printRecordRecord["mobiel"];

    }
    */

    print("

        <form action='wijzig.php' method='POST'>
            <table>

                <tr>
                    <td bgcolor='green'><b>Naam</b></td>
                    <td bgcolor='green'><b>Telefoonnummer</b></td>
                    <td bgcolor='green'><b>Mobiel</b></td>
                    <td bgcolor='green'></td>
                </tr>

                <tr>
                    <td><input type='text' name='naam' value='$huidigeNaam'/></td>
                    <td><input type='text' name='nummer' value='$huidigeNummer' /></td>
                    <td><input type='text' name='mobiel' value='$huidigeMobiel' /></td>
                    <td><input type='submit' value='Wijzig' /></td>
                </tr>

            </table>
        </form>

    ");

?>

</body>

wijzig.php

<head>
<title>Gewijzigd</title>
</head>

<body>

<?php

    //geupdate gegevens ophalen
    $newNaam = $_POST["naam"];
    $newNumber = $_POST["nummer"];
    $newMobile = $_POST["mobiel"];

    //gegevens updaten als ALLES is ingevuld
    if ( ($newNaam != "") && ($newNumber != "") && ($newMobile != "") ) {

        $user = "root";
        $pass = "root";

        $dbh = new PDO(
        'mysql:host=localhost; port=8473; dbname=telefoon',
        $user,
        $pass
    );

        $sth = $dbh -> prepare("

            UPDATE Telefoonnummers
            SET naam = :naam,
            telefoonnummer = :nummer,
            mobiel = :mobiel
            WHERE naam = :naam

        ");

        $sth -> bindValue( ":naam", $newNaam, PDO::PARAM_STR );
        $sth -> bindValue( ":nummer", $newNumber, PDO::PARAM_STR );
        $sth -> bindValue( ":mobiel", $newMobile, PDO::PARAM_STR );

        $sth -> execute();

        $sthCheck = $dbh -> prepare("

            SELECT *
            FROM Telefoonnummers
            WHERE naam = :naam

        ");

        $sthCheck -> bindValue( ":naam", $newNaam, PDO::PARAM_STR );

        echo "Number of records changed: ".$sthCheck -> execute();

    }

?>

</body>

對現有記錄運行時,將產生以下輸出:

記錄數更改:1

暫無
暫無

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

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