User Tools

Site Tools


programmieren:html:forms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programmieren:html:forms [2026/08/12 16:03] – validation rules adminprogrammieren:html:forms [2026/09/03 14:56] (current) – Mistakes admin
Line 117: Line 117:
 } }
 </code> </code>
 +
 +==== 3rd Iteration ====
 +
 +In the 3rd Iteration I simply added saving the data put into the form to the Database:
 +
 +<code php>
 +    public function savePostData($post): void
 +    {
 +        $servername = "localhost";
 +        $username = "root";
 +        $password = "";
 +        $dbname = "form_exercise";
 +
 +        $conn = new mysqli($servername, $username, $password, $dbname);
 +
 +        if ($conn->connect_error) {
 +            die("Connection failed: " . $conn->connect_error);
 +        }
 +
 +        $sql = "INSERT INTO entries (name, email, phone, birthday, message) VALUES (?, ?, ?, ?, ?)";
 +
 +        $stmt = $conn->prepare($sql);
 +        if ($stmt) {
 +            $stmt->bind_param("sssss", $name, $email, $phone, $birthday, $message);
 +
 +            $name = $post["name"];
 +            $email = $post["email"];
 +            $phone = $post["phone"];
 +            $birthday = $post["birthday"];
 +            $message = $post["message"];
 +
 +            $stmt->execute();
 +        } else {
 +            echo "Error: " . $sql . "<br>" . $conn->error;
 +        }
 +
 +        $stmt->close();
 +        $conn->close();
 +    }
 +</code>
 +
 +And displaying all entries in the index as a list:
 +
 +<code php>
 +    public function displayAllEntries(): void
 +    {
 +        $servername = "localhost";
 +        $username = "root";
 +        $password = "";
 +        $dbname = "form_exercise";
 +
 +        $conn = new mysqli($servername, $username, $password, $dbname);
 +
 +        if ($conn->connect_error) {
 +            die("Connection failed: " . $conn->connect_error);
 +        }
 +
 +        $sql = "SELECT * FROM entries";
 +        $result = $conn->query($sql);
 +
 +        if ($result->num_rows > 0) {
 +            while($row = $result->fetch_assoc()) {
 +                echo 'Name: ' . htmlspecialchars($row['name']) . '  -  Email: ' . htmlspecialchars($row['email']) . '  -  Phone: ' . htmlspecialchars($row['phone']) . '  -  Birthday: ' . htmlspecialchars($row['birthday']) . '  -  Message: ' . htmlspecialchars($row['message']) . "<br>";
 +            }
 +        } else {
 +            echo "0 results";
 +        }
 +    }
 +</code>
 +
 +==== Mistakes ====
 +
 +Code style:
 +
 +  * Apperantly I had too much repetitive code
 +  * Making it object oriented was good
 +
 +Functionality:
 +
 +  * Apperantly it was just barely reached, no explanation as to why.
 +  * I couldve done better validation
 +  * I couldve displayed multiple validation messages for all that was wrong
 +  * I couldve used PHP native validation functions
 +  * I couldve positioned the Form better
 +
 +Presenting:
 +
 +  * I was unprepared
 +  * I didn't answer many questions
 +  * There were some incidents with the streaming so that was bad blegh
programmieren/html/forms.1786543434.txt.gz · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki