KodeInfo | Learning resources for web and mobile development

How To Insert Data Into Database in PHP.

September 15th, 2014 14:22:29 by Irfan Iqbal Comments(0) - Views(5829)

To insert data into database in php you need to follow the simple steps.

1.Create a table in phpmyadmin as shown in the image.

After creating a table you need to create a HTMl file for requesting inputs by user.

let us take email and password and store it in database.

<html>
<head>
<title>testing</title>
</head>
<body>             
  <div class="container">
                    <div class="row">
                        <div class="col-md-5 col-md-push-4">
                            <form role="form" style="margin-top:100px;">
                                <div class="form-group">
                                    <label for="exampleInputEmail1">Email address</label>
   <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputPassword1">Password</label>
   <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputFile">File input</label>
                                    <input type="file" id="exampleInputFile">
                                    <p class="help-block">
                                        Example block-level help text here.
                                    </p>
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox">
                                        Check me out </label>
                                </div>
                                <button type="submit" class="btn btn-default">
                                    Submit
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
    </body>
</html>

Now let us create a PHP file to store email and password into database.

<?php

// Create connection
$con = mysqli_connect("localhost", "root", "", "shopefy");

// Check connection
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
mysqli_query($con, "INSERT INTO register (email,password)
VALUES ('$email',  '$password ')");
?>

In above PHP file file at first we have created a connection after that we initialised the variables.

Insert query is used to insert data into database as shown above.

Thanks switch to another posts for more.

 

Author

  • Irfan Iqbal
    Irfan Iqbal

    Irfan is a web developer and consultant from India. He is the co-founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, manages everything related to marketing and seo for kodeinfo , works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus