I am creating a library management system, and I am also adding e-books. Adding e-book with pdf file and image are working. They are stored in their designated folders at inserted into the database. But the problem now is when I create the "edit e-book" part. I added a feature that if they want to overwrite the existing pdf file or image, they can update it. I used checkboxes so that they could pick what to update and include: a pdf file, an image, or both of them. The problem is that when I include any of the two and click the update button, it just unlinks the pdf file and the image. So in the database, the pdf name and the image name of the old ones are still there, and the new file I chose is not inserted.
https://i.stack.imgur.com/0MEXh.jpg (Picture of the checkboxes I wanted to work)
My code:
<!-- PDF -->
<div class="form-group">
<input id="checkbox-id" type="checkbox" name="upload[]" value="PDF File" class="righty">
<label class="rightx" for="checkbox-id"> Include E-Book
</label>
<label class="control-label col-md-4" for="file"></label>
<div class="col-md-4">
<input id="subject" type="file" name="pdf" value="" class="form-control col-md-7 col-xs-12">
<?php
include '../include/dbcon.php';
if (isset($_POST['update11'])) {
$checkbox=$_POST['upload'];
foreach($checkbox as $item){
$sql="SELECT pdf FROM ebooks WHERE ebook_id = " . $_GET['ebook_id'];
$query=mysqli_query($con,$sql);
while ($info=mysqli_fetch_array($query)) {
unlink("../../ebooks/" . $info['pdf']);
}
$pdf=$_FILES['pdf']['name'];
$pdf_type=$_FILES['pdf']['type'];
$pdf_size=$_FILES['pdf']['size'];
$pdf_tem_loc=$_FILES['pdf']['tmp_name'];
$pdf_store="../../ebooks/".$pdf; // This line is for editing the PATH for storing PDF Files.
try {
move_uploaded_file($pdf_tem_loc,$pdf_store);
} catch(\Exception $e) {
die($e);
}
mysqli_query($con," UPDATE ebooks SET pdf='$pdf',pdf_type='$pdf_type',pdf_size='$pdf_size',pdf_tem_loc='$pdf_tem_loc',pdf_store='$pdf_store' WHERE ebook_id = '$id' ")or die(mysql_error());
echo "<script>alert('Successfully updated!); history.go(-2
);</script>";
}
}
?>
</div>
</div>
<!-- Image -->
<div class="form-group">
<input id="checkbox-id2" type="checkbox" name="upload[]" value="Image File" class="rights">
<label class="rightx" for="checkbox-id2"> Include Image
</label>
<label class="control-label col-md-4" for="file"></label>
<div class="col-md-4">
<input id="subject" type="file" name="image" value="" class="form-control col-md-7 col-xs-12">
<?php
include '../include/dbcon.php';
if (isset($_POST['update11'])) {
$checkbox=$_POST['upload'];
foreach($checkbox as $item){
$sql="SELECT ebook_img FROM ebooks WHERE ebook_id = " . $_GET['ebook_id'];
$query=mysqli_query($con,$sql);
while ($deli=mysqli_fetch_array($query)) {
unlink("../../images/" . $deli['ebook_img']);
}
$ebook_img=$_FILES['image']['name'];
$img_size=$_FILES['image']['size'];
$img_tem_loc=$_FILES['image']['tmp_name'];
$img_store="../../images/".$ebook_img; // This line is for editing the PATH for storing Images.
try {
move_uploaded_file($img_tem_loc,$img_store);
} catch(\Exception $e) {
die($e);
}
mysqli_query($con," UPDATE ebooks SET ebook_img='$ebook_img',img_size='$img_size',img_tem_loc='$img_tem_loc',img_store='$img_store' WHERE ebook_id = '$id' ")or die(mysql_error());
echo "<script>alert('Successfully updated!); history.go(-2
);</script>";
}
}
?>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-5">
<a href="ebook.php"><button type="button" class="btn btn-primary"><i class="fa fa-times-circle-o"></i> Cancel</button></a>
<button type="submit" name="update11" class="btn btn-success"><i class="glyphicon glyphicon-save"></i> Update</button>
</div>