first commit

This commit is contained in:
Sharayu Markunde 2024-01-18 19:29:46 +05:30
commit f4329580fc
5 changed files with 303 additions and 0 deletions

96
add-new.php Normal file

@ -0,0 +1,96 @@
<?php
include "db_conn.php";
if (isset($_POST["submit"])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$sql = "INSERT INTO `crud`(`id`, `first_name`, `last_name`, `email`, `gender`) VALUES (NULL,'$first_name','$last_name','$email','$gender')";
$result = mysqli_query($conn, $sql);
if ($result) {
header("Location: index.php?msg=New record created successfully");
} else {
echo "Failed: " . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>CRUD Application</title>
</head>
<body>
<nav class="navbar navbar-light justify-content-center fs-3 mb-5" style="background-color: #00ff5573;">
CRUD Application
</nav>
<div class="container">
<div class="text-center mb-4">
<h3>Add New User</h3>
<p class="text-muted">Complete the form below to add a new user</p>
</div>
<div class="container d-flex justify-content-center">
<form action="" method="post" style="width:50vw; min-width:300px;">
<div class="row mb-3">
<div class="col">
<label class="form-label">First Name:</label>
<input type="text" class="form-control" name="first_name" placeholder="Albert">
</div>
<div class="col">
<label class="form-label">Last Name:</label>
<input type="text" class="form-control" name="last_name" placeholder="Einstein">
</div>
</div>
<div class="mb-3">
<label class="form-label">Email:</label>
<input type="email" class="form-control" name="email" placeholder="name@example.com">
</div>
<div class="form-group mb-3">
<label>Gender:</label>
&nbsp;
<input type="radio" class="form-check-input" name="gender" id="male" value="male">
<label for="male" class="form-input-label">Male</label>
&nbsp;
<input type="radio" class="form-check-input" name="gender" id="female" value="female">
<label for="female" class="form-input-label">Female</label>
</div>
<div>
<button type="submit" class="btn btn-success" name="submit">Save</button>
<a href="index.php" class="btn btn-danger">Cancel</a>
</div>
</form>
</div>
</div>
<!-- Bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>

14
db_conn.php Normal file

@ -0,0 +1,14 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "php-crud";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";

11
delete.php Normal file

@ -0,0 +1,11 @@
<?php
include "db_conn.php";
$id = $_GET["id"];
$sql = "DELETE FROM `crud` WHERE id = $id";
$result = mysqli_query($conn, $sql);
if ($result) {
header("Location: index.php?msg=Data deleted successfully");
} else {
echo "Failed: " . mysqli_error($conn);
}

103
edit.php Normal file

@ -0,0 +1,103 @@
<?php
include "db_conn.php";
$id = $_GET["id"];
if (isset($_POST["submit"])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$sql = "UPDATE `crud` SET `first_name`='$first_name',`last_name`='$last_name',`email`='$email',`gender`='$gender' WHERE id = $id";
$result = mysqli_query($conn, $sql);
if ($result) {
header("Location: index.php?msg=Data updated successfully");
} else {
echo "Failed: " . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>PHP CRUD Application</title>
</head>
<body>
<nav class="navbar navbar-light justify-content-center fs-3 mb-5" style="background-color: #00ff5573;">
CRUD Application
</nav>
<div class="container">
<div class="text-center mb-4">
<h3>Edit User Information</h3>
<p class="text-muted">Click update after changing any information</p>
</div>
<?php
$sql = "SELECT * FROM `crud` WHERE id = $id LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
?>
<div class="container d-flex justify-content-center">
<form action="" method="post" style="width:50vw; min-width:300px;">
<div class="row mb-3">
<div class="col">
<label class="form-label">First Name:</label>
<input type="text" class="form-control" name="first_name" value="<?php echo $row['first_name'] ?>">
</div>
<div class="col">
<label class="form-label">Last Name:</label>
<input type="text" class="form-control" name="last_name" value="<?php echo $row['last_name'] ?>">
</div>
</div>
<div class="mb-3">
<label class="form-label">Email:</label>
<input type="email" class="form-control" name="email" value="<?php echo $row['email'] ?>">
</div>
<div class="form-group mb-3">
<label>Gender:</label>
&nbsp;
<input type="radio" class="form-check-input" name="gender" id="male" value="male" <?php echo ($row["gender"] == 'male') ? "checked" : ""; ?>>
<label for="male" class="form-input-label">Male</label>
&nbsp;
<input type="radio" class="form-check-input" name="gender" id="female" value="female" <?php echo ($row["gender"] == 'female') ? "checked" : ""; ?>>
<label for="female" class="form-input-label">Female</label>
</div>
<div>
<button type="submit" class="btn btn-success" name="submit">Update</button>
<a href="index.php" class="btn btn-danger">Cancel</a>
</div>
</form>
</div>
</div>
<!-- Bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>

79
index.php Normal file

@ -0,0 +1,79 @@
<?php
include "db_conn.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>PHP CRUD Application</title>
</head>
<body>
<nav class="navbar navbar-light justify-content-center fs-3 mb-5" style="background-color: #00ff5573;">
CRUD Application
</nav>
<div class="container">
<?php
if (isset($_GET["msg"])) {
$msg = $_GET["msg"];
echo '<div class="alert alert-warning alert-dismissible fade show" role="alert">
' . $msg . '
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
<a href="add-new.php" class="btn btn-dark mb-3">Add New</a>
<table class="table table-hover text-center">
<thead class="table-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
<th scope="col">Gender</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM `crud`";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["first_name"] ?></td>
<td><?php echo $row["last_name"] ?></td>
<td><?php echo $row["email"] ?></td>
<td><?php echo $row["gender"] ?></td>
<td>
<a href="edit.php?id=<?php echo $row["id"] ?>" class="link-dark"><i class="fa-solid fa-pen-to-square fs-5 me-3"></i></a>
<a href="delete.php?id=<?php echo $row["id"] ?>" class="link-dark"><i class="fa-solid fa-trash fs-5"></i></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<!-- Bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</body>
</html>