John Davidson

Image overwrite and update in Mysql Database using PHP

0 comments
Message:


I have a table of student data and a folder with their images.
I want to replace some imges but it is not working. please help.
I got this code somewhere and changed it according to my need but it is not working.
code is in two parts. The first part is a form with a jquery
action part in PHP.
please help what is wrong with this code



code of photochange.php


    <h1 class="page-head-line">Student Photo Change</h1>

<div class="container" style="width:900px;">

</div>
</div>
</body>
</html>

<div id="imageModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Add Image</h4>
</div>
<div class="modal-body">
<form id="image_form" method="post" enctype="multipart/form-data">
<p><label>Select Image</label>
<input type="file" name="image" id="image" /></p><br />
<input type="hidden" name="action" id="action" value="insert" />
<input type="hidden" name="image_id" id="image_id" />
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info" />

</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function(){

fetch_data();

function fetch_data()
{
var action = "fetch";
$.ajax({
url:"photochangeaction.php",
method:"POST",
data:{action:action},
success:function(data)
{
$('#image_data').html(data);
}
})
}
$('#add').click(function(){
$('#imageModal').modal('show');
$('#image_form')[0].reset();
$('.modal-title').text("Add Image");
$('#image_id').val('');
$('#action').val('insert');
$('#insert').val("Insert");
});
$('#image_form').submit(function(event){
event.preventDefault();
var image_name = $('#image').val();
if(image_name == '')
{
alert("Please Select Image");
return false;
}
else
{
var extension = $('#image').val().split('.').pop().toLowerCase();
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
$('#image').val('');
return false;
}
else
{
$.ajax({
url:"photochangephotochangeaction.php",
method:"POST",
data:new FormData(this),
contentType:false,
processData:false,
success:function(data)
{
alert(data);
fetch_data();
$('#image_form')[0].reset();
$('#imageModal').modal('hide');
}
});
}
}
});
$(document).on('click', '.update', function(){
$('#image_id').val($(this).attr("id"));
$('#action').val("update");
$('.modal-title').text("Update Image");
$('#insert').val("Update");
$('#imageModal').modal("show");
});
$(document).on('click', '.delete', function(){
var image_id = $(this).attr("id");
var action = "delete";
if(confirm("Are you sure you want to remove this image from database?"))
{
$.ajax({
url:"photochangephotochangeaction.php",
method:"POST",
data:{image_id:image_id, action:action},
success:function(data)
{
alert(data);
fetch_data();
}
})
}
else
{
return false;
}
});
});
</script>

code of photochangeaction.php


<?php

if(isset($_POST["action"]))
{

if($_POST["action"] == "fetch")
{
$query = "SELECT id, form_no, sname, photo FROM student where center_code='1001'";
$result = mysqli_query($conn, $query);
$output = '
<table class="table table-bordered table-striped">
<tr>
<th width="10%">ID</th>
<th width="70%">Image</th>
<th width="70%">File Name</th>
<th width="70%">Student Name</th>
<th width="10%">Change</th>

</tr>
';
while($row = mysqli_fetch_array($result))
{

$output .= '

<tr>
<td>'.$row["id"].'</td>
<td>
<img src="'.($row['photo'] ).'" height="60" width="75" class="img-thumbnail" />
</td>
<td>
'.($row['photo'] ).'
</td>
<td>
'.($row['sname'] ).'
</td>
<td><button type="button" name="update" class="btn btn-warning bt-xs update" id="'.$row["id"].'">Change</button></td>

</tr>
';
}
$output .= '</table>';
echo $output;
}


if($_POST["action"] == "update")
{
$file = $_FILES["image"]["tmp_name"];
// $image=PATHINFO($_FILES['image']['name']);
$form_no=$conn->query('select form_no from student WHERE id = '.$_POST["image_id"].'');
$newFilename=$form_no . '.' . $file['extension'];
if (file_exists("sphoto/" . $newfilename))
{
// file already exists error
unlink("sphoto/$newfilename");
move_uploaded_file($_FILES["image"]["tmp_name"], "sphoto/" . $newfilename);
$photo='../sphoto/' . $newFilename;
$query = "UPDATE student SET photo = '$photo' WHERE id = '".$_POST["image_id"]."'";
if(mysqli_query($conn, $query))
{
echo 'Image Updated into Database';
echo "File Overwritten";
}

}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"], "sphoto/" . $newfilename);
echo "File uploaded successfully.";
$query = "UPDATE student SET photo = '$photo' WHERE id = '".$_POST["image_id"]."'";
if(mysqli_query($conn, $query))
{
echo 'Image Updated into Database';
echo "File Overwritten";
}
}
}
}
?>


My Answer:






Rate this post

3 of 5 based on 3153 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us