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>
<html>

<head>
    <title> Index </title>
</head>

<body>

    <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">
    </form>

</body>

</html>

 

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

 

Insertingdata.php

 

<?php

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

if(!$con){
     die("Database Connection failed".mysql_error());
}else{

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

if(!$db_select){
    die("Database selection failed".mysql_error());

}else{

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

    die("error".mysql_error());

}else{

    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