Php and Mysql Insert View Edit Delete and Upload Picture

Multiple Image Upload With View, Edit And Delete In PHP Example

Hi guys,

Today i will explained how to Upload Multiple Images With View, Edit And Delete In PHP. This example is then easy to use in php. THis case to I am upload to the multiple images and add together,delete,prove ,and edit in image gallery in php.

This exampel To i am create a uploads/images folder and five files to addEdit.php,Db.class.php,index.php,postAction.php and view.php File create.

And then let's first to the example.

Index.php


          

<?php

session_start();

require_once 'DB.class.php';

$db = new DB();

$images = $db->getRows();

$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';

if(!empty($sessData['status']['msg'])){

$statusMsg = $sessData['status']['msg'];

$statusMsgType = $sessData['status']['type'];

unset($_SESSION['sessData']['status']);

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title></championship>

<meta charset="utf-8">

<meta proper name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/four.5.ii/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/iii.v.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/four.5.2/js/bootstrap.min.js"></script>

</head>

<body>

<div class="container">

<div grade="row">

<div class="col-md-12">

<?php if(!empty($statusMsg)){ ?>

<div form="col-xs-12">

<div class="alarm warning-<?php echo $statusMsgType; ?>"><?php repeat $statusMsg; ?></div>

</div>

<?php } ?>

<div course="row">

<div class="col-medico-12">

<!-- Add together link -->

<div class="float-right p-2">

<a href="addEdit.php" class="btn btn-success"><i class="plus"></i> New Gallery</a>

</div>

</div>

<div course="container">

<div grade="row">

<div class="col-medico-12">

<table class="table tabular array-striped tabular array-bordered">

<thead class="thead-dark">

<tr>

<th width="5%">#</thursday>

<th width="10%"></thursday>

<thursday width="40%">Title</th>

<th width="19%">Created</th>

<th width="half-dozen%">Condition</th>

<th width="twenty%">Action</thursday>

</tr>

</thead>

<tbody>

<?php

if(!empty($images)){

$i=0;

foreach($images as $row){

$i++;

$defaultImage = !empty($row['default_image'])?'<img style="width:100px;" src="uploads/images/'.$row['default_image'].'" alt="" />':'';

$statusLink = ($row['status'] == i)?'postAction.php?action_type=block&id='.$row['id']:'postAction.php?action_type=unblock&id='.$row['id'];

$statusTooltip = ($row['status'] == ane)?'Click to Inactive':'Click to Active';

?>

<tr>

<td><?php repeat $i; ?></td>

<td width="50px;"><?php echo $defaultImage; ?></td>

<td><?php echo $row['championship']; ?></td>

<td><?php echo $row['created']; ?></td>

<td><a href="<?php echo $statusLink; ?>" championship="<?php echo $statusTooltip; ?>"><span grade="badge <?php echo ($row['condition'] == one)?'badge-success':'badge-danger'; ?>"><?php echo ($row['status'] == 1)?'Agile':'Inactive'; ?></bridge></a></td>

<td>

<a href="view.php?id=<?php repeat $row['id']; ?>" class="btn btn-primary">view</a>

<a href="addEdit.php?id=<?php repeat $row['id']; ?>" class="btn btn-warning">edit</a>

<a href="postAction.php?action_type=delete&id=<?php echo $row['id']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure to delete data?')?truthful:fake;">delete</a>

</td>

</tr>

<?php } }else{ ?>

<tr><td colspan="6">No gallery institute...</td></tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

</div>

</div>

</div>

</div>

<script>

function deleteImage(id){

var result = confirm("Are you sure to delete?");

if(result){

$.post( "postAction.php", {action_type:"img_delete",id:id}, part(resp) {

if(resp == 'ok'){

$('#imgb_'+id).remove();

alert('The prototype has been removed from the gallery');

}else{

alert('Some problem occurred, please try once again.');

}

});

}

}

</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/three.4.1/jquery.min.js"></script>

</body>

</html>

DB.class.php

          

<?php

course DB{

private $dbHost = "localhost";

private $dbUsername = "root";

private $dbPassword = "root";

private $dbName = "login";

individual $galleryTbl = "gallery";

private $imgTbl = "gallery_images";

public function __construct(){

if(!isset($this->db)){

// Connect to the database

$conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);

if($conn->connect_error){

die("Failed to connect with MySQL: " . $conn->connect_error);

}else{

$this->db = $conn;

}

}

}

/*

* Returns rows from the database based on the conditions

* @param cord name of the table

* @param assortment select, where, order_by, limit and return_type conditions

*/

public role getRows($conditions = array()){

$sql = 'SELECT ';

$sql .= '*, (SELECT file_name FROM '.$this->imgTbl.' WHERE gallery_id = '.$this->galleryTbl.'.id ORDER BY id DESC LIMIT 1) every bit default_image';

$sql .= ' FROM '.$this->galleryTbl;

if(array_key_exists("where",$atmospheric condition)){

$sql .= ' WHERE ';

$i = 0;

foreach($conditions['where'] equally $key => $value){

$pre = ($i > 0)?' AND ':'';

$sql .= $pre.$key." = '".$value."'";

$i++;

}

}

if(array_key_exists("order_by",$conditions)){

$sql .= ' ORDER By '.$conditions['order_by'];

}else{

$sql .= ' ORDER BY id DESC ';

}

if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){

$sql .= ' LIMIT '.$conditions['beginning'].','.$atmospheric condition['limit'];

}elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){

$sql .= ' LIMIT '.$atmospheric condition['limit'];

}

$result = $this->db->query($sql);

if(array_key_exists("return_type",$conditions) && $atmospheric condition['return_type'] != 'all'){

switch($weather['return_type']){

case 'count':

$information = $upshot->num_rows;

pause;

case 'single':

$data = $issue->fetch_assoc();

if(!empty($information)){

$sql = 'SELECT * FROM '.$this->imgTbl.' WHERE gallery_id = '.$data['id'];

$issue = $this->db->query($sql);

$imgData = array();

if($result->num_rows > 0){

while($row = $result->fetch_assoc()){

$imgData[] = $row;

}

}

$information['images'] = $imgData;

}

intermission;

default:

$information = '';

}

}else{

if($result->num_rows > 0){

while($row = $result->fetch_assoc()){

$data[] = $row;

}

}

}

return !empty($data)?$data:false;

}

