File "product_crud.php"
Full Path: /home/ovanhxso/public_html/panel/admin/app/product_crud.php
File size: 4.09 KB
MIME-type: text/x-php
Charset: utf-8
<?php
session_start();
error_reporting(0);
include '../../assets/constant/config.php';
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
$uploadDir = '../../assets/images/';
if (!empty($_FILES['photo']['tmp_name'])) {
$originalName = basename($_FILES['photo']['name']);
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$newName = rand(100, 999) . '.' . $extension;
$newFilePath = $uploadDir . $newName;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $newFilePath)) {
$img = $newName;
// Compression Logic
// compressImage($newFilePath, $newFilePath, 75); // 75 is the compression quality
} else {
echo 'There was an error uploading the file.';
exit;
}
}
$stmt = $conn->prepare("INSERT INTO `productlist`(`pro_name`,`short_content`, `photo`, `content`) VALUES (?,?,?,?)");
$stmt->execute([
htmlspecialchars($_POST['pro_name'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['short_content'], ENT_QUOTES, 'UTF-8'),
$img,
htmlspecialchars($_POST['content'], ENT_QUOTES, 'UTF-8'),
]);
$_SESSION['success'] = "Added Product";
?>
<script>
// Redirect to ../about_seo.php
window.location.href = "../manage_product.php";
</script>
<?php
}
////update Product
if (isset($_POST['update'])) {
// Handle the main product image
if (!empty($_FILES['photo']['tmp_name'])) {
$file_extension = pathinfo(htmlspecialchars($_FILES["photo"]["name"], ENT_QUOTES, 'UTF-8'), PATHINFO_EXTENSION);
$new_filename = uniqid() . '.' . $file_extension;
$filepath = "../../assets/images/" . $new_filename;
if (move_uploaded_file($_FILES["photo"]["tmp_name"], $filepath)) {
$img = $new_filename;
// Optional: Image compression logic
// compressImage($filepath, $filepath, 75); // 75 is the compression quality
@unlink("../../assets/images/" . $_POST['old_photo_img']);
}
} else {
$img = $_POST['old_photo_img'];
}
// Update the product details
$stmt = $conn->prepare("UPDATE `productlist` SET `pro_name`=?,`short_content`=?,`photo`=?,`content`=? WHERE id=? ");
$stmt->execute([
htmlspecialchars($_POST['pro_name'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['short_content'], ENT_QUOTES, 'UTF-8'),
$img,
htmlspecialchars($_POST['content'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8')
]);
$_SESSION['success'] = "Product Updated";
?>
<script>
// Redirect to the manage products page
window.location.href = "../manage_product.php";
</script>
<?php
}
/////category delete
if (isset($_POST['del_id'])) {
$stmt = $conn->prepare("UPDATE `productlist` SET delete_status='1' where id=? ");
$stmt->execute([htmlspecialchars($_POST['del_id'], ENT_QUOTES, 'UTF-8')]);
$_SESSION['success'] = "Product Deleted";
?>
<script>
// Redirect to ../about_seo.php
window.location.href = "../manage_product.php";
</script>
<?php
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
function compressImage($source, $destination, $quality)
{
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source);
imagepng($image, $destination, round(9 - ($quality / 10))); // PNG quality ranges from 0 to 9
}
}