Home > Enterprise >  Inserting record into SQLite using PHP
Inserting record into SQLite using PHP

Time:11-05

For the past two hours, I have been trying to create a simple insertion form that connects to a SQLite. For some reason, the insertion of a new record won't work. I get no error message when I run my app using php -S localhost:1234. My form is just emptied out without any insertion after a click on the Submit button.

My database is named database.db, the table is named students_tb, and the columns in the table are id, sname and score.

Here is my code, which is based on https://www.youtube.com/watch?v=cyl0Oj3rmmg&list=PLU70qqWW4frENsWYAm-tAKp2ZJQ_dt3WR&index=8. I checked and rechecked the 3-minute-long tutorial, but wasn't successful at tracking down my bug. I guess this must be a silly mistake, but I really can't find it.

<!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>Add Student</title>
    <style>
        label, input {
            display: block;
        }
    </style>
</head>
<body>
    <h1>Add student to database</h1>
    
    <?php
        // has the form been submitted?
        // if not, show the HTML form
        if (!isset($_POST['submit'])) {
    ?>

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

        <label for="sname">Student's Name</label>
        <input type="text" name="sname" required>

        <label for="score">Score</label>
        <input type="number" name="score" required>

        <button type="submit">Submit</button>

    </form>

    <?php
        } else {
            try {
                $db = new PDO("sqlite:database.db");
                $sql = "INSERT INTO students_tb (sname, score) VALUES (:sname, :score)";
                $stat = $db->prepare($sql);

                // named params

                $sname = filter_input(INPUT_POST, "sname");
                $stat->bindValue(":sname", $sname, PDO::PARAM_STR);

                $score = filter_input(INPUT_POST, "score");
                $stat->bindValue(":score", $score, PDO::PARAM_INT);
                
                $success = $stat->execute();

                // does the value exist?
                if ($success) {
                    echo "The student has been added to the database.";
                } else {
                    echo "The student has NOT been added to the database.";
                }

                $db = null;

            } catch (PDOException $e) {
                // for development
                print "We had an error: " . $e->getMessage() . "<br>";
                die();
            }

        }
    ?>
    

</body>
</html>

CodePudding user response:

It has been a while since I worked in PHP, but I think the problem might be in the HTML code of the form. You have:

<button type="submit">Submit</button>

And your PHP code is checking for a value of the variable named submit, but this field does not have a name, only a type. Should it be:

<button type="submit" name="submit">Submit</button>
  • Related