38: Insert data from a website into a database using MySQLi | PHP tutorial | Learn PHP programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to learn how to insert data inside our database directly from our website using PHP code and as you guys can see in front of me here I have the same code as we did in the previous episode but we learned how to select data from inside the database that's because we're going to use basically the same code in order to do the same thing when we want to insert data inside our database so as you guys can see in front of me I have all the same code ends at the index file as the database connection inside my gpa's file and I will also include the database code to our actual database inside database code that SQL now if you guys don't have these files and you want to get access to them maybe because you didn't watch the previous episode you can go ahead and download these files in the description so you can actually follow this lesson so just for the sake of it let's actually go ahead and go through the code from the previous episode so the basic thing we have here is the database connection at the top of our index file so we can actually get access to the database which is in here and inside the body tags we just simply wrote a SQL statement they will actually you know do something with the database then the next line we just simply query this SQL statement so it actually does something to the database using the connection we have down here and then on the next line we just simply check if we had any kind of results from this select statement if we did not have a result it's not going to run the if statement down here now if we did actually have a result it's going to run a while loop that will actually assign all the data to wherever row and spit it out inside our website so when we want to insert data instead of selecting data what we're basically going to do is we're going to go and delete this if statement here we're going to delete the result check like so so we only have these two lines of code so what I'm going to do here is I'm going to change these select statements and what I'm going to change it to is the insert statement I have inside my database code file here called database code that SQL I'm just going to go ahead and would wrap this for you so I can actually see everything so I'm basically just going to copy this code we have here go back inside my SQL statement inside my index file and pay in here and save it and that's basically how we insert data inside the database so just to explain what exactly is going on here basically we just have an insert statement that I inserted instead of the Select statement and then below here we just simply say you want to run a MySQL I underscore query function that simply courage this code and does something with it inside the database now since we're not selecting anything we basically just need to run the code and then it simply creates the user called Daniel Nelson blah blah blah and put them inside the database so basically we don't actually need to add the variable here we just need to actually run this function down here called MySQL underscore query and this will actually work so this is basically how we can insert data into our database directly from our website using PHP code but I know some of you will ask me how do we actually use this inside a practical example because right now I just sort of hand wrote the data inside the single quotes and here how do we actually do something inside a website that will insert data from for example a form into the database let's actually go ahead and go inside the body tag and I'll show you how to do this I'm going to go ahead and create a form and inside this form I'm going to create a couple of inputs so going to say we have an input called text we're going to have a name for this input this one is going to be at least the first one it's going to be the first name of the person we're going to have a placeholder called first name and again this is just basic HTML code so you guys should know how to do this by now so I'm just going to go and copy/paste this input statement a couple of times so we have for all the input we need to have inside our insert statement so we need one for first-name one for the last name one for the email one for the username and one for the password now for the password one I'm going to go and change the type to password I'm going to change the names of all of them so we have the second one up here called last which is last name going to change the placeholder to last name I'm never actually sure this is one word or two words because thing is not my native language but I'm just going to go and write it one word the third one is going to have a email so we're going to write email as the name we're going to write e - mail as the placeholder inside the fourth one we're going to write the username so it's called UID which is what I usually call a username then we're going to write username inside the placeholder and again this is basically if you want to create a user for for example a website inside the last one we're going to write PWD put a password and then inside the placeholder password like so now just for the heck of it let's hang to go and include a break in between each one of them because I know once we do X to get inside the website it's actually going to spit these out on one line and I want them on separate lines so I'm just going to go and include a break here I'm going to go and include a button at the end because when you taksa click something to actually activate this form so I'm going to say sign up which i think is two words in English inside the button tag I'm going to go and include a type set to submit so that actually registers that we submitted this form I'm going to set a name equal to submit now the reason I'm including a name inside the button is because once I do actually want to check if the user click this button I want to use the name in order to actually check for the submit I'll show you guys in a second now some of you people asked me what about required the attribute called required inside the inputs why do we not just include those using HTML if you want to make sure that the person actually filled out all these fields because the way we would typically do it is using PHP code you would actually go and check if the user did actually type in everything inside the inputs well when it comes to writing code in HTML or JavaScript or jQuery if I were to write the required attribute inside the inputs using HTML we can actually get around it inside the developer tool inside most browsers so it's not entirely secure when we use required inside HTML and so we don't want to do it only the HTML way we actually want to do the PSP way or with some other kind of thing language because it's much safer okay so now we have this you can actually go and go inside the form tag and say okay once I hit the submit button I want to run some kind of PSP code now the PHP code right now it's actually down here but I want to create a separate file and insert this code inside that file and in order to actually run that script once I click the button because right now it's just going to run this code as soon as i refresh the browser and I don't want to do that okay so I'm going to go to set an action which is going to be the path to the actual file that I want to run once we do I click the submit button now inside my root folder I have a index file I have the database code you guys can use if you want to copy my code from the database and I have an includes folder now inside this includes folder I have my database connection and like I said in the previous episode all the code that goes inside thing codes folder for me at least is going to be code that is not a direct page inside the website but instead just some PHP code that gets run in the background we don't actually physically see inside the website so I'm going to go and include a path to a file that we haven't actually created yet inside the includes folder called so we're going to say includes because you need to point to the file or to the folder and inside this folder we have a sign up dot Inc dot PHP file which again like I said we have an X created yet that's actually what makes me write that correctly like so and what I'm going to do now is I'm going to go and include a method so we need to actually decide how secure do you want this information to be when we do actually send it to our side of the link the PHP file right now we're going to do that using a method attribute which is going to be set equal to a post method since when we use the post method we can actually see the data inside the URL so if we were to use a get method we could execute the data but because this data is more sensitive because you have passwords and that sort of thing I want to use a post method okay so what we need to do now is we could actually go ahead and copy this code down here then delete it and insert it inside a new file save it inside the includes folder as signup dot Inc dot JSP like so so now we're going to actually run this file we will X include a user inside the database but wait once we do actually click the form button and go into this file it will not take us back to the front page and we need to fix that so inside this file we're going to go ahead and go to next line I'm going to say we have a header function inside PHP which simply takes us to another file so once it has actually run this code up here it's going to take us to the front page again so I'm going to say we have double quotes location colon space dot dot forward slash because it needs to take us back a directory because I'm inside my includes folder and then it's going to load index dot PHP if we could actually include some kind of success message so we're going to say question mark signup is equal to success if you want it to now the last thing we need to include inside this file is right now because I copy paste this code inside a new document right now we don't actually have access to our database connection so I need to go back to the front page and say we have this include one database connection copy it and then just leave it because you don't exit either inside the front page anymore go back to the signup page and include it at the very top here so now we do have access to where for column which we need to have access to in order to actually connect to the database so now we have this we could actually go back to the front page refresh and as you guys can see we now have a form with a couple of inputs and then a button now what is going to happen now is if I were Tyson click the sign up button is going to go ahead and run the code inside signup the link the PSP now right now since we haven't modified our insert statement yet it's just going to create a user called n Nelson blah blah blah inside the database but it's not going to take the information we typed in inside the input fields so we need to change that so inside my sign up during the PHP file we right after we have they included database connection I'm going to create a couple of variables so I'm going to go and say that variable called first which is the first name it's going to be equal to dollar sign underscore post brackets now this post variable here so super global which simply takes the data we passed down from the forum into this document and it's going to assign it equal to variable first okay now the name of this super global post is going to be equal to if we were to go back to the forum here equal to the name that we assigned the input so want to get the data from this input we need to refer to the one with the name first so I'm going to copy this name go back to the post superglobal paste in the name and then simply copy this statement a couple of times because we need to get all the other results as well which were a thing about five of them we're going to change the second one to last because that's the last name it's going to change the third one to email the fourth one is going to be change to UID and the last one is the password now again right now there's no security for this password it has not been has to anything when we insert into database I know some people actually comment on that this is just a very very basic signup form okay so now that we have all this data we can actually go ahead copy the first name go down to the SQL code down here and paste in variable first so I'm just going to do that for all of them instead of last name Nelson I'm just going to paste in last instead of the email I'm going to copy a miracle email and we're just going to continue doing this till we have everything filled out like so so now that we've pasted in all the variables inside our insert statement there's one more thing we need to fix because right now I just included the database connection but because we're already inside the includes folder I need to delete the include part inside the path so we just need to reference to DBH that Inc the PHP without the includes path okay so now that we have this we're basically ready to actually test this out so I'm going to go back inside the website I'm going to refresh and then I'm going to go and type in some information inside the form and then click sign up because now we'll actually take the information from the form and use it inside the insert statements so I'm going to write John go email could be John at gmail.com we're going to go insert a username as John Doe I'm going to set the password as test then when I click sign up you guys can see we get this path in the UL that says sign up equal to success and now if we were to actually go inside the database consume out a little bit for you guys right now we have two users inside the database one is called Daniel and one is called gain if I were to actually refresh the table you guys considered now we have John Doe so this is how we can actually insert data directly from our website using something like a form hope you guys enjoyed I'll see you guys next time
Info
Channel: Dani Krossing
Views: 337,160
Rating: undefined out of 5
Keywords: How to insert data into a database using MySQLi, insert data into a database using mysqli, how to insert data from a website into a database using mysqli, Insert data from a website into a database using MySQLi, php insert data into a database, mysqli database function, mysqli, php, php tutorial, php database tutorial, mysqli function, insert data into a database, send sql to database using mysqli, how to insert data into a database using php
Id: XhLAB1wwzNk
Channel Id: undefined
Length: 14min 8sec (848 seconds)
Published: Thu Jul 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.