Search code examples
phphtmlpostcontact-form-7

PHP Insert INTO DB don´t work with the Submit Button


I have a question about PHP, I am new at it, and would try something.

I have a Contact form the should submit data into my Database.

This is the PHP Content

<?php

if(isset($_POST['addEntry'])){
        $fname  = $_POST['sirname'];
        $lname  = $_POST['lastname'];
        $email  = $_POST['email'];
        $amount = $_POST['amount'];
        $period = $_POST['period'];

        $sqlInsert = "INSERT INTO testBase (sirname, lastname, email, amount, period) VALUES ('$fname', '$lname', '$email', '$amount', '$period');";
        $pdo->query($sqlInsert);

        var_dump('Test');
    }
    else {
        echo '<p style="color: white;"> Error !? Show Mee </p>';
    }

?>

This is the HTML Document :

<form action="index.php?id=test" method="POST" style="display: flex;">
    <input id="boxInput" class="editBoxInput" placeholder="Vorname" name="sirname" autocomplete="off" />
    <input id="boxInput1" class="editBoxInput" placeholder="Nachname" name="lastname" autocomplete="off" />
    <input id="boxInput2" class="editBoxInput" placeholder="Email" name="email" autocomplete="off" />
    <input id="boxInput3" class="editBoxInput" placeholder="Betrag €" name="amount" autocomplete="off" />
    <input id="boxInput4" class="editBoxInput" placeholder="Bezahlperiode" name="period" autocomplete="off" />
    <button class="addButton" type="submit" name="addEntry"> Einfügen </button>
    <button onclick="deleteContentEntrys()" class="addButton" type="submit" name="clearEntry" style="margin-left: 15px;"> Löschen </button>
</form>

The Error Message in the PHP Code is showing all the Time! ( echo ' Error !? Show Mee

'; ) <-- This showing up all time in the Document.

All code is in the same file. The PHP code is above the HTML Form


Solution

  •   <?php
    
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "bgvt_db";
    
       if(isset($_POST['addEntry'])){
    
        $fname  = $_POST['sirname'];
        $lname  = $_POST['lastname'];
        $email  = $_POST['email'];
        $amount = $_POST['amount'];
        $period = $_POST['period'];
    
    
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
       // set the PDO error mode to exception
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       $sqlInsert = "INSERT INTO testBase (sirname, lastname, email, amount, period) VALUES ('$fname', '$lname', '$email', '$amount', '$period');";
      // use exec() because no results are returned
        $conn->exec($sqlInsert);
    
    
        var_dump('Test');
      }
     else {
         echo '<p style="color: white;"> Error !? Show Mee </p>';
     }
    
    ?>
    
    
     <form action="stack.php?id=test" method="POST" style="display: flex;">
     <input id="boxInput" class="editBoxInput" placeholder="Vorname" name="sirname" autocomplete="off" />
    <input id="boxInput1" class="editBoxInput" placeholder="Nachname" name="lastname" autocomplete="off" />
    <input id="boxInput2" class="editBoxInput" placeholder="Email" name="email" autocomplete="off" />
    <input id="boxInput3" class="editBoxInput" placeholder="Betrag €" name="amount" autocomplete="off" />
    <input id="boxInput4" class="editBoxInput" placeholder="Bezahlperiode" name="period" autocomplete="off" />
    <button class="addButton" type="submit" name="addEntry"> Einfügen </button>
    <button onclick="deleteContentEntrys()" class="addButton" type="submit" name="clearEntry" style="margin-left: 15px;"> Löschen </button>