PHP: MULTIPLE IMAGE [[ UPLOAD UPDATE DELETE UNLINK TABLE JOINING SESSION ARRAY_METHODS HEADER ]]
connection.php
<?php
$servername="localhost";
$username="root";
$password="";
$db="brainiuml9";
$conn=mysqli_connect($servername,$username,$password,$db);
//print_r($conn);
?>
deledu.php
<?php
session_start();
include("connection.php");
$deleduid=$_REQUEST['deleduid'];
$sql2= "SELECT distinct users.education FROM users left join education on users.education = education.ed_id";
$data2=mysqli_query($conn,$sql2);
$usg_ed_id=array();
while($row = mysqli_fetch_array($data2))
{
array_push($usg_ed_id,$row['education']);
}
// print_r($usg_ed_id);
// print($deleduid);
if( in_array($deleduid,$usg_ed_id)){
$_SESSION["notedudeleted"]="Education field using, Cannot deleted.";
header("location:display1.php");
}else{
$sql = "DELETE FROM `education` WHERE `ed_id`='$deleduid'";
$data=mysqli_query($conn,$sql);
if($data){
$_SESSION["edudeleted"]="Education deleted successfully";
header("location:display1.php");
}else{
echo "Not updated";
}
}
?>
deleteaction.php
<?php
session_start();
include("connection.php");
$id= $_REQUEST['del'];
// delete previous images
$sql2 = "SELECT * FROM `users` WHERE `id`='$id'";
$data2=mysqli_query($conn,$sql2);
$row2=mysqli_fetch_assoc($data2);
$arr = explode(", ",$row2['image']);
foreach ($arr as $value) {
unlink('image/'.$value);
}
$sql = "DELETE FROM `users` WHERE `id`='$id'";
$data=mysqli_query($conn,$sql);
if($data){
$_SESSION["deleted"]="Data deleted successfully";
header("location:display1.php");
}else{
echo "Not updated";
}
?>
delsingleimg.php
<?php
session_start();
include("connection.php");
$delImgName=$_REQUEST['delimg'];
// echo $delImgName;
$eid=$_REQUEST['eid'];
// echo $eid;
$sql2 = "SELECT * FROM `users` WHERE `id`='$eid'";
$data2=mysqli_query($conn,$sql2);
$row2=mysqli_fetch_assoc($data2);
$arr = explode(", ",$row2['image']);
$fold=array();
foreach ($arr as $value) {
if($delImgName == $value)
unlink('image/'.$value);
else
array_push($fold,$value);
}
$fold=implode(', ',$fold);
$sql2 = "UPDATE `users` SET `image`='$fold' WHERE `id`='$eid'";
$data2=mysqli_query($conn,$sql2);
header('location:display1.php?eid='.$eid);
?>
display1.php
<?php
session_start();
include("connection.php");
if(isset($_REQUEST['eid'])){
$id = $_REQUEST['eid'];
// <!-- Register -->
$sql = "SELECT * FROM users INNER JOIN education on users.education = education.ed_id where id='$id'";
$data = mysqli_query($conn,$sql);
$res=mysqli_fetch_assoc($data);
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>DATE: 5/9/22</title>
<!-- -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<!-- -->
<!-- -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
<!-- -->
</head>
<style>
img:hover{
height:130px;
width:240px;
}
</style>
<body
style=" font-family: 'Julius Sans One', sans-serif; background:linear-gradient(#b6c3e8,#1d803a99); background-size: 100vw 100vh; background-repeat: no-repeat; background-attachment: fixed; ">
<!-- <marquee behavior="scroll" direction="right"><h6>Sk SAIFUDDIN</h6></marquee> -->
<br>
<div class="container">
<div class="row">
<div class="col-sm-3 col-md-6 col-lg-4 ">
<?php
if(isset($_SESSION["updated"])){
echo "<script>swal({title: 'Updated!', text: 'Data Updated Successfully!',icon: 'success',button: 'Aww yiss!',}); </script>";
unset($_SESSION['updated']);
}
if(isset($_SESSION["eduinserted"])){
echo "<script>swal({title: 'Inserted!', text: 'Education Inserted Successfully!',icon: 'success',button: 'Aww yiss!',}); </script>";
unset($_SESSION['eduinserted']);
}
if(isset($_SESSION["inserted"])){
echo "<script>swal({title: 'Inserted!', text: 'Data Inserted Successfully!',icon: 'success',button: 'Aww yiss!',}); </script>";
unset($_SESSION['inserted']);
}
if(isset($_SESSION["deleted"])){
echo "<script>swal({title: 'Deleted!', text: 'Data Deleted Successfully!',icon: 'success',button: 'Aww yiss!',}); </script>";
unset($_SESSION['deleted']);
}
if(isset($_SESSION["edudeleted"])){
echo "<script>swal({title: 'Deleted!', text:' ".$_SESSION['edudeleted']."',icon: 'success',button: 'Aww yiss!',}); </script>";
unset($_SESSION['edudeleted']);
}
if(isset($_SESSION["notedudeleted"])){
echo "<script>swal({title: 'Sorry!', text:' ".$_SESSION['notedudeleted']."',icon: 'error',button: 'Aww yiss!',}); </script>";
unset($_SESSION['notedudeleted']);
}
?>
<?php if(isset($_REQUEST['eid'])){?>
<h3 class="fw-bolder"> Update </h3>
<!-- Register -->
<!-- edit -->
<form method="post" action="formeditaction.php" enctype="multipart/form-data">
<div class="mb-2">
<input type="hidden" value="<?php echo $res['id']?>" name="id">
<label for="" class="form-label">Name</label>
<input type="text" class="form-control" name="username" value="<?php echo $res['username']; ?>">
</div>
<div class="mb-2">
<label for="" class="form-label">Email address</label>
<input type="email" name="email" class="form-control" value="<?php echo $res['email']; ?>">
</div>
<div class="mb-2">
<label class="form-label">Address</label>
<input type="text" name="address" class="form-control" value="<?php echo $res['address']; ?>">
</div>
<div class="mb-2">
<label class="form-label">Education</label>
<select name="education[]" id="education" multiple="multiple" class="form-control js-example-basic-multiple">
<?php
$ed=explode(', ',$res['education']);
$AQL="SELECT * from education";
$ras=$conn->query($AQL);
while($res2=$ras->fetch_assoc()){
?>
<option value="<?php echo $res2['ed_id']; ?>" <?php
foreach($ed as $val) { if($val==$res2['ed_id']){echo 'selected';} } ?>><?php echo $res2['title'] ?></option>
<?php
}
?>
</select>
</div>
<script>
$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});
</script>
<div class="mb-2">
<label class="form-label">Gender</label>
<input type="radio" name="gender" value="male"
<?php if($res['gender']=='male'){echo "checked";} ?>>Male
<input type="radio" name="gender" value="female"
<?php if($res['gender']=='female'){echo "checked";} ?>>Female
<input type="radio" name="gender" value="others"
<?php if($res['gender']=='others'){echo "checked";} ?>>Others
</div>
<div class="mb-2">
<label class="form-label">Language</label>
<?php
$langArray = explode(",",$res['language']);
?>
<input type="checkbox" name="language[]" value="bengali"
<?php if(in_array("bengali",$langArray)){echo "checked";} ?>>Bengali
<input type="checkbox" name="language[]" value="english"
<?php if(in_array("english",$langArray)){echo "checked";} ?>>English
<input type="checkbox" name="language[]" value="hindi"
<?php if(in_array("hindi",$langArray)){echo "checked";} ?>>Hindi
</div>
<div class="mb-2">
<label class="form-label">Browse Image</label>
<!-- <?php
// $arr = explode(", ",$res['image']);
// $_SESSION["old_img1"] = $arr[0];
// $_SESSION["old_img2"] = $arr[1]; ?> -->
<div class="h-25 w-75">
<label class="form-label">Image</label>
<input type="file" name="uploadfile[]" multiple>
<?php $arr = explode(", ",$res['image']);
$_SESSION["old_imgs"] = $res['image'];
foreach ($arr as $value) {
$jsdata=array(0=>$value);
json_encode($jsdata);
if ($value!='')
echo "<div class='d-inline-block my-2' id='imgtagval'>
<img src='image/$value' width='60%'>
<a href='delsingleimg.php?delimg=$value&eid=$id' class='text-danger p-2'> <i class='fa-solid fa-trash-can'></i></a>
</div> ";
} ?>
</div>
<script>
function imgupdel(imgmyDiv){
// console.log(imgmyDiv); //value of img
}
</script>
</div>
<div align="right" class="mb-2">
<button type="submit" class="btn btn-primary">Update</button>
<a class="btn btn-warning" href="display1.php">Save New</a>
</div>
</form>
<?php }else{ ?>
<h3 class="fw-bolder"> Register </h3>
<form method="post" action="forminsertaction.php" enctype="multipart/form-data">
<div class="mb-2 form-group">
<label for="" class="form-label">Name</label>
<input type="text" class="input-sm form-control" name="username">
</div>
<div class="mb-2 form-group">
<label for="" class="form-label">Email address</label>
<input type="email" name="email" class="form-control">
</div>
<div class="mb-2 form-group">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="mb-2 form-group">
<label class="form-label">Address</label>
<input type="text" name="address" class="form-control">
</div>
<div class="mb-2 form-group">
<label class="form-label">Education</label>
<select name="education[]" id="education" multiple="multiple" class="form-control js-example-basic-multiple">
<option>Select</option>
<?php
$sqledu = "SELECT * from education";
$dataedu = mysqli_query($conn,$sqledu);
while($res = mysqli_fetch_assoc($dataedu)){
echo "<option value='".$res['ed_id']."'>".$res['title']."</option>" ;
}
?>
</select>
</div>
<script>
$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});
</script>
<div class="mb-2 form-group">
<div class="input-group">
<input type="text" name="eduadd" id="eduadd" class="form-control" placeholder="Add education here">
<span class="input-group-text btn btn-info" onclick="myFunction()">Add </span>
<span class="input-group-text btn btn-success" data-bs-toggle="offcanvas" data-bs-target="#demo">Show</span>
</div>
<!-- offcanvas -->
<div class="offcanvas offcanvas-start" id="demo">
<div class="offcanvas-header">
<h1 class="offcanvas-title">List of Educations</h1>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas"></button>
</div>
<div class="offcanvas-body">
<?php
$dataedu = mysqli_query($conn,$sqledu);
while($res = mysqli_fetch_assoc($dataedu)){ ?>
<div class="container list-group-item list-group-item-action">
<h4 class="fw-bolder text-end"><?php echo $res['title']; ?>
<a href="deledu.php?deleduid=<?php echo $res['ed_id']; ?>" class="btn btn-danger my-2"> <i class="fa-solid fa-trash-can"></i> </a>
</h4>
</div>
<?php } ?>
</div>
</div>
<!-- offcanvas ends -->
</div>
<div class="mb-2 form-group">
<label class="form-label">Gender:</label>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="others">Others
</div>
<div class="mb-2 form-group">
<label class="form-label">Language</label><br>
<input type="checkbox" name="language[]" id="gender" value="bengali">Bengali
<input type="checkbox" name="language[]" id="gender" value="english">English
<input type="checkbox" name="language[]" id="gender" value="hindi">Hindi
</div>
<div class="mb-2 form-group">
<label class="form-label">Add Images</label>
<input type="file" name="uploadfile[]" multiple>
</div>
<div align="right" class="mb-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<!-- Register ends -->
<?php } ?>
</div>
<div class="col-sm-9 col-md-6 col-lg-8 mt-3">
<h3 class="text-center fw-bolder"> Details of Users </h3>
<div class="mb-2 table-responsive">
<table class="table align-middle table-hover table-sm" id="table_id">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Address</th>
<th scope="col">Email</th>
<th scope="col">Education</th>
<th scope="col">Gender</th>
<th scope="col">Language</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM users INNER JOIN education on users.education = education.ed_id";
$data = mysqli_query($conn, $sql);
$c=1;
while($res = mysqli_fetch_assoc($data)){
$ed= explode(", ", $res['education']) ;
?>
<tr>
<th scope="row"><?php echo $c++; ?></th>
<td scope="col">
<?php $arr = explode(", ",$res['image']);
foreach ($arr as $value) {
if ($value!='')
echo " <img src='image/$value' width='45%' class='dispImg'>";
} ?>
</td>
<td scope="col"> <?php echo ucfirst($res['username']); ?></td>
<td scope="col"> <?php echo ucfirst($res['address']); ?> </td>
<td scope="col"><?php echo ucfirst($res['email']); ?></td>
<td scope="col">
<?php
$sql2= "SELECT * FROM education";
$res2 = mysqli_query($conn, $sql2);
while($row2 = mysqli_fetch_assoc($res2)){
foreach($ed as $val){
if($val == $row2['ed_id']){
echo ucfirst(strtolower($row2['title'])." ");
}
}
}
?>
</td>
<td scope="col"><?php echo ucfirst($res['gender']); ?></td>
<td scope="col"><?php echo ucfirst($res['language']); ?></td>
<td>
<a href="display1.php?eid=<?php echo $res['id'] ?>" class='text-info'> <i class="fa-solid fa-pen"></i> </a>
<a href="deleteaction.php?del=<?php echo $res['id']?>" class='text-danger'> <i class="fa-solid fa-trash-can"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
$(document).ready( function () {
$('#table_id').DataTable();
} );
</script>
<!-- display ends -->
</div>
</div>
</div>
</div>
<script src = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity = "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin = "anonymous"></script>
<script>
function myFunction(){
var title = document.getElementById("eduadd").value;
if (title!=""){
console.log("DATA ADDED ",title);
fetch("eduadd.php", {
method:"post",
headers:{
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
body:`title=${title}`,
}).then((response)=>{
window.location.href = 'http://localhost/Laravel/7sep/type1/display1.php';
});
console.log("REFRESH")
}
}
</script>
</body>
</html>
eduadd.php
<?php
session_start();
include("connection.php");
$title=$_REQUEST['title'];
$sql = "INSERT INTO `education`( `title`) VALUES ('$title')";
$data=mysqli_query($conn, $sql);
if($data)
$_SESSION["eduinserted"]="Education inserted successfully";
else
echo "not inserted";
?>
formeditaction.php
<?php
session_start();
include("connection.php");
$id= $_REQUEST['id'];
$name =$_REQUEST['username'];
$address =$_REQUEST['address'];
$email =$_REQUEST['email'];
$education=implode(', ',$_REQUEST['education']);
$gender =$_REQUEST['gender'];
$language= implode(",",$_REQUEST['language']);
// multiple images
$fold=array();
$extension=array('jpeg','jpg','png','gif');
if($_FILES['uploadfile']['tmp_name'][0]==''){
// echo gettype($fold);
$fold= explode(', ',$_SESSION["old_imgs"]);
// echo gettype($fold);
}
else{
// echo count($_FILES['uploadfile']['tmp_name']);
foreach ($_FILES['uploadfile']['tmp_name'] as $key => $value) {
$filename=$_FILES['uploadfile']['name'][$key];
$filename_tmp=$_FILES['uploadfile']['tmp_name'][$key];
echo '<br>';
$ext=pathinfo($filename,PATHINFO_EXTENSION);
$finalimg='';
if(in_array($ext,$extension)) {
if(!file_exists('image/'.$filename)){
move_uploaded_file($filename_tmp, 'image/'.$filename);
$finalimg=$filename;
}else{
$filename=str_replace('.','-',basename($filename,$ext));
$newfilename=$filename.time().".".$ext;
move_uploaded_file($filename_tmp, 'image/'.$newfilename);
$finalimg=$newfilename;
}
array_push($fold,$finalimg);
}else {
//display error
}
}
array_push($fold,$_SESSION["old_imgs"]);
}
// echo gettype($fold);
$fold= implode(", ",$fold);
// echo $fold;
$sql = "UPDATE `users` SET `username`='$name',`email`='$email',`address`='$address',`language`='$language',`gender`='$gender',`education`='$education',`image`='$fold' WHERE `id`='$id'";
$data=mysqli_query($conn,$sql);
if($data){
$_SESSION["updated"]="Data updated successfully";
header("location:display1.php");
}else{
echo "Not updated";
}
?>
forminsertaction.php
<?php
session_start();
include("connection.php");
$name=$_REQUEST['username'];
$address=$_REQUEST['address'];
$email=$_REQUEST['email'];
$password=md5($_REQUEST['password']);
$gender=$_REQUEST['gender'];
// echo $_REQUEST['education'];
$education=implode(', ',$_REQUEST['education']);
$language= implode(",",$_REQUEST['language']);
// multiple images
$fold=array();
$extension=array('jpeg','jpg','png','gif');
foreach ($_FILES['uploadfile']['tmp_name'] as $key => $value) {
$filename=$_FILES['uploadfile']['name'][$key];
$filename_tmp=$_FILES['uploadfile']['tmp_name'][$key];
echo '<br>';
$ext=pathinfo($filename,PATHINFO_EXTENSION);
$finalimg='';
if(in_array($ext,$extension))
{
if(!file_exists('image/'.$filename))
{
move_uploaded_file($filename_tmp, 'image/'.$filename);
$finalimg=$filename;
}else
{
$filename=str_replace('.','-',basename($filename,$ext));
$newfilename=$filename.time().".".$ext;
move_uploaded_file($filename_tmp, 'image/'.$newfilename);
$finalimg=$newfilename;
}
array_push($fold,$finalimg);
}else
{
//display error
}
}
$fold= implode(", ",$fold);
// echo $fold;
$sql = "INSERT INTO `users`( `username`, `email`, `password`, `address`, `language`, `gender`, `education`, `image`) VALUES ('$name','$email','$password','$address','$language','$gender','$education','$fold')";
$data=mysqli_query($conn, $sql);
// echo $fold." ";
if($data){
$_SESSION["inserted"]="Data inserted successfully";
header("location:display1.php");
}
else
echo "not inserted";
?>
Comments
Post a Comment