简体   繁体   English

使用PHP / PDO和mySQL更新数据库记录

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

I have a database "Telefoon" with a table "Telefoonnummers". 我有一个数据库“ Telefoon”和一个表“ Telefoonnummers”。 The table has the columns "naam", "number" and "mobiel" 该表具有“ naam”,“ number”和“ mobiel”列

I am trying to make a .php page where I can update a record. 我正在尝试制作一个.php页面,我可以在其中更新记录。 I am pretty sure this is right, but somehow nothing changes. 我很确定这是正确的,但是无论如何都不会改变。

Here you fill in the updated records, and when you click the button it goes to the next page which updates it. 在这里,您可以填写更新的记录,然后单击按钮,将转到下一个对其进行更新的页面。

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

This is the next page which actually changes (at least that's what I want) the record: 这是下一个实际更改(至少是我想要的)记录的页面:

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

What is wrong with this? 这有什么问题?

I set up quick test pages using your code. 我使用您的代码设置了快速测试页。 I found two things that weren't working correctly. 我发现有两件事不能正常工作。

  1. The initial form - The "naam" field was disabled in the form. 初始表单 - 表单中的“ naam”字段已禁用。 This means that the parameter doesn't make it to the Wijzig.php page. 这意味着该参数不会进入Wijzig.php页面。 Since "naam" would always be null - and thus equal to "" - it wouldn't make it past your if statement. 由于“ naam”始终为null,因此等于“”,因此它不会超出if语句。 To get this to work, I reenabled the "naam" field in the first form. 为了使其正常工作,我在第一种形式中重新启用了“ naam”字段。
  2. Parameter in the UPDATE statement - In your UPDATE statement, it attempted to access a parameter named ":telefoonnummer" yet in the parameters, ":nummer" was used. UPDATE语句中的参数 -在您的UPDATE语句中,它尝试访问名为“:telefoonnummer”的参数,但在参数中使用了“:nummer”。 This would cause PDO to throw an exception on execution. 这将导致PDO在执行时引发异常。

Quick Note - I added an echo statement to wijzig.php so that a successful run would produce a visible result of some sort. 快速说明 -我在wijzig.php中添加了echo语句,以便成功运行将产生某种可见的结果。

With that, I have updated the two files to be like such: 这样,我将两个文件更新为:

index.php 的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 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>

When run against an existing record, it produces the following output: 对现有记录运行时,将产生以下输出:

Number of records changed: 1 记录数更改:1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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