public part getImgRow($id){

$sql = 'SELECT * FROM '.$this->imgTbl.' WHERE id = '.$id;

$result = $this->db->query($sql);

render ($upshot->num_rows > 0)?$outcome->fetch_assoc():false;

}

/*

* Insert data into the database

* @param string name of the table

* @param assortment the data for inserting into the table

*/

public function insert($data){

if(!empty($data) && is_array($information)){

$columns = '';

$values = '';

$i = 0;

if(!array_key_exists('created',$data)){

$data['created'] = date("Y-m-d H:i:s");

}

if(!array_key_exists('modified',$data)){

$data['modified'] = date("Y-m-d H:i:s");

}

foreach($data as $central=>$val){

$pre = ($i > 0)?', ':'';

$columns .= $pre.$key;

$values .= $pre."'".$this->db->real_escape_string($val)."'";

$i++;

}

$query = "INSERT INTO ".$this->galleryTbl." (".$columns.") VALUES (".$values.")";

$insert = $this->db->query($query);

return $insert?$this->db->insert_id:false;

}else{

return simulated;

}

}

public function insertImage($information){

if(!empty($data) && is_array($data)){

$columns = '';

$values = '';

$i = 0;

if(!array_key_exists('uploaded_on',$data)){

$information['uploaded_on'] = date("Y-thousand-d H:i:s");

}

foreach($data every bit $key=>$val){

$pre = ($i > 0)?', ':'';

$columns .= $pre.$key;

$values .= $pre."'".$this->db->real_escape_string($val)."'";

$i++;

}

$query = "INSERT INTO ".$this->imgTbl." (".$columns.") VALUES (".$values.")";

$insert = $this->db->query($query);

return $insert?$this->db->insert_id:imitation;

}else{

return simulated;

}

}

/*

* Update data into the database

* @param string proper name of the tabular array

* @param array the data for updating into the table

* @param array where condition on updating data

*/

public role update($data, $conditions){

if(!empty($data) && is_array($data)){

$colvalSet = '';

$whereSql = '';

$i = 0;

if(!array_key_exists('modified',$data)){

$data['modified'] = date("Y-m-d H:i:s");

}

foreach($data as $central=>$val){

$pre = ($i > 0)?', ':'';

$colvalSet .= $pre.$cardinal."='".$this->db->real_escape_string($val)."'";

$i++;

}

if(!empty($conditions)&& is_array($weather condition)){

$whereSql .= ' WHERE ';

$i = 0;

foreach($conditions every bit $key => $value){

$pre = ($i > 0)?' AND ':'';

$whereSql .= $pre.$key." = '".$value."'";

$i++;

}

}

$query = "UPDATE ".$this->galleryTbl." SET ".$colvalSet.$whereSql;

$update = $this->db->query($query);

render $update?$this->db->affected_rows:false;

}else{

return false;

}

}

/*

* Delete data from the database

* @param string name of the table

* @param array where status on deleting information

*/

