TLDW logo

How to Connect HTML Form with MySQL Database using PHP

By Technical Babaji (Tarique Akhtar)

Summary

## Key takeaways - **Specify action and POST method**: To take form data, specify the form attribute action with the php file name like connect.php and set method to post. [00:34] - **Access POST data with $_POST**: Using $_POST because data is sent using post method, specify the input field name like first_name to get the data into php variable. [01:47] - **Create database and table**: Create database named test, then table registration with columns id primary key auto increment, first_name varchar(50), last_name varchar(50), gender enum('m','f','o'), email varchar(50), password varchar(20), number varchar(10). [02:34], [03:05] - **Connect with mysqli**: Use $con = new mysqli('localhost', 'root', '', 'test'); then check if (!$con) give connection failed message. [04:34] - **Prepare prevents SQL injection**: Prepare the insert query with question marks for values, then bind_param with 'ssssssi' for five strings and one integer to bind data at runtime not compile time. [05:23] - **Execute and verify insertion**: After bind_param pass variable names, execute the query, show registration successfully message, close statement and connection; data inserts perfectly into table. [06:08]

Topics Covered

  • POST Captures Form Data Precisely
  • Enum Locks Gender to MFO Values
  • Bind Five Strings One Integer

Full Transcript

hi it's me tarik akhtar ansari in the last video i have shown how to design this registration form in this video i will take the user input and when user will click on the submit button i will store this form data into a database using php so let's get started subscribe technical babachi channel and press the bell icon to get the latest video updates

these are the coding which i have done in the last video so in this video i am going to take this form data to take this form data we have to specify the form attribute action there is specify the php file name so here i am giving the php file name connect .php and one more attribute is specify method so here i am taking the input using post method now create this connect .php file

connect .php file connect .php save it now write

connect .php save it now write php code inside this file using php tag for getting the form data we have to specify the form input field name so here i am specifying the name of each input field name

first name so using this name i will get the data into the php file now in the same way i will define the name for all the input field so here i have already defined the name for the name for the gender now we have to specify the value so that we can get the value of this name m when user will click on m female when user will click the female

and others o in the same way i will define the name for email name password and for the number now using this name we can get the data into the php file here i am specifying the variable name dollar first name and dollar underscore post because i am sending the

data using the post method so using the post we can get the data into the php and specify the name first name is the name of the input field so we will get the data into the first name variable in the same way we will do the last name gender email password and for the number now for the multiple select use ctrl d and change the name last

name do the same for the others like gender email the password and for the number now we have to save this data into the database so we have to do the database connection we don't have the database so first of all we have to create the database go to the php my admin panel click on new and create one database so

here i am giving the database name test click on create now create the table to store the data here i am giving the table name registration and i am taking seven column first column i am specifying as id which will be primary key and auto incremented okay now i am defining first name where care last name

where care type gender enum type and i am defining the m f o m for male f for female and o for other it will not take any other value okay next email type

where care password type where care and number type begin now i am defining the length of each column 50 50 and password for length is 20 and for the number length should be 10 okay now click on

save so my database is ready now back to the php coding and write the connection code so here i am taking the con as a variable new mysqli localhost user name is root by default xam password is blank mysql password is blank and i am here i am giving the database name test which is now i have

created now check the connection if error then we can give a message connection failed and give the error message if no error then else part will execute inside this first we have to take prepare then inside this prepare write the insert query

insert into registration then specify the column name first name last name gender email password and number and for the values specify question mark for each column name okay now bind this question mark with their proper data bind param we

have only four different data types for binding so enum and string will also treated as a string so here first name last name gender email password will be treated as a string and number will be treated as integer so here i will specify five f and one i for integer now i will

pass the variable name for the binding now finally we will execute this query and now i will give one message to the user registration successfully finally we will close the prepare statement and finally we will close the connection now our coding almost done let's check it

it's working or not so here i am giving the information when i am clicking on submit button you can see my data is inserted into the table so here you can see the data is coming perfectly now once more time let's check it so here i am giving the different data

password password now when i am clicking registration successfully so let's once again check it so data is coming perfectly so thank you guys for watching this video you

Loading...

Loading video analysis...