Complete PHP PDO Login | Register Application in 1 Hour with Source Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this tutorial we will be building a complete project and PHP using PHP data objects a this video library we will be interacting with the database with this library so a short little project is their login register system so here you can see that the list of users are empty because there are no users let me also show the database so this is my database login project inside it a users table is there so currently it is empty so that is why you are saying this in empty users either we have two options here login or register so first of all I want to register someone so I will give the user name as John and email address let me give John at the red gmail.com and password 1 2 3 4 5 6 if I click register here you will see that guys a profile dot PHP page has opened and it is welcoming John here apart from that we have three options logout update and delete functionality if I click logout I will be redirected into the index for PHP and we are successfully logout and if I want to log in again here within with the credentials John 1 2 3 4 5 6 if I click login here so you will see that I am redirected to the profile or PHP so if I show you the table here by the entry will be successfully added here you will see that username is equal to John email John at the rate gmail.com and password is being hatched by md5 hashing algorithm so you can see that if I want to update the entries I can just click the Update button and change the username from John to John 1 2 3 and email let me change to John 1 2 3 add the rate gmail.com and password is same 1 2 3 4 5 6 if I click update here you will see that guys the entries have been successfully updated you can see that the profile welcome John 1 2 3 and if I click the refresh the database also you will see that the entries will be effect reflected John 1 2 3 you can see that the email address has also been updated John 1 2 3 at the rate gmail.com and one last functionality is delete at the account here you can even delete the account if I click delete here you will see that the entry has been gone here and if i refresh the database you will see that the entry will be gone here so a simple crud application in login register system I have not styled the application at all I want to just concentrate of the on the just functionality part so John again I register it John add the red gmail.com some users I want to register first of all click on register and you will see that if I want to log out I can just log out and register one of the user Smith Smith I add the red gmail.com one two three four five six register and you will see that the profile wrote PHP if I click logout two users are added so this is displayed in the table like structure user name is there John Smith John at the red gmail.com Smith add the red gmail.com if i refresh the database you will see that two entries will be there so you can see that so now basically one of the functionality is also there if I want to register with this username I will be getting a error so if I click Submit John again and email address let me submit John at the red gmail.com once again one two three four five six if I click register you will see error that is username is already taken so if I click the differential database you will see that the entry will not be added so this is a again a validation we are checking further if I change the username to one two three four now the username is unique but the email address is same so if I submit the password once again and click the register so you will see that email is already taken so validation for both username and email have been done and if i refresh the database nothing is added so you can see that now if I just change the email to something else which is not existent and give the password one two three four five six so you can see that now it is refreshed and click logout here so you can see that the entry has been successfully added three you girls are there so now this is the short little application guys we will be building so let's start building the application so now basically start building the project so just go to xampp htdocs directory so I am using a local server called as exam which will be running my local development environment so just go to htdocs and create a new folder let me just name it a login project so you can just name it anything you want but I will be naming at login project so now I will be opening the command line in this folder and opening the text editor I'm using Visual Studio code you can use any text editor of your choice so let me just create the index dot HTML file so this will be a very simple HTML file HTML close the head and let me write the body and close the HTML tag also that's it so now in the head I will give a title that is log in register project using PDO close a title and now after that in the body we will be having two anchor tags but before that we will be having a heading so h1 tab so inside this we will be having a heading which is log in register project and now after that we will be having two anchor tags so this will be going to login dot PHP and the next one will go to register dot PHP so just name it as register dot HTML for the time being and let me just give a label login and register let me just you can see that the code is now looking good here and now if I want to open this refresh the application so you will see that log in register project and two links are there so it is going to the login dot HTML which we have not created so we will create this so just create a new file login dot HTML and register dot HTML so I can just copy paste the code here copy it and paste it in your login and same do for the register also and now we will be changing the title so father this will be register system and video so in this we will be having some fields here so just read this register at your if tag at the bottom here just delete this you only need the login one and here we will be just having a three input fields so input type will be 'text and names will be equal to username and the placeholder text will be username that's it input type is equal to e mail in this case because we are writing this for email name is equal to email placeholder is equal to email then we have the password input type password name is equal to password placeholder is equal to password that's it so now after that we will be having the input input type is equal to submit so in this case we will be giving a value attribute which will be register and let me also give a name attribute to it which is register that's it so this is the whole quote now if i refresh if i see the application reco to register you will see that three use fields are there you can see that username email and password and let me have a break back after that so that it shifts to the next line now if i refresh it you will see that now basically guys i want to do the same stuff for the login functionality also so i can just copy this code just paste it here just leave it all this paste it for the login also and some we need to just to register here and replace we just heard out HTML and this will be all same and here we just need to replace login and name will be login and here we don't need email because we will be only be login with the username and password that's it so also delete this so our login page is also complete register is also complete so now if i see once again if i click login you will see that username password login button and if i click the register button you will see that username email password register so now we just need to have a we obviously need to surround this by the form tag so I forgot it so just make sure that you do this so close the form and also in the login also and close this form tag so now basically guys this we will be sending the data by post so method will be post and the action attribute we only process dot PHP and same I will do for the register dot PHP also so method will be post and action it will be processed or PHP so now we just need to create this file which is processed or PHP and the first thing we need to do is just try these PHP tags and now we just need to make a class which have the connection this will be config dot PHP and in this we need to be having a static method which connects our database so we just need to write class config and a set of curly brackets and this will be class here you can see that so now we just need to have a public static method public static function connect this is a function let me write it once again here conflict dot PHP but it is deleted why there was coming I don't know let me rename this to config dot PHP just with a small C so now if I write it this will be class conflict so this is the class where you go 0 guys so now we just need to create a public static method public static function which will be connect and now we just need to have our for variables host will be localhost the username will be root password will be in this case null so just have double quotes and the DB name the database name and we have not created the database so just go to local host PHP my admin and first of all created database new database so for this create new here and database you just create here log in project just create the database database already exist so create this database skies login slash project here and also create let me just create once again a new database with a different name log in example sorry plot an example create the database database is created here and we want to create a user's table inside it number of columns would be 4 it is ok click on go and then we will be having our first will be ID it will be integer it will also be the primary key of the table so just click this auto increment box to make this primary key click on go the next field will be username username it will be varchar' the length will be 250 characters then we have email it will be again varchar' 250 characters and then we have the password it will again be a Varsha 250 characters and after this click on save and save our table here if I show you the structure ID username email password so this is our table and the database name is important login projects so we just need to substitute this name here inside this which is login project that's it so we have declared our variables now just we now have to write the PDO code in order to have connect to the database of PDO exception in you just need to write this in this in this fashion so you will be writing our code in try caps so if any error take place we will be catching this error and printing it out so we can just say that is echo if connection failed so you can just write a connection failed and concatenate by dollar e arrow get message that's it and now we just need to write our PDO code so now basically guys we will be writing the connection code so in the try catch so first of all write the connection variable corn in this we will be initializing new PDO inside this we will write in double quotes my sequel : host is equal to and then we will bind our host variable that's it semicolon and then we will write the DB name database name and is equal to and then we will bind our DB name variable that's it and then go out of the double quotes and put a comma and then we will be initializing our username variable and the password that's it so this is the syntax by which you can make the connection to the database in in PDO that is PHP data objects and now we just need to copy paste this line here that is set attribute we are setting the warning error mode in PDO so just write this line guys so it is very much imported and after doing this last here return this connection object that's it we will be getting the connection object from this static function and now the function is complete and just go to process dot PHP in order to access this function first of all we need to include the class so now for that you need to write include ones and there we will pass the full part of the file which is config dot PHP and then we will making a new variable connection which will hold the connection of the database so for calling the static method it is very easy write the class name which is conflict colon colon and then write the method which is connect so we are calling this method and storing the reference of the connection inside this cone variable now we just need to have a if condition is set dollar post and here we will pass register here because the name attribute that we have given in the register or HTML let me just show you is register you can see that the name attribute is very much imported in PHP it is used to access values so if you are saying that if the value of the button is set then we want to execute the code so first of all we will be getting the values first of all we will get the username Donal post in single quotes username I will pass this is a name attribute that I have provided in that register or HTML you can see that and then I will repeat for email dollar post email and then I will do the same thing for the password as well and one more thing guys I forgot in the config dot PHP you can see that this is our database name this database name is incorrect because our database name is login example so just make the correction I will make the correction but your database name will be different so if you are following me so just make this change login example so this was error that I wanted to talk about after making this change we need to first insert the details so for that we will make a new function insert details and in that I will first of all pass the connection object corn and then I will pass the username email and password and one more thing guys just make this connection variable inside this if condition so just copy this line here and delete from here and paste it here in the if condition so that it is accessible here so now we just need to make this function so I will just write function insert details first its get the connection second username email and password and now we just need to write very simple Curie in PDO so Curie and we will making use of the connection variable which is connection prepare so prepare is a state method by which we can just write sequel Curie's in PDO so inside this we will write our insert Curie insert into users so users is our table and then we will be right in the column names so column name first will be username email and password and then we will provide the appropriate values so this is the syntax so here we just need to provide colon and then followed by the placeholder value so these placeholder values can be anything you can name it anything but I will name it user name and similarly you can just write call an email calling password and now we just need to bind these values with actual values that we are receiving here so for that we just write curie bind param and here we are writing : username with actual username which is passed and we will repeat it for curie bind param this will be email we will be binding the email similarly we will be binding the password as well password and then pass in the password and now in order to execute the cure it is it is very easy in PDO so just we use single line Curie execute that's it so this will execute the Curie and i think that we need to return a boolean value from this function so what can I try it here we can just return this Curie execute it will either return true or false depending upon whether other curie executors successfully or not so here we will be binding this in the if condition so if it returns true then we know that the Curie executed successfully so here we can just print out a message that is details inserted successfully that's it so now basically guys our application should work it should insert new users in suit into the table if I open it localhost login Project and go to the register page and if I provide a user named such in and such in 1 2 3 add the red gmail.com password 1 2 3 4 5 6 click on register invalid catalogue no data is selected on line 33 let me just see in the config dot DB oh sorry guys this is not taller so just make this change here this was error which was coming so just remove the dollar sign here and if I again try to go to the same refresh the page first of all and such in search in 1 2 3 either a gmail.com 1 2 3 4 5 6 click on register you can see that guys details inserted successfully so now if i refresh the database and click the users table so you will see guys one entry was previously was there I / iya at this entry so now you can see that a new entry has been added such in such in 1 2 3 Arthur at gmail.com 1 2 3 4 5 6 so now basically we just need to go to the profile or PHP if it successfully registered so now in this if condition wherever we are printing out a successful message instead we need to set a session variable first of all so dollar session with the help of this we said session variables so we can just name it user name and we will bind the actual username which is passed in this function that's it and now the second statement that we want to be net mean to redirect the users into profile dot PHP for that we will be in header function in double quotes we will write location : profile dot PHP that's it and for this session guys we need to start the session also so right at the top here we need to write this statement session start we need to write the statement on all the pages on all PHP pages at the very top here so we will create a new profile or PHP file and in this we will be printing out a very simple message which will be echo which will be welcome space and followed by the session variable that we have which is username for this we first of all need to start the session the session start once again and then we need to include the database file which is conflict with PHP so this file you also need to include that in every script that you right here and now the step is ready from profile dot PHP so if the new user register it will be redirecting to it to profile dot PHP so now if i refresh and let me just register with another user go to register here and if I Smith Smith at the red gmail.com one two three four five six click on register so you will see that guys it is welcoming Smith here so we are making use of session variables and we are successfully redirecting the user to profile dot PHP and now we just need three very links here for logout and for update and for delete so here in the profile dot PHP we need to have three links so this link will be a anchor tag H is equal to logout dot PHP so whenever it is clicked it will go to this link so the text will be logout this is the first link the second link will be for the update functionality so if we go to the update dot PHP close anchor tag so here the label will be update and the last one will be to delete the count so this will be a gain anchor tag it will go to delete dot PHP and the delete functionality is very easy guys and just try delete here put a semicolon and also guys put a single quote at the end of it so you can see that now if i refresh it refresh the profile page you will see that the links are appearing logout update delete so basically we need to first of all implement the logout functionality so whenever it clicks logout it will redirect it to logout dot PHP so first of all we need to create this file logout dot PHP so here we can just write a very simple two lines of code here will actually log out the user first of all we will destroy the session so session destroy' that's it so it destroys all the data registered to a session so after that we need to use the header function once again to redirect the user header location and I will pass the location to index.html for right now that's it guys so now if I click the logout button I will be redirected to index dot HTML you can see that guys the logout functionality is successfully working here so you can see that if I want to log in with this username search in 1 2 3 4 5 6 I cannot login because I have not implemented the login functionality so just implement the login functionality also so now I will go to the process or PHP and I will copy paste this if condition here and paste it here and then I will change this register to login login and the the rest of the code will remain constant here I don't need the email so just delete the email here and instead we will be calling the check login function so here I will call the check login function so 80 receives log connection variable username and password so now we just need to create this function guys check login so right here at bottom create this function check log in check login this keyboard is very much this check login so it receives three arguments connection username password so we need to just copy-paste this time the Select URI I need to write here so I will write Kure and this will be connection but a pair once again I will use a prepared statement and in this prepared statement I will write here select select hurry so select star from the table name which is users where username is equal to the placeholder value at I will provide and password is equal to the placeholder value that I will provide so this is a simple select every guy so now we just need to bind the values so it is very simple so what can I do here is that I can just copy paste lines here which I have written here and also this Kure execute so and I can just paste I don't want the email because email is not passed here so we are passing the username and password and then we first of all need to execute the Curie here I don't want to return it and instead I want to check how many rows are returned so I will be having a very simple if condition here so if in order to check how many rows were returned in PDO it is very easy we have a function Curie arrow for row count so row count is a function which it returns how many rows a little if it is returned or unique row which is one then we can just say that my username exists with the password so we can just return true here else in the else part if no rows are returned then we can safely say that the username doesn't exist so we can just say return false that's it so now we just need to bind this in AF condition similarly we have done here so bind this in F condition so we can check here if it returns true here we will be similarly be setting the the session variable these two lines of code I will copy here once again paste it so you can see that and in the else block I can just print out a error message the username the username and password are incorrect that's it so now if I see guys if I go back to the login page now and if I try to have a join 1 2 3 4 5 6 if I write the John 1 2 3 4 5 6 so this is a valid user guide so if I click the login button it should redirect me you can see that welcome John and the login functionality is very much working here if I click logout I will be logging out of the application if I click login again and if I try to register with the random user here which doesn't exist it should do sorry guys it should print out a message which is the username and password does not exist so you can see that guys the errors are also working and this is a login functionality so now basically we are done with the login and register and now we just need to make the update and the delete functionality delete we also done so na basically guys we will be applying some validation in our application so if I try to restore a client here you can see that if I click the register button it should prompt me because of the required attribute that we set in the register dot HTML so basically you can see that if I try to register this username with some spaces so you will see that it will accept it there is no kind of validation which is there inside PHP we can see that if I register it you can see that it will take this username I don't want this username to be inserted into the database if I again show you one other scenario if I please register here if I insert HTML tags here you can see that this is malicious code here it's going to destroy your application it will accept this so if I write this register you will see that it is accepting it so it is malicious code here and if we refresh the database here you will see that it is inserting this HTML tags so now I this is a very dangerous security challenged for our application for removing this we first of all need to go to this process or PHP and first of all make a very simple if condition here we need to check here dollar post or we can check under this condition after these lines we can just check for the variables if username is empty so we can just write like this or email is empty in either of these scenarios we don't want to execute any kind of code here so also we can just write for the password as well password is empty if any of these fields are empty we just need to write return here so this will not execute the code after it and now we just need to copy paste the same line in our login functionality also and here we don't want email we only need username and password so just delete this that's it so this is the first validation that we are doing the second validation guys we need to write some helper functions first for helper function I will do is for if condition I will write here which is I will be sanitizing it so let me just make this function sanitized string this is a function that we will make here and we will pass this dollar post username value similarly I will be doing this for the email also sanitize string sorry dollar and I will be passing this email so just make this guys and for password this will be difference because if I show you how we are storing passwords in plain text which is not a very good practice at all we need to the hash them has those passwords so to hash them it is very much it is very easy let me just show you sanitize it first of all that's it so sanitize password in this case so we will make these functions here guys so very right at the bottom here we will write first of all sanitize Sani ties drink so this will just receive it a string argument and inside it first of all we just need to apply a strip tax function guys so this will remove all the HTML PHP tags from a string it will pass the string here and second check we will apply is the string replace replace and we just need to replace the spaces so we will provide the spaces with single quotes you can see that we will remove the spaces inside the string that's it so after applying all these changes we just need to return the string return string that's it this is a function by very simple function we are using to function in speech P that is first is strip tax and string replace and now I will be writing my sanitize sanitize password which will receive our password first of all we just need to have our md5 applied to it I will just write md5 this is a hashing algorithm calculate the md5 hash of a string and I will just pass the string to it that's it and just return it return string that's it string so now this function is also complete guys so now if I apply the same things if I copy it do this for the login functionality also so just paste it here in this case we don't want the email so just delete the email field so now if i refresh the application guys and once again I did don't I want to delete all the entries which are presently there here if I delete all these entries and start from scratch so all these entries have been deleted and if I go back to the index.html page if I try to register one other client here if I have some spaces here John James and John add the weight gmail.com or mail.com one two three four five six click on register you will see that the spaces will be gone here you can see that John James are printed out and if I check the database refresh it you will see that the password is now stored in a hashing algorithm way it is not stored in it plain text which is secure now and also the spaces are removed here from the username that's what we have warned here so our validation is now done guys so this is the validation that we have applied and now we just need to go to the this profile or PHP and implement a delete dot PHP functionality so now just create a delete dot PHP file just rename it to PHP so in this script this is very easy guys so just declare the PHP tax first of all and here we just need to start the session and write a very simple Curie so this Curie will be first of all it will be taking the connection object connection preparer so we just need to include our collection for that we just need to include our file include one's config file config class and then we will store our connection variable just right connect so it will have the connection here so now in this prepared statement we will write our delete Curie so delete Curie is very easy guys in PDO so just write delete from table name which is users where username is equal to the currently logged in user name which is we will get by using session variables so let me just declare a new variable which is username we will get the currently logged in user name by dollar session we will pass username here that we have set it in register and login here and then we will just find the by using bind param and here I can just bind my username to the actual user name which is correctly logged in and then we can just execute our query and lastly we need to just redirect the user to index dot HTML so for redirecting it we will use in header function so here I will just pass location index rotate index dot HTML that's it guys so this is the delete dot PHP code here first of all we are grabbing the currently logged-in user name by session variable and then we are executing a very simple delete Kure delete from users where username is equal to the correctly logged in user that's it so now if I register an user let's suppose John John at the red gmail.com one two three four five six register if I try to delete this user sky so let me just first of all show you a table here one entry will be added John you can see that if I try to delete it you can see that guys it is successfully redirected me to the index dot HTML page if I check the entry here you will see that the entry will be gone here so we have successfully implemented the delete functionality also so now we just need to implement our update functionality also so also we just need to now update functionality so just create the update dot PHP file and here we just need to write our update dot PHP code so now basically guys we will be writing the update dot PHP code so first of all we need to write the statement which is session start in order to start the session and then we will be grabbing the actual user name which is logged in so for that we will be writing to all our session username that's it and now first of all we also will be including include ones config dot PHP file and then we will be writing that's all that we need here and now we will be writing the basic HTML for having the update form HTML head title will be update details for and then we will be having the PHP tags so here we will echo out the username semicolon and close the PHP tags and here I will close a title also close the head and in the body we will be having a simple form so we will just copy the register or HTML file here you can see that guys so you can copy this copy this and paste it in the update dot PHP inside body here so instead of register we will be having update and in the value also this will be not register this will be update and apart from that the values remains constant here it will go to processed or PHP and now if i refresh the page here if I log in into some user here and if I provide John one two three four five six click on login and the user and password are incorrect sorry let me just see oh I deleted that user so I should login through John James password is one two three four five six so you can see that guys it is profile dot PHP if I click update you will see that it will be redirecting it to this page where we can just update the details and if I see the title you will see that update details for John James it it is grabbing the username and now we just need to go to this process or PHP file and process over code here for update so right here inside we have a another if condition here after this we will now check for a set dollar post update so if the Update button was clicked we just need to execute our code so we will just copy paste the whole code which was there inside register so you will all copy this validation let me just copy paste all this paste it here in the update so this will remain remain constant here we need to apply the validation sanitizing straying password and we will be checking for the emptiness of the username email and password and here we just need to make some change here instead of insert details so you will be updating the details update details and here we will be passing connection object username email password and apart from that I need to pass the ID of the field that need to update the ID ID field is unique for every table it is a primary key of the table so for fetching aidid first of all we need to write a simple curri curri we need to write at the paid statement so here guys first of all we need to grab the currently logged in user name so here we can just create a new variable current user name something like this and here I will just make the session variable this will be user name so now I will pass this user name so here I will just select select star from users where username is equal to the currently logged in user name so here I will just provide a placeholder value and then in the next step Kure bind param I will be initializing it to the variable that we have that is current user name that's it so now in order to fetch the ID it is very easy first of all we will be fetching the whole result curie fetch and inside this we will pass a parameter which is a fetch associated so basically it will fetch all the results in an associative array and now we just need to grab the ID by this which is result and inside double square brackets we just need to write the column name which is ID that's it so this will grab the idea of the field that we need to update and now we just need to pass this ID also along with the connection ID that's it and if this sum if the function exam is successful then we will be redirecting it to we will be setting the session variable username user name currently updated user name and then we will be setting the header location to profile or PHP that's it so now basically guys need to create this function which is update detail so right here at the bottom you will create it function update details first argument its takes is the connection object second it takes the ID third it takes the username email and password so these variables it takes so first of all we just need to write here a simple curie here connection repair so inside this preparedstatement we just need to update the entries so this update curie is very simple guys update followed by the table name which is users and here we just need to set the column name so first column is user name we need to set to that it required a placeholder value that we will provide comma the email sorry this will be email is equal to the placeholder value that we will provide comma password is equal to the placeholder value that we will provide and there is a where clause where ID is equal to the ID that we will provide here we have passed this ID here and now we just need to bind the parameters so here in the next step we will just write curie bind param param first will be the user name : user name you will be passing the currently logged in user name so i will just copy paste this line here for the email password and ID so this will be email so this will become email this will become password this will become password and lastly we have ID and this will become ID that's it so now we just need to execute the query guys so cure execute and lastly we just need to I think that we just need to return Theory execute that's it if the Curie is successful or not depending upon it it will return a boolean value and now hopefully let me just to have a if clause here so you can see that we will be updating it to the profile what PHP so header function will work so now if i refresh the application once again guys if i click the login here or let me just register a new client here john john at the day gmail.com one two three four five six click on register and if I want to update the details about John so if I want to change the username John one two three and John one two three at the rate gmail.com so email is also changed one two three four five six password remains same if I update call to undefined function fetch [Music] 7100 are you guys do yes this is missing here this arrow is missing so that is why it was taking problem if I again this was minor mistake of mine so if I again login into it click on login provide the username which is John one two three four five six click on login and then update the details John one two three John one two three add red gmail.com one two three four five six click on update so you will see that guys it is updating the details about John with John one two three and if i refresh the database you will also see this entry being replayed reflected here this which is John at the red gmail.com if I again refresh it it should the entry is not the dated here let me just see let me just log in was once again John one two three four five six welcome John if I try to update it John one two three John one two three hundred gmail.com it should update let me just see if I refreshed it it is not updating so the problem was coming guys because of a very simple mistake that I have committed here in the process wrote PHP so after selecting this Curie here selecting start from users where username is equal to username I have binded the parameter which is currently logged in user name but I didn't execute the Curie so you just write Curie execute so this is my error guys so just write this Curie execute here and then you will be getting the result in an associative array and now if i refresh the application once again guys and if i click register and let's suppose I write James here James at the rate gmail.com password is one two three four five six if I click register you will see that welcome James if i refresh the database this is my login example database you can see that James have been successfully inserted if I click update here if I want to change the details about James James one two three username James one two three add the raid gmail.com and the password let me change also to something like this James click update you will see that it changes to James one two three but most importantly if i refresh the database it will also reflect the changes that is james one two three james one two three at the rate gmail.com so now the update functionality is also working guys and now one last thing that i wanted to implement guys that if I click if I try to register this James again if I try to gesture James again you will see that it will not be preventing me to J to use the same email address which is currently present if I try to register once again it will again register James again so this is not appropriate at all you need to prevent the same entries being duplicated in the database you will see that these this email address is taken but it is already inserted for the second time so to prevent this we will be creating a function validation function inside our register here just before we insert details here we need to have a if condition here first of all we will check the username exists function then we will pass our username and also our connection object that's it we will bind this in if condition so if it returns true then we just need to or if it returns false we just need to have a not sign here we don't want to execute the quote just return that's it and we just need to do the same stuff for the email also check email exists and here we just need to pass email so password can be same but we need to check for the username and email and now we just need to do the copy-paste this for the update functionality also because when we update the details we also need to tell the user that this username is taken out this email is taken so just to write after this paste is lines and now we just need to create these two functions so right here at the bottom create these functions first is check username exists you will have the connection and the username and second function is check email exist we would have the connection and the email address so now basically guys in first check username exists we need to have the connection so this is we need to write here sorry guys this is curie not connection so Curie connection prepare we need to write a very simple prepared statement so this will be select star from users where username is equal to the currently passed and username i will provide the placeholder value later on in the next step so I will bind this curie bind para username is equal to the currently passed in username and off in order to check if the user exists we have the if condition if Curie row count so how many rows are returned if it is equal to one then we can safely say that the username exists so here in this case we can just return false that's it in else we can just return true if no rows are returned then we can say we say that the username is unique and it does not exist in the database so we are returning true here and so same call code I will copy paste for the email also and paste it here paste and here I will just replace email is username this will be email that's it guys so this is our two helper functions which are completed here and now if I go back to the application once again let me see if I execute the query Hey once again I have not executed the Curie knives this fatal mistake I am doing it to execute the Curie if I don't execute the query and how the result will come so curious acute just write the statement I wish I cannot forgot this so now basically guys if I try to register with any of the username which is already present not login register if I try to check here if I try to submit James again let's suppose and James one two three at the weight gmail.com the email address is also present one two three four five six if I try to register you will see that it is preventing me now if i refresh your database you will see that no entry will be added here and let me also print out a appropriate message here so that right here we can just print out a message echo user [Music] username already exists and also just write the return statement after this email already exists and I can just copy-paste this whole code here for the update also so right here when we are updating the details here this is for the update we need to also do for the register also so right here paste it so now it will also display a message so if I try once again fill out the password so i refresh the page for changes to take place James 1 2 3 register username already exists because it takes if I try to enter unique username but the email is present here check email exists oh sorry I have made a mistake of once again s should be there also I need to do this for just attach s here these small mistakes have really cost we even see that X check email exists username exists let's just see if I named it correctly or not check email exists and check username exists now if I again refresh if I try to enter James James one-two-three at the raid gmail.com one two three four five six username already exists so now if I try to enter unique username if I try to register there is error once again I have check email exists I need to just change its name here so replace this name to this that's it where email is equal to email curie bine to re-execute now if i once again try to register it james one two three james one two three at the red gmail.com password one two three four five six click on register so you will see that username already exists so if I try to enter unique username it is taking the e it is inserting the same email once again so I just need to look here why what is sorry guys I figured out the error because you can see that multiple entries with the same email email address was present at first so now I just need to delete all these entries first of all just left one entry and delete all the rest of it and then you will see the error messages coming up you will see that now if I try to use the same email address and go back to my application and let me just try to register here click register and use the username John and again James 1 2 3 ad 3 at gmail.com now it you ciara click on register you will see that email already exists early on we have multiple entries in the email field so that is why it was violating this condition that we have if row count is equal to is equal to one so this was violating the condition and thereby it was inserting the same email into the database and now our validation are all complete guys so now the last thing that is remaining in order to print out all the user name in a table like structure in our indexed or HTML page so for that guys we need to move on to our indexed or HTML page and now we just time has come to rename this file to index dot PHP because we will be inserting some PHP code so index dot PHP and here we just need to apply or you you may say that in delete delete' dot PHP we need to make some changes here first of all we just need to make index dot PHP not index dot HTML and that said this is a one change that we need to make here and now in this we have to have the PHP tags right at the top here so here we will be writing our code in order to displaying the list of users in a table like structure so na basically guys we will be fetching all the records and displaying the table like structures of first of all we need to include our config dot PHP file include once config dot PHP file that's it so now in order to use the connection we will be declaring connection variable and calling the config class get connect method and storing the correction variable and for getting the results here we will be storing them inside this results variable and we will be creating a function fetch records inside this we will call the path the connection variable and that said this is the hole that we need here connection variable and now we will be making this function fetch it Graz which is taking the connection object and now we just need to write a simple Curie so which will be gone prepare so this will be a very simple select Curie guys select star from users grab all the entries users table name and now we just need to bind the entries Curie or there there are no variables guys so we just need to execute the quixote not find anything execute and in order to grab it the results there we will be or we can just result we can just return curie fetch all this is a way by which we can just return all the entries which are presently there in the database so this will be stored inside results here and now we just need to go back into the HTML after these anchor tags we will be having the table tab close the table so inside this we will be having a table row close a table row inside this first of all we will be having two table headings which is email close the email and username close our table headings and now close our table two also and now we will be having a new table row inside it we will be populating over the course but before that just have declare the PHP tags here PHP close that and inside it we will be populating over for each loop we will be using for each inside this we will be using the results variable which gets all the users inside it as user and you can see that in the now we just need to close this PHP here so this is the way by which you can just embed PHP in HTML and now we just need to have our table Rotax close the table row inside this we will be having TD tag which stands for table data so inside this we can just populate our PHP tags which is first will be echo dollar user the first field that we need to display is the email of the user close the PHP and close the TD tag I can just copy paste this line here and repeat it for the username and replace here username that's it so this is all the code that is there guys we are displaying the email and username on the index page and if I now refresh it hopefully I will see the entries you will see that guys one entry has me added so it will displaying the email and username if I enter or some more details if I try to be just a John John add the red gmail.com one two three four five six click on register so you will see that one entry has been added if i refresh the database you will see that it results also if I log out you will see that nothing is there because it is redirecting it to the index dot HTML file you just need to also makes that change also so just go to logout dot PHP and replace from index HTML index dot PHP so now you can see that two entries are there if I try to adjust her once again Smith Smith at the red gmail.com one two three four five six click on register log out you will see that this is a fun project guys I really enjoyed making this project if I log in using the credentials that I have created you will see that it will login me also you will see that if I try to delete it you will see that that the heat functionality is also working if I show you the database you can see that so tax or rich guys for watching this video and hopefully you enjoyed the video if you really enjoyed the video please hit the like button if which is there in below the video click the red subscribe button for watching these videos in the future also please support me guys and I will be seeing you my next video
Info
Channel: Coding Shiksha
Views: 11,306
Rating: undefined out of 5
Keywords: complete php pdo login, pdo login register system, pdo, php pdo full project, php pdo, php pdo login system, coding shiksha, php pdo login with session, php pdo login/register system source code, pdo project, pdo project oman, php pdo project
Id: 5p7YwbEf8Y8
Channel Id: undefined
Length: 73min 58sec (4438 seconds)
Published: Sat Nov 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.