Image update

How to update image using php and Mysql

Source code

config.php

								
									<?php 
									// DB credentials.
									define('DB_HOST','localhost');
									define('DB_USER','root');
									define('DB_PASS','');
									define('DB_NAME','myimage');
									// Establish database connection.
									try
									{
										$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
									}
									catch (PDOException $e)
									{
										exit("Error: " . $e->getMessage());
									}

									$conn = mysqli_connect('localhost', 'root', '', 'mydata');
									?>
								
							

index.php

								
									<?php
									session_start();
									error_reporting(0);
									include "config.php";
									// Uploads files
									if(isset($_POST['save'])) { // if save button on the form is clicked
									  $imagename=$_FILES["imagename"]["name"];
									  move_uploaded_file($_FILES["imagename"]["tmp_name"],"media/".$_FILES["imagename"]["name"]);
									  $sql = "INSERT INTO imageupload (filename) VALUES ('$imagename')";
									  if (mysqli_query($conn, $sql)) 
									  {
									    echo "<script>alert('Image uploaded successfully');</script>";
									  }
									  else {
									    echo "<script>alert('Failed to upload image.');<</script>";
									  }
									}
									?>

									<!DOCTYPE HTML>
									<html lang="en">
									<head>
									 <meta charset="utf-8">
									 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
									 <title>How to update images in Mysql and php</title>
									 <!--Bootstrap -->
									 <link rel="stylesheet" href="css/bootstrap.css" type="text/css">
									</head>
									<body>
									  <div class="container">
									    <h3>How to update images in Mysql and php</h3>
									    <form class="form-horizontal row-fluid" name="insertproduct" method="post" enctype="multipart/form-data">
									      <div class="control-group"> 
									        <label class="control-label" for="basicinput">Upload Image</label>
									        <div class="controls">
									          <input type="file" name="imagename" id="imagename" value="" class="span8 tip" required>
									        </div>
									      </div>
									      <div class="form-group row pt-3">
									        <div class="col-12">
									          <button type="submit" class="btn btn-success" name="save">
									            <i class="fa fa-plus "></i> Upload
									          </button>
									        </div>
									      </div>
									    </form>

									    <table id="" class="table table-bordered">
									      <thead>
									        <tr>
									          <th>ID</th>
									          <th>Image</th>
									          <th>Image Name</th>
									          <th>Action</th>
									        </tr>
									      </thead>

									      <tbody>

									        <?php 
									        $sql = "SELECT * from imageupload  order by id desc";
									        $query = $dbh -> prepare($sql);
									        $query->execute();
									        $results=$query->fetchAll(PDO::FETCH_OBJ);
									        $cnt=1;
									        if($query->rowCount() > 0)
									        {
									          foreach($results as $result)
									          {
									            ?>
									            <tr>
									              <td><?php echo $cnt;?></td>
									              <td><img class="" src="media/<?php  echo $result->filename;?>" alt="Image" width="100" height="80"></td>
									              <td><?php echo ($result->filename) ?></td>
									              <td class="text-center"><a href="update.php?imageid=<?php echo ($result->id) ?>" class="btn btn-primary">Update</a></td>
									            </tr>
									            <?php
									            $cnt=$cnt+1;
									          }
									        } ?>
									      </tbody>
									    </table>
									  </div>
									</body>
									</html>
								
							

update.php

								
									<?php
									session_start();
									error_reporting(0);
									include "config.php";
									// Uploads files
									$imgid=intval($_GET['imageid']);
									if(isset($_POST['submit'])) { // if save button on the form is clicked
										$aimage=$_FILES["imagename"]["name"];
										move_uploaded_file($_FILES["imagename"]["tmp_name"],"media/".$_FILES["imagename"]["name"]);
										$sql="update imageupload set filename=:aimage where id=:imgid";
										$query = $dbh->prepare($sql);
										$query->bindParam(':imgid',$imgid,PDO::PARAM_STR);
										$query->bindParam(':aimage',$aimage,PDO::PARAM_STR);
										$query->execute();
										if( $query->execute()){
											echo '<script>alert(" image has been Updated.")</script>';

										}else{
											echo '<<script>alert("Something went wrong please try again.")</script>';
										}
									}
									?>

									<!DOCTYPE HTML>
									<html lang="en">
									<head>
										<meta charset="utf-8">
										<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
										<title>Image Upload</title>
										<!--Bootstrap -->
										<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
									</head>
									<body>
										<div class="container">
											<div class="card col-md-8">
												<form class="form-horizontal" name="image" method="post" enctype="multipart/form-data">
													<?php 
													$imgid=intval($_GET['imageid']);
													$sql = "SELECT * from imageupload where id=:imgid";
													$query = $dbh -> prepare($sql);
													$query -> bindParam(':imgid', $imgid, PDO::PARAM_STR);
													$query->execute();
													$results=$query->fetchAll(PDO::FETCH_OBJ);
													$cnt=1;
													if($query->rowCount() > 0)
													{
														foreach($results as $result)
														{ 
															?> 
															<div class="form-group ml-4">
																<label for="focusedinput" class=" control-label">Current Image </label>
																<div class="">
																	<img src="media/<?php  echo $result->filename;?>" width="200">
																</div>
															</div>

															<div class="form-group ml-4">
																<label for="focusedinput" class=" control-label">New Image</label>
																<div class="">
																	<input type="file" name="imagename" id="imagename" required>
																</div>
															</div>
															<?php 
														}
													} ?>

													<div class="row mb-4 ml-4">
														<div class="col-sm-8 col-sm-offset-2">
															<button type="submit" name="submit" class="btn-primary btn">Update</button>
														</div>
													</div>
												</form>
											</div>
										</div>
									</body>
									</html>
								
							
Download

Comments

Joan Love

Great job may the lord bless you

Willy James

You trained me and am now expert thank you

Mercy Johnson

This is clean and latest code I never found, thank you

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More