Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Wednesday, June 8, 2016

Creating new website and handling its database on your local system.



In my last two posts :Install XAMPP server, test and run a website on your own local system and Host more than one website  from your own local system., I told you about installing a server on your system and testing a simple website on your own local computer. Now in this post we will learn about creating a website for users and handling its database from your system.
To begin with first we create database for our website. We are creating a website for users to enter username, email id and contact number. Let the name of database be userd, and table name is users. First go to your web browser and type the following: http://localhost/phpmyadmin/



New web page will open: to create database, click on new ,

Enter the name of database as userd, and select collation as utf8mb4_unicode_ci and click on create.
Now after creating database, it will ask for table name, number of fields in table. Under create table enter name of table as users and Number of columns as 3. Click on go.



Now enter the name of columns: username, e_id, contact.



Click on save. Now our database has been created.
To create a website named www.mytestweb.com, first create a new folder named mytestweb in hdocs folder at C:\XAMPP\htdocs.
Now open the file httpd-vhosts file with note pad or other editor which is lying in the folder:
C:\XAMPP\apache\conf\extra
Add the following lines at the end of file:

<VirtualHost *:80>  
DocumentRoot "C:\XAMPP\htdocs\mytestweb"
ServerName www.mytestweb.com
</VirtualHost>

Save the file and close it.
To local host the website we have to add www.mytestweb.com to hosts file in the folder:
C:\Windows\System32\Drivers\etc
Open the hosts file with note pad or any other editor and add the following lines at the end of the file:
127.0.0.1 www.mytestweb.com
Save the file and close it.
Now add the following php files to the mytestweb folder which is lying in C:\XAMPP\htdocs.
Let us start with creating a registration page in php. In this registration page we will accept some data like username, email_id, contact_no from user and store it in our data base. Following is the php code for registration page:

<html>
<body>
<h1>Welcome To mytestweb.com</h1>
<br>
<h2>Please Register Your Self at our wesite for free</h2>
<br>Please Enter Your Details:
<form action="insert.php" method="post">
User Name: <input type="text" name="username" /><br><br>
Email ID : <input type="text" name="e_id" /><br><br>
Contact Number: <input type="int" name="contact" /><br><br>
<input type="submit" />
</form>
</body>
</html>

Copy the above code and paste in new text document file and save it as register.php.
In the above code, insert.php file is used to insert the data into our database.



Code for insert.php:

<html>
<body>
<?php
 // Create connection
$conn = new mysqli('localhost','root','');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// this will select the Database userd
mysqli_select_db($conn,"userd");
// create INSERT query
$sql="INSERT INTO users (username, e_id, contact) VALUES ('$_POST[username]','$_POST[e_id]','$_POST[contact]')";
if ($conn->query($sql) === TRUE) {
    echo "Thanks for Entering Your Details";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
mysqli_close($conn);
?>
<br><a href = 'index.php'>Click Here For Home Page</a>
</body>
</html>

Every website have an index.php or home.php file. So indext.php file in our case is:

<html>
<body>
<body style="background-color:orange">
<h1>Welcome to Mytestwebsite</h1>
<br>
<a href="register.php"> Register yourself here: </a><br>
<br>
OR
<br>
<br>
<a href="details.php"> Click here to see the list of registered user details</a>

</body>
</html>

Now show the details entered by users:
Code for details.php file is as follows:

<html>
<body>
<?php

// Create connection
$conn = new mysqli('localhost','root','');

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

mysqli_select_db($conn,"userd");

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
echo"<table border = '1'>
<tr>
<th>User Name:</th>
<th>Email ID: </th>
<th>Contact Number</th>
</tr>";
echo "<tr>";
echo "<td>" . $row["username"]."</td>";
echo"<td>"  . $row["e_id"]."</td>";
echo"<td>" . $row["contact"]."</td>";
echo"</tr>";
echo"</table>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>
<br>

</body>
</html>

Go to your favorite browser and enter the website address : http://www.mytestweb.com/
Following screen will open:


First enter some data by clicking on the link: Register yourself here: 



Enter your details and click on submit.

You will see a thanks message like:


To go on home page click on the link Click Here For Home Page

On the home screen click on the link Click here to see the list of registered user details to see the list of users registered.