1

I am trying to get my database update after taking input from a form. It won't work, what do I do?

Issue:

  • Input into forum after submitting won't change/update the database

What I've tried:

  • Trying to just echo out every and each statement seperately
  • Make seperate queries for each different variable (input variable)

Current code:

<?php
$user = 'root';
$pass = '';
try {
    $connection = new PDO('mysql:host=localhost;dbname=netland', $user, $pass);
    foreach ($connection->query('select database(), version()') as $row) {
    }   
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

$array_id = $_GET["id"];

$movies = $connection->prepare("SELECT * FROM films WHERE id = ?");
$movies->execute([$array_id]);
$movies_array = $movies->fetch();

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Netland - Edit: Films</title>
    </head>
    <body>
        <a href="index.php">
            Terug
        </a>
        <h1>
            <?= $movies_array['titel'] . " - Rating: " . $movies_array['duur']; ?>
        </h1>
        <form method="POST" autocomplete="off" action="<?php $_PHP_SELF ?>">
            <table>
                <tr>
                    <td>
                        <label><b>Titel</b></label>
                    </td>
                    <td>
                        <input type="text" id="titel" name="titel" value="<?= $movies_array['titel'] ?>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label><b>Duur</b></label>
                    </td>
                    <td>
                        <input type="number" id="duur" name="duur" value="<?= $movies_array['duur'] ?>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label><b>Datum van uitkomst</b></label>
                    </td>
                    <td>
                        <input type="date" id="datum_van_uitkomst" name="datum_van_uitkomst" value="<?= $movies_array['datum_van_uitkomst'] ?>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label><b>Land van uitkomst</b></label>
                    </td>
                    <td>
                        <input type="text" id="land_van_uitkomst" name="land_van_uitkomst" value="<?= $movies_array['land_van_uitkomst'] ?>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label><b>Omschrijving</b></label>
                    </td>   
                    <td>
                        <textarea id="omschrijving" name="omschrijving" rows="10" cols="40"
                        ><?= $movies_array['omschrijving'] ?></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label><b>YouTube trailer id</b></label>
                    </td>
                    <td>
                        <input type="text" id="link" name="link" value="<?= $movies_array['link'] ?>">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" id="wijzig" name="wijzig" value="Wijzig">
                    </td>
                </tr>
            </table>
        </form>
        <?php
            if (isset($_POST['wijzig'])) {
                $titel = $_POST['titel'];
                $duur = $_POST['duur'];
                $datum_van_uitkomst = $_POST['datum_van_uitkomst'];
                $land_uit_uitkomst = $_POST['land_van_uitkomst'];
                $omschrijving = $_POST['omschrijving'];
                $link = $_POST['link'];
                $stmt = $connection->prepare('UPDATE films SET title = ?, duur = ?, datum_van_uitkomst = ?, land_van_uitkomst = ?, omschrijving = ?, link = ? WHERE id = ?');
                $stmt->execute([$titel, $duur, $datum_van_uitkomst, $land_uit_uitkomst, $omschrijving, $link, $array_id]);
                $updated_array = $stmt->fetchAll();
            }
        ?>
    </body>
</html>

Thank you for helping in advance.

Lar
  • 65
  • 8
  • 1
    Please turn on error reporting (https://stackoverflow.com/a/21429652/296555) and try setting your connection as follows: https://phpdelusions.net/pdo#dsn. Report back any errors you find. Importantly, `PDO::ATTR_ERRMODE` will turn MySQL errors into PHP exceptions. The last line, `$updated_array = $stmt->fetchAll();`, is not needed. – waterloomatt Sep 12 '21 at 17:56
  • 1
    Also, can you confirm if the issue is the database is not updated, or if the database is updated and just not reflected in your web app? If the latter, just move our update statement above the query at the top. – waterloomatt Sep 12 '21 at 17:57
  • 1
    Why the `fetchAll` on an `UPDATE` query? No records are returned. – Paul T. Sep 12 '21 at 18:42
  • 1
    You are using the `id` for both select query and update query, but your form is using POST. Hence, (1) change the $_GET["id"] to $_REQUEST["id"] so that it serves both GET and POST. (2) Please add a hidden input field to store the $_REQUEST["id"] in your form, so that when your click the "submit" button, it will get resubmitted as POST ; – Ken Lee Sep 12 '21 at 19:22
  • 1
    thank you @KenLee, Paul T. and waterloomatt, I see where I went wrong now! I fixed it, you guys were all right, I removed a query and fixed the ID. – Lar Sep 14 '21 at 08:55

0 Answers0