���� ������������������������������������ prepare($query); $stmt->bind_param("i", $pid); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $existingIds[] = $row['id']; } $stmt->close(); $idsToDelete = array_diff($existingIds, $ids); if (!empty($idsToDelete)) { foreach ($idsToDelete as $idToDelete) { $query = "SELECT image FROM images WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $idToDelete); $stmt->execute(); $stmt->bind_result($oldImagePath); $stmt->fetch(); $stmt->close(); if (!empty($oldImagePath) && file_exists($oldImagePath)) { unlink($oldImagePath); } $query = "DELETE FROM images WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $idToDelete); $stmt->execute(); $stmt->close(); } } foreach ($ids as $key => $id) { $caption = isset($captions[$key]) ? $captions[$key] : ''; $pins = isset($pinsArray[$key]) ? json_encode(json_decode($pinsArray[$key], true)) : null; $imageOrder = isset($imageOrders[$key]) ? intval($imageOrders[$key]) : 0; $imagePath = ''; $fileKey = $id != 'undefined' ? 'image_' . $id : 'image_' . $key; $oldImagePath = ''; if ($id && $id != 'undefined') { $query = "SELECT image FROM images WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($oldImagePath); $stmt->fetch(); $stmt->close(); } if (isset($_FILES['image_files']['name'][$fileKey]) && !empty($_FILES['image_files']['name'][$fileKey])) { $name = $_FILES['image_files']['name'][$fileKey]; $timestamp = time(); $fileExtension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); $nameWithoutSpaces = str_replace(' ', '_', $name); $newFileName = $timestamp . rand(1000, 10000000) . "_" . basename($nameWithoutSpaces); $targetFile = $targetDir . $newFileName; $check = getimagesize($_FILES["image_files"]["tmp_name"][$fileKey]); if ($check !== false) { if (!empty($oldImagePath) && file_exists($oldImagePath)) { unlink($oldImagePath); } if (move_uploaded_file($_FILES["image_files"]["tmp_name"][$fileKey], $targetFile)) { $imagePath = $targetFile; // Resize if width and height are provided if (!empty($imageSizes[$key])) { // list($width, $height) = explode(',', $imageSizes[$key]); // try { // $image = new ImageResize($targetFile); // if (!empty($width) && !empty($height)) { // $image->resizeToWidth($width); // $image->crop($width, $height); // } // $image->save($targetFile); // } catch (Exception $e) { // echo "Error resizing image: " . $e->getMessage(); // continue; // } list($width, $height,$type) = explode(',', $imageSizes[$key]); // Split width and height try { $image = new ImageResize($targetFile); if (!empty($width) && !empty($height)) { if($type=='width'){ $image->resizeToWidth($width); }elseif($type=='height'){ $image->resizeToHeight($height); }elseif ($type=='crop'){ $image->crop($width, $height); }elseif ($type=='widthCrop'){ $image->resizeToWidth($width); $image->crop($width, $height,0, 0); }elseif ($type=='heightCrop'){ $image->resizeToHeight($height); $image->crop($width, $height); }else{ $image->resize($width, $height); } } $image->save($targetFile); $imagePath = $targetFile; } catch (Exception $e) { echo "Error resizing image: " . $e->getMessage(); continue; } } } else { echo "Error uploading file for image: $name."; continue; } } else { echo "File is not a valid image: $name."; continue; } } $dataArray[] = [ 'id' => $id, 'caption' => $caption, 'pins' => $pins, 'imagePath' => $imagePath, 'pid' => $pid, 'imageOrder' => $imageOrder ]; } foreach ($dataArray as $data) { $id = $data['id']; $caption = $data['caption']; $pins = $data['pins']; $imagePath = $data['imagePath']; $pid = $data['pid']; $imageOrder = $data['imageOrder']; $stmt = null; if ($id && $id != 'undefined') { $query = "UPDATE images SET caption = ?, pins = ?, pid = ?, image_order = ?" . (!empty($imagePath) ? ", image = ?" : "") . " WHERE id = ?"; $stmt = $conn->prepare($query); if (!empty($imagePath)) { $stmt->bind_param("ssiisi", $caption, $pins, $pid, $imageOrder, $imagePath, $id); } else { $stmt->bind_param("ssiii", $caption, $pins, $pid, $imageOrder, $id); } } else { echo 'inserting'; if ($imagePath) { echo 'inside'; $query = "INSERT INTO images (image, caption, pins, pid, image_order) VALUES (?, ?, ?, ?, ?)"; $stmt = $conn->prepare($query); $stmt->bind_param("sssii", $imagePath, $caption, $pins, $pid, $imageOrder); }else{ // Set the existing pid and new pid $old_pid = $ret_id; // Existing pid to duplicate $new_pid = $pid; // New pid to assign to duplicates // Step 1: Retrieve the records that have the old pid $query = "SELECT image, caption, pins, image_order FROM images WHERE pid = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $old_pid); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { // Step 2: Insert each record as a duplicate with the new pid $insertQuery = "INSERT INTO images (image, caption, pins, pid, image_order) VALUES (?, ?, ?, ?, ?)"; $insertStmt = $conn->prepare($insertQuery); while ($row = $result->fetch_assoc()) { $image = $row['image']; $caption = $row['caption']; $pins = $row['pins']; $imageOrder = $row['image_order']; // Bind the values and insert the new row with the new pid $insertStmt->bind_param("sssii", $image, $caption, $pins, $new_pid, $imageOrder); $insertStmt->execute(); } echo "Records duplicated successfully with new pid."; } else { echo "No records found with pid = $old_pid."; } // Close statements $stmt->close(); $insertStmt->close(); $conn->close(); echo 'imagePathnotFound'; } } if ($stmt && $stmt->execute()) { echo "Image saved/updated successfully!"; } else { echo "Error: " . $stmt->error; } } } ?>