Sample Code
Sample Code
Sample Code
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
/* define variables and set to empty values */
$nameErr = $emailError = $mobileError ="";
$name = $email = $mobile = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
else {
$name = test_input($_POST["name"]);
/* check if name only contains letters and whitespace */
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["name"])) {
$mobileError = "Name is required";
} else {
$mobile = test_input($_POST["mobile"]);
/* check if name only contains letters and whitespace */
if (!preg_match('/^[0-9]{10}+$/', $mobile)) {
$mobileError = "10 digit Number";
}
}
if (empty($_POST["email"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["email"]);
/* check if e-mail address is well-formed */
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "Invalid email format";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Mobile: <input type="text" name="mobile" value="<?php echo $mobile;?>">
<span class="error">* <?php echo $mobileError;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailError;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
What is CRUD?
CRUD refers to the four basic types of Database operations: Create, Read, Update, Delete. Most
applications and projects perform some kind of CRUD functionality. Once you learn about these
CRUD operations, you can use them for many projects. For an example, if you learn how to
create student table with multiple columns, you can use similar approach to create employee
table or customers table.
1) Write a PHP script to create login page with SQL database connectivity
Login1.php
<html>
<head>
<title>PHP login system</title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
</head>
<body>
<div id = "frm">
<h1>Login</h1>
<form name="f1" action = "authentication.php" onsubmit = "return validation()" method =
"POST">
<p>
<label> UserName: </label>
<input type = "text" id ="user" name = "user" />
</p>
<p>
<label> Password: </label>
<input type = "password" id ="pass" name = "pass" />
</p>
<p>
<input type = "submit" id = "btn" value = "Login" />
</p>
</form>
</div>
// validation for empty field
<script>
function validation()
{
var id=document.f1.user.value;
var ps=document.f1.pass.value;
if(id.length=="" && ps.length=="") {
alert("User Name and Password fields are empty");
return false;
}
else
{
if(id.length=="") {
alert("User Name is empty");
return false;
}
if (ps.length=="") {
alert("Password field is empty");
return false;
}
}
}
</script>
</body>
</html>
connection.php
<?php
$host = "localhost";
$user = "root";
$password = '';
$db_name = "logindb";
Authentication.php
<?php
include('connection.php');
$username = $_POST['user'];
$password = $_POST['pass'];
$sql = "select *from login where username = '$username' and password = '$password'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count == 1){
echo "<h1><center> Login successful </center></h1>";
}
else{
echo "<h1> Login failed. Invalid username or password.</h1>";
}
?>
2) Write a php script to retrieve student data from MySQL database using PHP CRUD
Operation
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentdb";
// if error occurs
if ($conn -> connect_errno)
{
echo "Failed to connect to MySQL: " . $conn -> connect_error;
exit();
}
if ($result->num_rows > 0)
{
// fetch all data from db into array
$row = $result->fetch_all(MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<style>
td,th {
border: 1px solid black;
padding: 10px;
margin: 5px;
text-align: center;
}
</style>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($row))
foreach($row as $rows)
{
?>
<tr>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
mysqli_close($conn);
?>
3) Write a php script display name, mobileno, email address and perform CRUD
operation using SQL database
Form.php
<?php
// Create database connection using config file
include_once("connect.php");
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href="add.php">Add New User</a><br/><br/>
<tr>
<th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$user_data['name']."</td>";
echo "<td>".$user_data['mobile']."</td>";
echo "<td>".$user_data['email']."</td>";
echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a href='delete.php?
id=$user_data[id]'>Delete</a></td></tr>";
}
?>
</table>
</body>
</html>
Connect.php
<?php
/**
* using mysqli_connect for database connection
*/
$databaseHost = 'localhost';
$databaseName = 'crud_db';
$databaseUsername = 'root';
$databasePassword = '';
?>
Add.php
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href="add.php">Add New User</a><br/><br/>
<tr>
<th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$user_data['name']."</td>";
echo "<td>".$user_data['mobile']."</td>";
echo "<td>".$user_data['email']."</td>";
echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a href='delete.php?
id=$user_data[id]'>Delete</a></td></tr>";
}
?>
</table>
</body>
</html>
<html>
<head>
<title>Add Users</title>
</head>
<body>
<a href="form.php">Go to Home</a>
<br/><br/>
<?php
Edit.php
<?php
// include database connection file
include_once("connect.php");
// Check if form is submitted for user update, then redirect to homepage after update
if(isset($_POST['update']))
{
$id = $_POST['id'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
Delete.php
<?php
// include database connection file
include_once("connect.php");
// After delete redirect to Home, so that latest user list will be displayed.
header("Location:form.php");
?>