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.