Skip to main content

Effortless CRUD Operations in PHP: Create, Edit, Display, and Delete on a Single Page



Let's Start with CRUD Operations in PHP:

INDEX.PHP

<?php
require("config.php");
$result = mysqli_query($con,"SELECT * FROM register");
if(isset($_GET['delete_id']))
{
$id = $_GET['delete_id'];
mysqli_query($con,"DELETE FROM register WHERE id='$id'");
echo "<script>window.location.href= 'index.php'</script>";
}
else if(isset($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$res = mysqli_query($con,"SELECT * FROM register WHERE id='$id'");

while($row = mysqli_fetch_array($res))
{
$name_tb = $row['name'];
$email_tb = $row['email'];
$image_tb = $row['image'];
}
}
else
{
$res = null;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Word System</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<style>
body{
float:left;
width:100%;
height:auto;
}
</style>
</head>
<body>
<table class="table table-stripped">
<tr>
<td>
<form style="width:80%;" method="post" action="" enctype="multipart/form-data">
<?php
if(!empty($res))
{
?>
<center>
<h3>Update Register</h3>
<div>
<label>Name</label>
<input required class="form-control" type="text" name="p_name" value="<?php echo $name_tb;?>"/>
</div>
<div>
<label>Email</label>
<input required class="form-control" type="email" name="p_email" value="<?php echo $email_tb;?>"/>
</div>
<div>
<label>Image</label>
<input class="form-control" type="file" name="imagee"/>
<?php echo $image_tb;?>
</div>
<div>
<label></label>
<input class="btn btn-info btn-sm" type="submit" name="update" value="Update"/>
</div>
</center>
<?php
}
else
{
?>
<center>
<h3>Register</h3>
<div>
<label>Name</label>
<input required class="form-control" type="text" name="name"/>
</div>
<div>
<label>Email</label>
<input required class="form-control" type="email" name="email"/>
</div>
<div>
<label>Image</label>
<input required class="form-control" type="file" name="image"/>
</div>
<div>
<label></label>
<input class="btn btn-info btn-sm" type="submit" name="register" value="Register"/>
</div>
</center>
<?php
}
?>
</form>
<?php
if(isset($_POST['register']))
{
$image = $_FILES['image']['name'];
$temp_name = $_FILES['image']['tmp_name'];
move_uploaded_file($temp_name,"img/$image");
$name = $_POST['name'];
$email = $_POST['email'];
$image = $image;
$create_date = date('Y-m-d');
mysqli_query($con,"INSERT INTO register VALUES ('','$name','$email','$image','$create_date')");
// echo "<script>alert('success.')</script>";
echo "<script>window.location.href= 'index.php'</script>";
}
else if(isset($_POST['update']))
{
if($_FILES['imagee']['name'])
{
$imagea = $_FILES['imagee']['name'];
$temp_name = $_FILES['imagee']['tmp_name'];
move_uploaded_file($temp_name,"img/$imagea");
}
else
{
$imagea = $image_tb;
}
$name = $_POST['p_name'];
$email = $_POST['p_email'];
$image = $imagea;
$create_date = date('Y-m-d');
mysqli_query($con,"UPDATE register SET name='$name', email='$email', image='$image', create_date='$create_date' WHERE id='$id'");
echo "<script>window.location.href= 'index.php'</script>";
}
?>
</td>
<td>
<center>
<table class="table table-stripped table-hover">
<tr>
<th>Image</th>
<th>Name</th>
<th>Email</th>
<th>Date</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td>
<img src="img/<?php echo $row['image'];?>" alt="<?php echo $row['image'];?>" style="width:80px;height:80px;border:1px solid lightgray;border-radius: 50%;"/>
</td>
<td>
<?php echo $row['name']."<br>";?>
<div id="actions">
<a href="index.php?edit_id=<?php echo $row['id'];?>"/>Edit</a>

<a href="index.php?delete_id=<?php echo $row['id'];?>">Delete</a>
</div>
</td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['create_date'];?></td>
</tr>
<?php
}
?>
</table>
</center>
</td>
</tr>
</table>
</body>
</html>




😃 Embrace the joy of coding!

Comments

Popular posts from this blog

JavaScript File Import and Export: A Comprehensive Guide

JavaScript, the language that powers the modern web, has evolved significantly over the years, and its capabilities extend beyond simple script tags in HTML. One of the most important features introduced in ECMAScript 6 (ES6) is the ability to modularize code using import and export statements. This article will dive into JavaScript file import and export, exploring how to effectively organize and share code across multiple files. Understanding Modules: Before ES6, JavaScript needed more built-in support for modules, making it challenging to manage large codebases. ES6 introduced the concept of modules, which allow developers to split their code into reusable and manageable chunks. Modules facilitate better code organization, encapsulation, and the reduction of global scope pollution. Exporting from a Module: In JavaScript, you can export variables, functions, classes, or even objects from a module to be used in other files. The export keyword is used to mark items for export. For exam...

Understanding Asynchronous JavaScript: Exploring async/await, Promises, and Callbacks

  Asynchronous programming is a fundamental aspect of JavaScript that allows you to execute code without blocking the main execution thread. It enables tasks like fetching data from APIs, reading files, or handling user interactions to be executed without freezing the entire application. In this article, we'll dive into the concepts of async/await, Promises, and callbacks – powerful tools that make managing asynchronous operations more elegant and efficient. Callbacks: The Traditional Approach Callbacks are the foundation of asynchronous programming in JavaScript. A callback is a function that is passed as an argument to another function. It's executed after the completion of an asynchronous operation. However, using callbacks can lead to callback hell – a situation where nested callbacks become hard to manage and read. Example: function fetchData(callback) {   setTimeout(() => {     const data = 'Hello, world!';     callback(data);   }, 1000); } fe...

Mastering the Basics of JavaScript: A Quick and Comprehensive Guide

JAVASCRIPT Javascript is a client-side programming language. It basically uses for changing the behavior of simple HTML at occurring an event by clicking, cursor hovering, and key pressing. Javascript writes in '.html' file, and '.php' file internally and externally with the extension of '.js'. In HTML, You can write javascript code anywhere but head tags (<head> javascript code </head>) are recommended. Note: * JQuery is a Javascript library that has pre-built functions of javascript. AJAX is a Javascript Framework that allowed changes in the data and behavior of a page without reloading. SYNTAX: '<script type="text/javascript">' (Open tag)  and   '</script>' (Close tag) DATATYPE: number (2, 2.5, 2.56e45) string ('java', "script", '5') boolean (true, false) object (all types of arrays) OPERATORS: Arithmetic Operators (+, -, *, /, %, **) Increment/Decrem...