Table design overview in phpmyadmin
PHP Sample Codes

Sample code to connect database using PHP

Here we are going to discuss about database connection using PHP. This is a sample code for PHP database connection to insert, update and read values from a table.  Before we proceed you people have knowledge of syntaxes and code of PHP. Please refer the PPT that I uploaded in this site to get familiarity with the code of PHP. Go through the php basics and php database connection methods.

First design a page that contains the following fields. First name, Last name, user name, password, confirm password. Keep all the necessary files (index.php, connection.php, user_create.php) in the www folder in Ubuntu. When you submit the form it will redirect to the page user_create.php. This tutorial will help you to connect mysql databse using PHP. Here is a sample code for PHP database connection. Try this sample code for PHP database connection in your system. Before you proceed make sure that you have installed PHP and configure phpmyadmin in your system.

sample code for PHP database connection

User Registration form (index.php):

Index.php

<html>

<head>

<title>Registrstion</title>

</head>

<body>

<form action="user_create.php" method="post">

<img src="" height="80px" width="80px"><h2> Registration Form</h2>

<input type="text" name="firstname" id="firstname" placeholder="First name"><br><br>

<input type="text" name="lastname" id="lastname"  placeholder="Last name"><br><br>

<input type="email" name="email_id" id="email_id"  placeholder="Email"><br><br>

<input type="text" name="username" id="username" placeholder="Userrname"><br><br>

<input type="password" name="password" id="password" placeholder="Password">

<br><br>

<input type="password" name="rpassword"  id="rpassword"  placeholder="Repeat Password">

<br><br>

<input type="submit" name="register" value="Register Now" class="btn-login">

</body>

</html>

 

sample code for PHP database connection
Home page

 

Database creation in MySql using phpmyadmin :

Before establishing database connection using PHP, go to the phpmyadmin page (type localhost/phpmyadmin in the address field and hit enter) in the browser. Create a database name and tables based on our requirement. Here I have created database called userdb and table name is users as shown in the figure. Table should have the fields to store the values in the index.php page. Follow the step to connect with the database using php .This sample code for PHP database connection tutorial will help you to achieve DB connection.

 

sample code for PHP database connection
Database table design

It is preferable to keep a separate copy of PHP database connection code and include the database connection code wherever is needed. Otherwise we need to type the database connection code in each and every file as this is time consuming.

sample code for PHP database connection

connection.php

<?php

$servername = "127.0.0.1";

$username = "root";

$password = "admin123";

$dbname = "userdb";

$port="3306";

$conn = new mysqli($servername, $username, $password, $dbname,$port);

if ($conn->connect_error)

{

die("Connection failed to MySql " . $conn->connect_error);

}

else

{

echo "MySql Server Opened...\n";

}

?>

 

Page that collect values from the Home page (index.php) and perform the database operations :

 

Create a page same as that we already specified in the action field of form tag in the index.php page so that when you submit the index.php page it will redirect values in the form  fields to the user_create.php page. User_create.php page should contain the methods to collect the values submitted from the index.php page.

Here the below code shows how to collect values from the index.php page and the operations of database using PHP.

user_create.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<?php

require_once('connection.php'); // import the database connection

$FirstName = $_POST['firstname'];// read value form field named firstname

$LastName = $_POST['lastname'];// read value form field named lastname

$Email_id = $_POST['email_id'];// read value form field named email_id

$UserName = $_POST['username'];// read value form field named username

$Password = $_POST['password'];// read value form field named password

$RPassword = $_POST['rpassword'];// read value form field named rpassword

$sql1 = "INSERT INTO users (FirstName,LastName,Email,UserName,Password)             VALUES('$FirstName','$LastName','$Email_id','$UserName','$Password')";

$rs1 = mysqli_query($conn, $sql1);

if($rs1)

{

echo "<br> User created ....";

}

/*

$sql1="update users set FirstName='$FirstName' where UserID=1";

$rs1 = mysqli_query($conn, $sql1);

if($rs1)

{

echo "<br> User updated ....";

}*/

/*$sql1="SELECT * FROM users where UserID='1' ";

$rs=mysqli_query($conn, $sql1);

while($row = $rs->fetch_assoc())

{

echo $row["FirstName"];

}*/

//$conn->close();

?>

</body>

</html>

The above code is to insert, update and read value from the database.  I have commented update query code and select query code, only insert query code is working. If you want to check update and select query, you can uncomment the select and update query according to your need.

You can also refer the code for database connection and php basics.

Leave a Reply

Your email address will not be published. Required fields are marked *