Inserting Form Data Into a Database Table Using PHP

This lesson is exclusive to members

Questions

1
1

Course info

13 lessons / 1 hours

Overview

Hi, In this course we are going to add a newsletter sign up form that will save our new subscribers to a database.

To do this we are going to be using a backend programming language called PHP and a MySQL database.Don’t worry if that doesn’t make any sense to you, we will be going over all of the basics and walking you through it step by step.

This course does however assume you have a basic understanding of html and css, and that you know how to push any updates up to a server and make them live.

If you don’t have that experience then I can recommend this course by Daniel Scott, it starts at the very beginning and will easy get you up to speed so you can start taking the next steps like working with PHP and databases.

So if you’re ready to get started with our PHP database project … then sign up to the course and lets get started :)
Daniel Scott

Daniel Scott

Founder of Bring Your Own Laptop & Chief Instructor

instructor

I discovered the world of design as an art student when I stumbled upon a lab full of green & blue iMac G3’s. My initial curiosity around using the computer to create ‘art’ developed into a full-blown passion, eventually leading me to become a digital designer and founder of Bring Your Own Laptop.

Sharing and teaching are a huge part of who I am. As a certified Adobe instructor, I've had the honor of winning multiple Adobe teaching awards at their annual MAX conference. I see Bring Your Own Laptop as the supportive community I wished for when I was first starting out and intimidated by design. Through teaching, I hope to bring others along for the ride and empower my students to bring their stories, labors of love, and art into the world.
True to my Kiwi roots, I've lived in many places, and currently, I reside in Ireland with my wife and kids.

Downloads & Exercise files

Transcript

In this video, we add in the last part of our PHP script  and we finally get to see something saving to our database. So now that we have our database connection,  the next step will be to go  and start organizing the data to add  to our newsletters table. So I'm just gonna save myself a little typing  and copy over another snippet of code. So what we're doing here is creating a customer array,  and we're creating our own array by using this shortcut,  which is just some square brackets. And inside that array we have a name, an email,  and a created date. Now this date is using the PHP date function,  and this text inside here is formatting it.

So we're just formatting it to year, month, day. And I'll just leave this comment here for you  to have a look at the PHP documentation. And if we un comment our pret tag  and the print R,  and we can have a look at what that array contains. So we can see here our name, email, and created that date. So if we just go back to VS code  and have a little tinker with this date function,  let's say if we just changed it to year refresh  that we can see that it's just outputting the year. So that's just a nice easy way to get up  and running with dates in PHP.

Now that we've got our data organized, let's go  and add the snippet of code that's going to actually  insert it into our table. So let's take a look at this. We've got our database connection variable that's going  to prepare this SQL statement  to add our customer data into the newsletters table. So what this is doing is if we just read this statement,  it's saying insert into newsletters, which is our table. These are our column names. And these values match the keys on our customer array,  which we pass through down here.

So if we save that  and refresh our page,  we get a blank screen because we're not outputting anything  after that query runs. But if we go back to our local host and refresh the page  and there we go, we're adding rows into our database table. Let's go back to VS code  and add in one more snippet. And this  Is just going to redirect us back  to our meat yogurt website. I don't think that's the correct URL. Let's just double check that.

Yep, it's just meat yogurt. So we wanna go back to, once this snippet  of code has run, we want to redirect back to  the meat yogurt website  and this exit at the end, just, there's another one up here  that just means that nothing else will run after this. So if we get into this error block, once it gets down  to the exit, that's it. It stops there, nothing else runs. Same with after this redirect, just double checking  that nothing else is going to run. So if we go back to our website,  and let's just start from the beginning at the homepage  and scroll down to our form and let's add in somebody else.

It's in Baxter, [email protected]. If we submit that all going well at redirects straight back  to our homepage. And if we take a look at PHP, my admin,  refresh the page, and there we go. We've added a new person to our newsletter list.
  • Powered by Marvin
  • Terms of use
  • Privacy policy
  • © Bring your Own Laptop Ltd 2025