CRUD OPERATIONS:
INSIDE xammp/htdocs: create the project : CRUD
Create database named: first_project
In visual studio:
A. Admin
a. auth
i. Connection
1. Config.php
ii. Database
1. User.sql
b. Users
i. Create.php
ii. Edit.php
iii. Delete.php
iv. Index.php
v. View.php
Config.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "php-tms";
// $conn = mysqli_connect($host, $user, $pass, $db);
$conn = new mysqli($host, $user, $pass, $db);
// if(!$conn){
// echo "Database Connection Failed";
// }
// another way to check connection
// if($conn){
// echo "Database Connection Successfull";
// }
// else{
// }
CREATE.PHP
<?php require('../auth/connection/config.php'); ?>
<section class="py-5">
<div class="container w-50">
<div class="d-flex justify-content-between">
<h4 class="pb-3">Add Users</h4>
<p class=""> <a name="" id="" class="btn btn-primary btn-sm"
href="index.php" role="button">Manage user</a>
</p>
</div>
<?php
if (isset($_POST['register'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];
if ($name != "" && $phone != "" && $email != "" &&
$password != "") {
$select = "SELECT * FROM users WHERE email='$email'";
$result = mysqli_query($conn, $select);
if ($result->num_rows > 0) {
?>
<div class="alert alert-danger alert-dismissible fade
show" role="alert">
<strong>Email is already exit</strong>
<button type="button" class="btn-close" data-bs-
dismiss="alert" aria-label="Close"></button>
</div>
<?php
// header('Refresh:2; URL=create.php');
echo "<meta http-equiv=\"refresh\"
content=\"2;URL=create.php\">";
} else {
$insert = "INSERT INTO users (name, phone, email,
password) VALUES ('$name', '$phone', '$email', '$password')";
$result = mysqli_query($conn, $insert);
if ($result) {
?>
<div class="alert alert-success alert-dismissible
fade show" role="alert">
<strong>User is created</strong>
<button type="button" class="btn-close" data-
bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
echo "<meta http-equiv=\"refresh\"
content=\"2;URL=index.php\">";
} else {
?>
<div class="alert alert-danger alert-dismissible
fade show" role="alert">
<strong>User is not created</strong>
<button type="button" class="btn-close" data-
bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
echo "<meta http-equiv=\"refresh\"
content=\"2;URL=create.php\">";
}
}
} else {
?>
<div class="alert alert-warning alert-dismissible fade
show" role="alert">
<strong>All fields are required</strong>
<button type="button" class="btn-close" data-bs-
dismiss="alert" aria-label="Close"></button>
</div>
<?php
}
}
?>
<form action="#" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="exampleInputName" class="form-
label">Name</label>
<input type="text" class="form-control" name="name"
id="exampleInputName" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPhone" class="form-
label">Phone</label>
<input type="tel" class="form-control" name="phone"
id="exampleInputPhone" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email
address</label>
<input type="email" class="form-control" name="email"
id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-
label">Password</label>
<input type="password" class="form-control"
name="password" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary"
name="register">Submit</button>
</form>
</div>
</section>
EDIT.PHP
<?php require('../auth/connection/config.php'); ?>
<section class="py-5">
<div class="container w-50 ">
<h4>Add User</h4>
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
$get_select="SELECT *FROM users WHERE id=$id";
$select_result = mysqli_query($conn, $get_select);
// fetch single row data from database
$data= $select_result->fetch_assoc();
}
if (isset($_POST['edit'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
if ($name != "" && $phone != "" && $email != "") {
$insert = "UPDATE users SET name='$name',
phone='$phone', email='$email' WHERE id=$id";
$result = mysqli_query($conn, $insert);
if ($result) {
?>
<div class="alert alert-success alert-dismissible
fade show" role="alert">
<strong>User is updated</strong>
<button type="button" class="btn-close" data-
bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
echo "<meta http-equiv=\"refresh\"
content=\"2;URL=index.php\">";
} else {
?>
<div class="alert alert-danger alert-dismissible
fade show" role="alert">
<strong>User is not updated</strong>
<button type="button" class="btn-close" data-
bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
echo "<meta http-equiv=\"refresh\"
content=\"2;URL=create.php\">";
}
}
} else {
?>
<div class="alert alert-warning alert-dismissible fade
show" role="alert">
<strong>All fields are required</strong>
<button type="button" class="btn-close" data-bs-
dismiss="alert" aria-label="Close"></button>
</div>
<?php
}
?>
<form action="#" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="exampleInputName" class="form-
label">Name</label>
<input type="text" class="form-control" name="name"
value="<?php echo $data['name']; ?>" id="exampleInputName" aria-
describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPhone" class="form-
label">Phone</label>
<input type="tel" class="form-control" name="phone"
value="<?php echo $data['phone']; ?>" id="exampleInputPhone" aria-
describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email
address</label>
<input type="email" class="form-control"
name="email"value="<?php echo $data['email']; ?>" id="exampleInputEmail1"
aria-describedby="emailHelp">
</div>
<button type="submit" class="btn btn-primary"
name="edit">Submit</button>
<div class="mb-3 form-check">
<span>I have already an account <a href="index.php">Login
Now</a></span>
</div>
</form>
</div>
</section>
DELETE.PHP
<?php
require('../connection/config.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
$get_select="DELETE FROM users WHERE id=$id";
$select_result = mysqli_query($conn, $get_select);
echo "<meta http-equiv=\"refresh\"content=\"0;URL=index.php\">";
}
VIEW.PHP
<?php require('../auth/connection/config.php'); ?>
<section class="py-5">
<div class="container w-50 ">
<h4>Add User</h4>
<a name="" id="" class="btn btn-primary" href="index.php"
role="button">Manage Users</a>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$get_select = "SELECT *FROM users WHERE id=$id";
$select_result = mysqli_query($conn, $get_select);
// fetch single row data from database
$data = $select_result->fetch_assoc();
}
?>
<form action="#" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="exampleInputName" class="form-
label">Name</label>
<input type="text" class="form-control" readonly
name="name" value="<?php echo $data['name']; ?>" id="exampleInputName"
aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPhone" class="form-
label">Phone</label>
<input type="tel" class="form-control" readonly
name="phone" value="<?php echo $data['phone']; ?>" id="exampleInputPhone"
aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email
address</label>
<input type="email" class="form-control" readonly
name="email" value="<?php echo $data['email']; ?>"
id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
</form>
</div>
</section>