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
Post a Comment