Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Using Relational Tables Querying Multiple Tables with JOIN

Alex Flores
Alex Flores
7,864 Points

How to post form data to database?

I have't been through all of the videos yet, but I don't see any title that looks like it'll explain it. How do you post form data (e.g., say user registration data) to the database?

Thanks

Jonathan Arbely
Jonathan Arbely
6,691 Points

Did you do the "Build a Basic PHP Website" course? I'd especially recommend chapter "Adding a Basic Form" to you. It teaches how data is exchanged between an HTML form and PHP (and from there to a database).

Alex Flores
Alex Flores
7,864 Points

Yes, I did that one. It doesn't show how to submit form data to a database. However, I figured out how to do it, but not how I want to. I'm trying to create a submit function so whenever a user submits data and it needs to be inserted into the database I can call this function. Unfortunately, everytime I try to pass POST data to a function it breaks and I don't know why.

Google isn't providing any answers at the moment.

3 Answers

Alex Flores
Alex Flores
7,864 Points

I figured out the answer and it was a WEIRD one. Firstly, I forgot to add:

error_reporting(E_ALL);
ini_set('display_errors', 1);

This is EVER so important to debugging in PHP. By using this I figured out that indeed my include(path) was broken. I then used

include($_SERVER['DOCUMENT_ROOT']."/Feedback/inc/functions.php");

to include my functions folder. I'm just going to use $_SERVER['DOCUMENT_ROOT'] on all of my includes not unless someone knows a reason for me not to.

Next, I learned from extensive Googling that my SQL statement within my function...

$results = "INSERT INTO customer_registration_info (first_name) VALUES ($firstName)";

was wrong. I learned that I needed to include single quotes on my variable and backticks on my table name and column name.

$results = "INSERT INTO `customer_registration_info` (`first_name`) VALUES ('$firstName')";

In SQL, backticks indicate names (of tables, columns, etc.) and single quotes or double quotes indicate strings. The backticks aren't necessary, but it's good practice to distinguish between the two.

Hope this helps someone else. Also, Johnathan, thanks for your help!

Jonathan Arbely
Jonathan Arbely
6,691 Points

Might wanna share this function with me? :) Also, it's a great idea to use "echo" for debugging.

Alex Flores
Alex Flores
7,864 Points

The function is

function push_registration($firstName) {
    include("connection.php"); //instantiates the connection with the database

    try{ 
        $results = "INSERT INTO customer_registration_info(first_name) VALUES ($firstName)";
        $db->exec($results);
    } catch (Exception $e) {
        echo "<h1> Cannot Connect to Database </h1> <br/>";
        echo $e->getMessage();
    }

}

I just want to note that I'm calling the function within an if statement (if($_SERVER["REQUEST_METHOD"] == "POST") ). I'm not sure if that has anything to do with it. Also, there in different files.

Jonathan Arbely
Jonathan Arbely
6,691 Points

I see. Is the "Cannot Connect to Database" error being thrown? Or nothing really happens? If that's the case, try to remporarily remove the if-statement. And as I sait, put echos wherever you can to figure out how far the code progresses and hwere it breaks.

Alex Flores
Alex Flores
7,864 Points

No, it goes blank. I think the issue is when I try to include(functions.php). I'm getting very inconsistent behavior from this statement. The file where the form is in a Procedures folder and functions.php is in my Inc folder. So I'm doing

include("../inc/functions.php"); 

However, I notice that it will behave differently if I add a "/" to the front of it (e.g., include("/../inc/functions.php");"). I'm not sure why this is.

Also, I have tried using:

    include($_SERVER['DOCUMENT_ROOT']."inc/functions.php";
        push_registration($firstName);

But i get the same issue.