PHP Tutorial (& MySQL) #30 - Saving Data to the Database
Key Takeaways
This PHP tutorial demonstrates how to save data to a MySQL database using the INSERT SQL command, covering the basics of database interaction and data storage.
Full Transcript
all right then gang so I wanted to do one little thing before we moved on and that's just to add the links to these things in the Navar so this should go to the form right here and this should go to the homepage so I've gone to the header already come down to the template and this right here the brand logo I've sent to index.php which is the homepage and this down here which is the button I've sent to add.php so now these things work okay like they should Okay so in this video I want to talk about how we can save data to the database and we're going to do that on the ad page over here so when we enter in the details we're going to click submit then we're going to run this file again when we submit the form and we're already doing that if we have a look in add.php we're already looking for any kind of errors and validating the form and so far all we're doing if there's no errors is we're relocating to the index page but right here this is where instead we want to take that data and we want to save it to to the database so how are we going to do that well the first thing we need to do is actually connect to the database inside this file we've done it inside the index.php right here but we also need to do it in this file because this is where we're going to be saving the data from so what we could do is copy and paste this code right here over here where we connect to the database and then check the connection but that's kind of just repeating ourselves and I don't want to do this every time we have a new file where we connect to the database so instead what I'd like to do is externalize this code modularize it in its own file then just include it whenever I need it so that's what we're going to do first of all so I'm going to create a new folder and call this config and then inside that config folder I'm going to create a new file and this will be saved as dbor connect. PHP because that's what this is doing then I'm going to copy or rather cut that connection script open up my PHP tags and inside here I'm going to paste that back in so that's all this file is going to do connect to the database using these credentials and then check the connection now we need to include it over here in the index.php where we used it before so I'll say include and we want to include SLC config to go into that new config folder then slash dbor connect. PHP okay so that should still work so let me just ref the index over here to make sure it still works okay and we got an error so let me see what's going on over here and this is called dbor connect and that is called dbor connect and it's because we have this forward slash right here we don't need that save this and refresh and now it works perfect okay then so now we can require as well or include this file inside the ad. PHP because we need to connect right here as well so let's do that first of all at the top I'm going to say include and we want to include the config SL dbor connect. PHP file okay and then down here in the if check this is where we're going to save the stuff to the database in this else Clause because at this point we know there's no errors if there are errors this would fire and we're outputting those errors down here in the Dom but when there's no errors we're just relocating them so this is where we need to add that data to the database and then relocate them to the index.php page okay so we have the data that we want to add to the database right we have now an email we have a title in this variable and we also have the ingredients now before we directly save them to the database what I'd like to do is use a function and that function is called my SQL I real Escape string and we're going to pass those values into that function and what that does is escape any kind of malicious SQL characters or any sensitive SQL characters and it protects us from SQL injection from people inserting harmful code into our database right now this is a bit like this but whereas this is what outputting values to the browser this other function I'm about to use is for protecting data going into the database okay now there are other ways to do all this using a PDO approach and prepared statements but since we've not covered PDO I'm showing you this way for now then maybe I will do a series on the PDO approach and prepared statements in the future because it's something you should definitely learn when you become more comfortable with PHP and MySQL but anyway we'll go with this way for now so what I'm going to do is override the values we currently have for the email title ingredients by saying email is now equal to my SQL iore real Escape string like so and then we pass in first of all the connection okay remember we make that connection reference right here and the second parameter we pass in is the actual value that we want to store in the database and that is going to be the email right so what I'm going to do is just copy this thing so post email okay so the value that we get back from the user and the connection and we're storing that now in this email variable so essentially we're overriding this email variable from before okay so then that's the first one what I'm going to do is just duplicate this a couple of times and we're going to change this to the title and the title over here and then we also need the ingredients so let me just control scoll click to do both of those and call this ingredients like so so now we have the data ready that we actually want to input into our database okay so the next thing to do is create the SQL string that is going to go and do that for us so I'll create a new variable comment first of all create SQL a new variable now called SQL and set that equal to a string now much like we used this stuff before to get data from a database this time we're using a different kind of command to add data to the database and what we want to do is use the keyword insert and then we say into to say which table we want to insert data into now we want to insert into the pizz table remember that's what we called our table and the things we want to insert are the title the email and the ingredients so we're going to basically insert into the pizzza table these three things that's what we have right here yeah now then the values that we want to inject or insert are going to be as follows and what we're going to do is enter in these different variables right here because they're the values we want to add into these things into these different columns so we have to do this in quotes so I'm going to do first of all the title and that's not in capitals then the email and then finally we want the ingredients like so and then finally close off the whole string like that so pretty simple all we're doing is saying okay insert into the pizza's table and we want to update these columns and then these are the values for those columns in order now the other two columns remember are created out which is going to get automatically created when we make a new row and the other one is the ID which Auto increments remember so that gets created for us as well so these are the only three we need to worry about adding so that's the SQL that we need now we want to save it to the database and check that it's worked so we'll say save to DB and check okay then now I'm going to write this out then I'm going to quickly explain it so my SQL iore query we've seen this this is the function we use to make a query and we pass in connection first of all and the SQL we want to use for that query and that's nested in an if statement now then this thing right here this is the function like I just said to make a query we've already seen it right here okay we didn't Nest it inside an if statement right here but we still made that query right here we passed in the connection and the SQL which in this case was to select data into that query and that gave us a result now over here what we're doing is nesting that in an if statement now the SQL we're passing through is this stuff to add to the database the table so if that's successful this query this will return an equivalent True Value and right here this would be a success now if it didn't then it would return a false value and there would be some kind of error and we'll Echo that error out so we'll do that first Echo and then a string we'll say query error and then we're going to concatenate to that string and I'll just say my SQL iore error and then pass in the connection okay so this is the error generated if there is one I'm going to Output that to the screen if this fails if this query fails and if there's a success then it means it has saved it to the database and what we do at this point is redirect them okay so we take that from here we don't need this anymore and we redirect them right here to the index page once that's been saved so then let's cross our fingers and hope this all works I'm going to go to the ad Pizza page what we got so far we've got ninja and Mario let's do the Yoshi Supreme so Yoshi at theet ninja. co.uk the pizza title is the Yoshi Supreme and that's going to be cheese tomato and eggs okay submit and now we can see that right here awesome so that has worked so now we know how to save data to the database what I'd like to show you in the next video is how when we click on more info we're going to show the details the extra details of individual records individual pizzas
Original Description
Hey gang, in this PHP & MySQL tutorial I'll explain how to save data to our MySQL database. We'll do this with a SQL command called INSERT.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/php-mysql-tutorial
+ VS Code editor - https://code.visualstudio.com/
+ Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff
🤑🤑 Donate
+ https://www.paypal.me/thenetninja
🎓🎓 Find me on Udemy
+ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
California just voted to make data centres pay for their own power lines.
Medium · Data Science
How I Built a Real-Time Formula 1 Stats Tracker: Mr Saturday Russell comes through other side after real tough period
Dev.to · Muhammad Bin Nazeer
Batch vs Stream Processing: What Data Engineers Actually Need to Know
Medium · Data Science
How Data Science Can Improve Materials Management in Offshore Oil & Gas Industry
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI