Domesticated Brain

Inserting form data into MySQL database using php

We are working with two files .one is index.php and the other is insertingdata.php. Index.php include basic html form to get user input of username and password field

Index.php file

<!DOCTYPE html>

    <title> Index </title>


    <form action="insertingdata.php" method="POST">
    UserName: <input type="text" name="username"><br>
    Password<input type="password" name="password"><br>
    <input type="submit" value="submit">




The output of the index.php will be displayed as shown below


PHP Tutorials Basic HTML Form

Setting up the database


Go to the phpMyAdmin
Create a New database called testdb

 PHP Tutorials testdb


Create a table called tblusers for recently created database testdb

 PHP Tutorials Create table tblusers


PHP Tutorials Inserting Data





$con = mysql_connect("localhost","root","root");

     die("Database Connection failed".mysql_error());

    $db_select = mysql_select_db("testdb", $con);

    die("Database selection failed".mysql_error());


    $sql = "INSERT INTO tblusers(uname,pswd) VALUES('$_POST[username]','$_POST[password]')";



    echo "inserted";





File the username, password and click submit button to check whether the program we created is functioning or not


PHP Tutorials Submit Form

PHP Tutorials End Result

For more details you may go and check in phpMyAdmin whether data inserted into the database

PHP Tutorials Inserted Table

If you have found  what you expected please comment and like on our facebook page