I've been trying a lot of different things to get the images to retain when other images are getting updated. Everything is good just except when I upload or update one of the images the other images are getting deleted or saved empty into the database.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
session_start();
include ("connection.php");
include ("functions.php");
$user_data = check_login($con);
if(count($_POST)>0) {
if(!empty($_FILES['garden_photo1']['name'] . $_FILES['garden_photo2']['name']) && isset($_FILES["garden_photo1"]["name"], $_FILES['garden_photo2']['name'] )){
$id = $_POST['id'];
$cultivar_name = $_POST['cultivar_name'];
$breeder = $_POST['breeder'];
$lineage1 = $_POST['lineage1'];
$garden_photo1 = $_FILES['garden_photo1']['name'];
$ImageName = $_FILES['garden_photo1']['name'];
$ImageName2 = $_FILES['garden_photo2']['name'];
$target = "/cultivar/images/" . $ImageName;
$target2 = "/cultivar/images/" . $ImageName2;
move_uploaded_file($_FILES['garden_photo1']['tmp_name'], $target);
move_uploaded_file($_FILES['garden_photo2']['tmp_name'], $target2);
$stmt = $con->prepare("UPDATE cultivar_db set cultivar_name = ?, breeder = ?, lineage1 = ?, garden_photo1 = ?, garden_photo2 = ? WHERE id= ? ");
$stmt->bind_param( "sssssi", $cultivar_name, $breeder, $lineage1, $garden_photo1, $garden_photo2, $id);
$stmt->execute();
}else {
$id = $_POST['id'];
$cultivar_name = $_POST['cultivar_name'];
$breeder = $_POST['breeder'];
$lineage1 = $_POST['lineage1'];
$stmt = $con->prepare("UPDATE cultivar_db set cultivar_name = ?, breeder = ?, lineage1 = ? WHERE id= ? ");
$stmt->bind_param( "sssi", $cultivar_name, $breeder, $lineage1, $id);
$stmt->execute();
}
$message = "Cultivar Record Modified Successfully!";
}
$id = $_GET['id'];
$sql = "SELECT * FROM cultivar_db WHERE id=?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = mysqli_fetch_array($result);
?>
<html>
<head>
<title>Update Cultivar Data</title>
<style>
.success{color: green; font-size:20px; fonte-weight:bold;}
</style>
</head>
<body>
<form name="cultivar_db" method="post" enctype="multipart/form-data">
<div style="padding-bottom:5px;">
<p><a href="cultivar-database.php">Go Back to Cultivar Database </a> > Edit <?php echo $row["cultivar_name"];?> Page</p>
</div>
<br>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" >
<br>
Cultivar Name: <br>
<input name="cultivar_name" value="<?php echo $row['cultivar_name']; ?>">
<br>
Breeder: <br>
<input type="text" name="breeder" value="<?php echo $row['breeder']; ?>">
<br>
Lineage 1 :<br>
<input type="text" name="lineage1" value="<?php echo $row['lineage1']; ?>">
<br>
Garden Photo 1: <img id="output" src="../cultivar/images/<?php echo $row["garden_photo1"];?>" style="height:auto; width:70px;">
<br>
<br>
<input id="garden_photo1" type="file" name="garden_photo1" >
<br>
Garden Photo 1: <img id="output" src="../cultivar/images/<?php echo $row["garden_photo2"];?>" style="height:auto; width:70px;">
<br>
<br>
<input id="garden_photo1" type="file" name="garden_photo2" >
<br>
<br>
<input id="submit" type="submit" name="submit" value="Submit" >
<br>
<br>
<div class="success">
<?php if(isset($message)) { echo $message; } ?>
</div>
</form>
</body>
</html>
Right now, you appear to be only differentiating between the two cases, no images were uploaded at all - or two images were uploaded. You need to handle the case(s) that either one of the images was uploaded, while the other one wasn't, as well.
But instead of writing different branches for all four possible combinations (no image was uploaded; image 1 was uploaded but image 2 was not; image 1 was not uploaded but image 2 was; or both were uploaded), it would make more sense if you handled each image upload separately:
Was image 1 uploaded? Then process that upload, and update the column for image 1 in your database.
Was image 2 uploaded? Then process that upload, and update the column for image 2 in your database.