public function delete($conditions){

$whereSql = '';

if(!empty($conditions)&& is_array($conditions)){

$whereSql .= ' WHERE ';

$i = 0;

foreach($weather as $central => $value){

$pre = ($i > 0)?' AND ':'';

$whereSql .= $pre.$key." = '".$value."'";

$i++;

}

}

$query = "DELETE FROM ".$this->galleryTbl.$whereSql;

$delete = $this->db->query($query);

render $delete?truthful:false;

}

public function deleteImage($conditions){

$whereSql = '';

if(!empty($conditions)&& is_array($conditions)){

$whereSql .= ' WHERE ';

$i = 0;

foreach($conditions as $key => $value){

$pre = ($i > 0)?' AND ':'';

$whereSql .= $pre.$key." = '".$value."'";

$i++;

}

}

$query = "DELETE FROM ".$this->imgTbl.$whereSql;

$delete = $this->db->query($query);

return $delete?true:false;

}

}

?>

addEdit.php

          

<?php

session_start();

$postData = $galData = array();

$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';

if(!empty($sessData['status']['msg'])){

$statusMsg = $sessData['status']['msg'];

$statusMsgType = $sessData['condition']['blazon'];

unset($_SESSION['sessData']['condition']);

}

if(!empty($sessData['postData'])){

$postData = $sessData['postData'];

unset($_SESSION['sessData']['postData']);

}

if(!empty($_GET['id'])){

// Include and initialize DB class

require_once 'DB.class.php';

$db = new DB();

$atmospheric condition['where'] = array(

'id' => $_GET['id'],

);

$conditions['return_type'] = 'single';

$galData = $db->getRows($conditions);

}

$galData = !empty($postData)?$postData:$galData;

$actionLabel = !empty($_GET['id'])?'Edit':'Add';

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<div form="container">

<h1><?php echo $actionLabel; ?> Gallery</h1>

<hr>

<?php if(!empty($statusMsg)){ ?>

<div grade="col-xs-12">

<div class="alert warning-<?php echo $statusMsgType; ?>"><?php repeat $statusMsg; ?></div>

</div>

<?php } ?>

<div class="row">

<div form="col-md-6">

<form method="mail" action="postAction.php" enctype="multipart/form-data">

<div course="form-group">

<characterization>Images:</label>

<input blazon="file" proper name="images[]" class="course-command" multiple>

<?php if(!empty($galData['images'])){ ?>

<div class="gallery-img">

<?php foreach($galData['images'] every bit $imgRow){ ?>

<div class="img-box" id="imgb_<?php echo $imgRow['id']; ?>">

<img src="uploads/images/<?php repeat $imgRow['file_name']; ?>">

<a href="javascript:void(0);" class="bluecoat badge-danger" onclick="deleteImage('<?php echo $imgRow['id']; ?>')">delete</a>

</div>

<?php } ?>

</div>

<?php } ?>

</div>

<div class="form-group">

<label>Title:</label>

<input type="text" proper name="title" class="form-control" placeholder="Enter title" value="<?php echo !empty($galData['title'])?$galData['title']:''; ?>" >

</div>

<a href="index.php" form="btn btn-secondary">Dorsum</a>

<input blazon="hidden" name="id" value="<?php repeat !empty($galData['id'])?$galData['id']:''; ?>">

<input blazon="submit" name="imgSubmit" course="btn btn-success" value="SUBMIT">

</course>

</div>

</div>

</div>

</body>

</html>

postAction.php

          

<?php

session_start();

require_once 'DB.class.php';

$db = new DB();

$uploadDir = "uploads/images/";

$allowTypes = array('jpg','png','jpeg','gif');

$redirectURL = 'index.php';

$statusMsg = $errorMsg = '';

$sessData = array();

$statusType = 'danger';

if(isset($_POST['imgSubmit'])){

$redirectURL = 'addEdit.php';

$title = $_POST['title'];

$id = $_POST['id'];

$galData = assortment(

'championship' => $title

);

$sessData['postData'] = $galData;

$sessData['postData']['id'] = $id;

$idStr = !empty($id)?'?id='.$id:'';

if(empty($championship)){

$fault = '<br/>Enter the gallery title.';

}

if(!empty($error)){

$statusMsg = 'Please fill all the mandatory fields.'.$mistake;

}else{

if(!empty($id)){

$condition = array('id' => $id);

$update = $db->update($galData, $condition);

$galleryID = $id;

}else{

$insert = $db->insert($galData);

$galleryID = $insert;

}

$fileImages = array_filter($_FILES['images']['name']);

if(!empty($galleryID)){

if(!empty($fileImages)){

foreach($fileImages as $key=>$val){

$fileName = $galleryID.'_'.basename($fileImages[$primal]);

$targetFilePath = $uploadDir . $fileName;

$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(in_array($fileType, $allowTypes)){

if(move_uploaded_file($_FILES["images"]["tmp_name"][$key], $targetFilePath)){

$imgData = array(

'gallery_id' => $galleryID,

'file_name' => $fileName

);

$insert = $db->insertImage($imgData);

}else{

$errorUpload .= $fileImages[$cardinal].' | ';

}

}else{

$errorUploadType .= $fileImages[$key].' | ';

}

}

$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):'';

$errorUploadType = !empty($errorUploadType)?'File Type Mistake: '.trim($errorUploadType, ' | '):'';

$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;

}

$statusType = 'success';

$statusMsg = 'Gallery images has been uploaded successfully.'.$errorMsg;

$sessData['postData'] = '';

$redirectURL = 'index.php';

}else{

$statusMsg = 'Some problem occurred, delight effort once again.';

$redirectURL .= $idStr;

}

}

