Submitting data to MySQL with PHP OOP

I am having an issue with a simple submit form submitting data to mysql. I have done this dozens of times but for some reason it isn’t working right now and I can’t seem to find out why. First I define the DB, then I connect, test the connection or exit if fails. Once connected I test the fields aren’t empty, store them in a variable and create a var with the query in it. Once the query is submitted I test the submission was successful, return a success or failed and close the DB connection. The code is below but it should’ve worked a long time ago. What can possibly be wrong in this? I also added an additional if statement not shown here that test to see if the field are empty and return a response if they are. It did return a response. “Fill out all fields please” It doesn’t seem to be reading the data in the input fields. First time that’s happened to me. That section isn’t added in here but just to give you a heads up that it tested for that and came out negative.

define("SERVER","IP Addy");
define("USER","DB User");
define("PASSWORD","DB Pass");
define("DATABASE","DB Table Name");

    $Con = new mysqli(SERVER,USER,PASSWORD,DATABASE);

if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['number']) && !empty($_POST['date']) && !empty($_POST['message'])) {

        if($Con->connect_errno) {
            printf("Connection to MySQL failed: %s n",$Con->connect_error);
            exit();
        }

            $name = $_POST['name'];
            $email = $_POST['email'];
            $number = $_POST['number'];
            $date = $_POST['date'];
            $message = $_POST['message'];

            $req= "INSERT INTO table (name,email,number,date,message) VALUES ('$name','$email','$number','$date','$message')";
            $query = $Con->query($req);

        if($query === TRUE) {
            echo "Data was submitted successfully";
        } else {
            printf("Data submission failed, Something is wrong with your query: %s n",$Con->error);
        }
    $Con->close();

}


Source: oop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.