<!--This is on HTdocs >NewFolder in xampp folder -->
<!-- Index.php -->
<?php
$conn = OpenCon();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<!-- Connection DB Setup --> (db_connnection.php)
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = '';
$db = "atlink";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
With the connection successful we can insert some data or display
This is a normal HTML form
-> index.html
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<meta charset="utf-8">
<meta name="viewport" content="=width=device" initial-scale=1>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<form action="connect.php" method="POST" name="myform" class="form-group" >
<h2 style="text-align:center; font-weight:bold">SIGN IN</h2>
<div class="row jumbotron">
<div class="col-md-12">
<label for="comment">Comment:</label>
<textarea class="form-control" rows="4" id="comment" placeholder="I'm signing because" name="comment"></textarea>
</div>
<div class="col-md-6">
<label for="inputname">First Name</label>
<input type="text" class="form-control" placeholder="Firstname" name="fname">
</div>
<div class="col-md-6">
<label for="inputname">Last Name</label>
<input type="text" class="form-control" placeholder="LastName" name="lname">
</div>
<div class="col-md-6">
<label for="email">Email</label>
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<label for="country">country</label>
<br/>
<select class="form-control" name="country">
<option>Please select</option>
<option value="srilanka">Sri Lanka</option>
<option value="america">America</option>
<option value="india">India</option>
</select>
</div>
<div class="col-md-12" style="text-align:center; margin-top: 20px;">
<input type="submit" class="btn btn-primary" value="sign in">
</div>
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
<html>
<head>
<title>Form</title>
<meta charset="utf-8">
<meta name="viewport" content="=width=device" initial-scale=1>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<form action="connect.php" method="POST" name="myform" class="form-group" >
<h2 style="text-align:center; font-weight:bold">SIGN IN</h2>
<div class="row jumbotron">
<div class="col-md-12">
<label for="comment">Comment:</label>
<textarea class="form-control" rows="4" id="comment" placeholder="I'm signing because" name="comment"></textarea>
</div>
<div class="col-md-6">
<label for="inputname">First Name</label>
<input type="text" class="form-control" placeholder="Firstname" name="fname">
</div>
<div class="col-md-6">
<label for="inputname">Last Name</label>
<input type="text" class="form-control" placeholder="LastName" name="lname">
</div>
<div class="col-md-6">
<label for="email">Email</label>
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<label for="country">country</label>
<br/>
<select class="form-control" name="country">
<option>Please select</option>
<option value="srilanka">Sri Lanka</option>
<option value="america">America</option>
<option value="india">India</option>
</select>
</div>
<div class="col-md-12" style="text-align:center; margin-top: 20px;">
<input type="submit" class="btn btn-primary" value="sign in">
</div>
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
Let's create PHP file for action ->connect.php
<?php
$comment = filter_input(INPUT_POST, 'comment');
$fname = filter_input(INPUT_POST, 'fname');
$lname = filter_input(INPUT_POST, 'lname');
$email = filter_input(INPUT_POST, 'email');
$country = filter_input(INPUT_POST, 'country');
echo $fname;
$dbhost = "localhost";
$dbuser = "root";
$dbpass = '';
$db = "atlink";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
$sql = "INSERT INTO form(comment,fname,lname,email,country)values ('$comment','$fname','$lname','$email','$country')";
if($conn->query($sql)){
echo "New record is inserted";
}
else{
echo "Error: ".$sql. "<br>" . $conn->close();
}
?>
Printing values in the database
<html>
<body>
<?php
include 'db_connnection.php'; <-connection setup file(Displayed Above)
$conn = OpenCon();
echo "Connected Successfully";
$query = "SELECT * FROM empoyee";
echo "<b> <center>Database Output</center> </b> <br> <br>";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Value1</font> </td>
<td> <font face="Arial">Value2</font> </td>
</tr>';
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
// coloum filds to fetch data
$field1name = $row["Uname"];
$field2name = $row["adress"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
</tr>';
}
$result->free();
}
CloseCon($conn);
?>
</body>
</html>

0 Comments