$sessData['status']['type'] = $statusType;

$sessData['status']['msg'] = $statusMsg;

}elseif(($_REQUEST['action_type'] == 'cake') && !empty($_GET['id'])){

$galData = array('status' => 0);

$status = array('id' => $_GET['id']);

$update = $db->update($galData, $condition);

if($update){

$statusType = 'success';

$statusMsg = 'Gallery data has been blocked successfully.';

}else{

$statusMsg = 'Some problem occurred, please try again.';

}

$sessData['status']['type'] = $statusType;

$sessData['status']['msg'] = $statusMsg;

}elseif(($_REQUEST['action_type'] == 'unblock') && !empty($_GET['id'])){

$galData = assortment('status' => ane);

$condition = array('id' => $_GET['id']);

$update = $db->update($galData, $status);

if($update){

$statusType = 'success';

$statusMsg = 'Gallery information has been activated successfully.';

}else{

$statusMsg = 'Some problem occurred, please attempt over again.';

}

$sessData['status']['blazon'] = $statusType;

$sessData['condition']['msg'] = $statusMsg;

}elseif(($_REQUEST['action_type'] == 'delete') && !empty($_GET['id'])){

$conditions['where'] = array(

'id' => $_GET['id'],

);

$atmospheric condition['return_type'] = 'single';

$prevData = $db->getRows($conditions);

$condition = assortment('id' => $_GET['id']);

$delete = $db->delete($condition);

if($delete){

$condition = array('gallery_id' => $_GET['id']);

$delete = $db->deleteImage($status);

if(!empty($prevData['images'])){

foreach($prevData['images'] as $img){

@unlink($uploadDir.$img['file_name']);

}

}

$statusType = 'success';

$statusMsg = 'Gallery has been deleted successfully.';

}else{

$statusMsg = 'Some problem occurred, please try again.';

}

$sessData['condition']['type'] = $statusType;

$sessData['status']['msg'] = $statusMsg;

}elseif(($_POST['action_type'] == 'img_delete') && !empty($_POST['id'])){

$prevData = $db->getImgRow($_POST['id']);

$condition = array('id' => $_POST['id']);

$delete = $db->deleteImage($condition);

if($delete){

@unlink($uploadDir.$prevData['file_name']);

$status = 'ok';

}else{

$status = 'err';

}

repeat $status;die;

}

$_SESSION['sessData'] = $sessData;

header("Location: ".$redirectURL);

leave();

?>

view.php

          

<?php

if(empty($_GET['id'])){

header("Location: manage.php");

}

require_once 'DB.grade.php';

$db = new DB();

$weather condition['where'] = assortment(

'id' => $_GET['id'],

);

$conditions['return_type'] = 'unmarried';

$galData = $db->getRows($conditions);

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-eight">

<title></championship>

</head>

<body>

<div class="row">

<div class="col-md-12">

<h5><?php repeat !empty($galData['championship'])?$galData['title']:''; ?></h5>

<?php if(!empty($galData['images'])){ ?>

<div form="gallery-img">

<?php foreach($galData['images'] every bit $imgRow){ ?>

<div form="img-box" id="imgb_<?php echo $imgRow['id']; ?>">

<img src="uploads/images/<?php repeat $imgRow['file_name']; ?>">

<a href="javascript:void(0);" class="badge badge-danger" onclick="deleteImage('<?php echo $imgRow['id']; ?>')">delete</a>

</div>

<?php } ?>

</div>

<?php } ?>

</div>

<a href="alphabetize.php" class="btn btn-chief">Dorsum to List</a>

</div>

</body>

</html>

Now you can check your own.

I hope it can help you...

Output :

clarkvichill.blogspot.com

Source: https://www.nicesnippets.com/blog/multiple-image-upload-with-view-edit-and-delete-in-php-example

0 Response to "Php and Mysql Insert View Edit Delete and Upload Picture"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel