Mini Shell
<?php
include('include/head_admin.php');
if (isset($_POST['submit'])) {
$active = isset($_POST['active']) ? 1 : 0;
$name = get_safe_value($con, $_POST['name']);
$title = get_safe_value($con, $_POST['title']);
$category = get_safe_value($con, $_POST['category']);
$url = generate_seo_friendly_title($name);
$image_alt_tag = get_safe_value($con, $_POST['image_alt_tag']);
$short_description = get_safe_value($con, $_POST['short_description']);
$description = get_safe_value($con, $_POST['description']);
$meta_title = get_safe_value($con, $_POST['meta_title']);
$meta_description = get_safe_value($con, $_POST['meta_description']);
$keyword = get_safe_value($con, $_POST['keyword']);
$canonical_tag = get_safe_value($con, $_POST['canonical_tag']);
$image_array = $_FILES['image'];
$image_count = count($image_array['name']);
$array = array();
for ($i = 0; $i < $image_count; $i++) {
$image_name = rand(111111111, 999999999) . '_' . $image_array['name'][$i];
$image_tmp_name = $image_array['tmp_name'][$i];
$image_size = $image_array['size'][$i];
$image_error = $image_array['error'][$i];
$image_type = $image_array['type'][$i];
array_push($array, $image_name);
// Upload the image to your desired location
move_uploaded_file($image_tmp_name, '../media/product/' . $image_name);
}
$product_images = json_encode($array);
$insert_query = "INSERT INTO `product`(`active`,`url`, `name`, `title`,`category`,`image`,`image_alt_tag`,`short_description`,`description`,`meta_title`,`meta_description`,`keyword`,`canonical_tag`)
VALUES ('$active','$url','$name','$title','$category','$product_images','$image_alt_tag','$short_description','$description','$meta_title','$meta_description','$keyword','$canonical_tag')";
mysqli_query($con, $insert_query);
header('location:product-all.php');
die();
}
if (isset($_POST['update_product_submit'])) {
echo $id = $_POST['id'];
$active = isset($_POST['active']) ? 1 : 0;
$name = get_safe_value($con, $_POST['name']);
$title = get_safe_value($con, $_POST['title']);
$category = get_safe_value($con, $_POST['category']);
$url = get_safe_value($con, $_POST['url']);
$image_alt_tag = get_safe_value($con, $_POST['image_alt_tag']);
$short_description = get_safe_value($con, $_POST['short_description']);
$description = get_safe_value($con, $_POST['description']);
$meta_title = get_safe_value($con, $_POST['meta_title']);
$meta_description = get_safe_value($con, $_POST['meta_description']);
$keyword = get_safe_value($con, $_POST['keyword']);
$canonical_tag = get_safe_value($con, $_POST['canonical_tag']);
// Check if new images are provided
if (isset($_FILES['image'])) {
// Fetch existing images from the database
$row = mysqli_query($con, "SELECT * FROM `product` WHERE id='$id'");
$result = mysqli_fetch_assoc($row);
$old_images = json_decode($result['image'], true); // Decode JSON array
$new_images = [];
// Loop through the new images
foreach ($_FILES['image']['name'] as $key => $value) {
// Check if the file field is not empty
if (!empty($_FILES['image']['name'][$key])) {
$new_image_name = rand(111111111, 999999999) . '_' . $_FILES['image']['name'][$key];
$new_image_tmp_name = $_FILES['image']['tmp_name'][$key];
move_uploaded_file($new_image_tmp_name, '../media/product/' . $new_image_name);
$new_images[] = $new_image_name;
} else {
// If no new image was uploaded for this index, add the corresponding old image
$new_images[] = $old_images[$key] ?? '';
}
}
// Encode the modified array of image names as JSON
$product_image = json_encode($new_images);
} else {
// If no new images were provided, keep the existing images
$product_image = $result['image'];
}
// Update the product information in the database
$update_sql = "UPDATE `product` SET `active`='$active',`url`='$url', `name`='$name', `title`='$title', `category`='$category', `image`='$product_image',`image_alt_tag`='$image_alt_tag',`short_description`='$short_description',`description`='$description',`meta_title`='$meta_title',`meta_description`='$meta_description',`canonical_tag`='$canonical_tag',`keyword`='$keyword' WHERE `id`='$id'";
mysqli_query($con, $update_sql);
header('location: product-all.php');
}
function generate_seo_friendly_title($title)
{
// Convert the title to lowercase
$title = strtolower($title);
// Replace spaces with dashes
$title = str_replace(' ', '-', $title);
// Remove special characters
$title = preg_replace('/[^A-Za-z0-9\-]/', '', $title);
return $title;
}