Unable to append data to DB Table using mysqli_query

I have a HTML form.

The form sends the (submitted) data to a .php file.
<form action="/php/post-contactus.php" method="POST">...</form>

This PHP file connects to my 000webhost database & appends the data to a (pre-existing) table (called formdata_jobapplication).

<?php
 //pass the name from your HTML form
$fName = htmlspecialchars($_POST['firstName']);
$lName = htmlspecialchars($_POST['lastName']);

//Store connection criteria
$conn=mysqli_connect("localhost", "USER", "PW", "DB_NAME");

// Checking connection...
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//Store SQL query after, after establishing connection to database.
$data = "INSERT INTO formdata_jobapplication (`fname`, `lname`) VALUES ('$fname', '$lName')";

//Execute SQL query!
mysqli_query($conn,$data);
echo 'Data submitted!';

?>

When the user submits the form they are presented with the success message (Data submitted!).

However when I connect to my DB (using phpMyAdmin) I do not see any new rows, listed in the table.

I CAN add rows (to the formdata_jobapplication table) using phpMyAdmin.

The form can be found here: http://mengelsen.000webhostapp.com/job-descriptions/job-application.html

I’m also attaching an image of the table columns:

Hi!

Your columns must have their Default attribute to NULL in order to insert data into table without having to specify them.

Great thought! However I tried changing all the column default values to “NULL” and I am still not seeing my form submissions appear in phpMyAdmin.

This leads me to believe that one of my fields is “breaking” the (INSERT INTO) SQL command.

I wrote a new .php file that spits-back the values of my form fields. Here is the output:
First Name: Neil
Last Name: E
Email: person@example.com
Phone Number: 911911911
Contact Preference: email
Max Hours: 33
Start Date: 2019-08-14
DOW: Sunday
Position: Manager
Bio: My bio here!
File Upload: image-icon.png

Is it possible that the “-” symbol (from “start_date”) is causing an issue? If so, how do I overcome this?

mysqli_query($conn, INSERT INTO table_name ('start_date') VALUES ('$start_date')";

Is it possible that the “-” symbol (from “start_date”) is causing an issue? If so, how do I overcome this?

No.


When you insert a new row, write everything to that same row at one time:

INSERT INTO table_name (stfname, lname, ...., EMPTY2) VALUES ('first name', 'last name', ...., 'empty value')

We are using MariaDB as database system, and syntax is slightly different than traditional MySQL: it’s more object-oriented like.Try the upper query and see if it works. Try it in phpmyadmin console as well (to make sure it’s not a PHP error). I apologize for not being entirely helpful; I am not a fulltime web developer :slight_smile: