im creating simple blog site, in creating new page, i have insert image gallery too. page and gallery both have two different table as i couldn’t figure out how to insert gallery into single table (means gallery section in pages table, so i created two tables)
What im trying to achieve in update pages is that check if image is insert or not in update page. if image is insert then display image else allow user to upload image in update page
table_name = english_version (table for page)
table_name = english_gallery (table for gallery)
*Where image_id = id (id from english_version)
so when user creates new page and sumbit, it is send to update page:
header(“Location:./update.php?update=$lastenterid”);
die();
so, if user forget to insert image for gallery then i want to give option for image upload in update pages…
if(isset($_GET['update'])){
$update_id = intval($_GET['update']);
$update = $conn->query("SELECT * FROM english_version WHERE id = $update_id ");
while($rows = $update->fetch(PDO::FETCH_OBJ) ):
$id = $rows->id;
$title = $rows->title;
$content = $rows->content;
endwhile;
}
checking if image is insert or not
<form action="" method="post">
<!-- english-version -->
<input type="hidden" name="id" value="<?php echo $id;?>">
<div class="form-group">
<input type="text" name="title" class="form-control mt-3" value="<?php echo $title?>">
</div>
<div class="form-group">
<textarea class="form-control" name="content" id="" cols="30" rows="10"><?php echo $content;?></textarea>
</div>
<div class="form-group">
<?php
//getting data from english_gallery (table for gallery)
$gallery = $conn->query("SELECT * FROM english_gallery WHERE image_id = $update_id");
while($rows_gal = $gallery->fetch(PDO::FETCH_OBJ) ):
$id = $rows_gal->gallery_id;
$image_id = $rows_gal->image_id;
$image = $rows_gal->image;
//check if image_id == $update_id (where $update is id of (primary_key) english_version (table for page))
if($image_id == $update_id ){ ?>
<div class="image-gallery">
<img src="<?php echo $image;?>" alt="">
</div>
<?php
}else{
$targetDir = "image/";
$allowTypes = array('jpg','png','jpeg','gif');
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
// File upload path
$fileName = basename($_FILES['images']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Store images on the server
if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$targetFilePath)){
$images_arr[] = $targetFilePath;
$insert = $conn->query("INSERT into english_gallery (image_id,image) VALUES ('$update_id','$targetFilePath')");
$galleryid = $conn->lastInsertId();
if($insert){
$count = $key + 1;
$statusMsg = " ".$count. " image file has been uploaded successfully.";
}else{
$statusMsg = "Failed to upload image";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
}
}//multiple_image upload starts
?>
<!-- if image_id !== $update then display multi-image upload section -->
<div class="form-group">
<input type="file" name="images[]" multiple>
</div>
<?php
}
endwhile;
?>
</div>
<div class="form-group">
<input type="submit" value="Update" name="update" class="btn btn-primary">
</div>
<!-- english-version -->
</form>
if there is image its displaying image but if there no image its not displaying input option for image upload
i think else conditions is not working because $image_id stores id of (primary_key) english_version (table for page)) it’s checking for the id which is never created…
so, how can i get the condition, where if user have insert image then, it will image will displayed in update page else user will get option for image uploading…