actually i have update page where update the data there product database updated successfully but stock and varients aren’t , please help me to finding the error. when i run this code then show : No stock entries found.No stock entries found.No stock entries found.No stock entries found.No stock entries found.No stock entries found
<code><?php
session_start();
$userProfile = $_SESSION['AdminLoginId'];
if($userProfile == true){
}
else{
header('location:../admin-login.php');
}
?>
<?php
include("../connection.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$ID = $_GET['id'];
$query_prod = "SELECT * FROM product WHERE product_id = '$ID'";
$data_prod = mysqli_query($connection, $query_prod);
$totalrow_prod = mysqli_num_rows($data_prod);
$result_prod = mysqli_fetch_assoc($data_prod);
$prod_tags = explode(', ', $result_prod['tags']);
$query_cat = "SELECT category_id, category_name FROM categories";
$data_cat = mysqli_query($connection, $query_cat);
$query_variants = "SELECT * FROM product_varients WHERE product_id = '$ID'";
$data_variants = mysqli_query($connection, $query_variants);
$query_stock = "SELECT * FROM product_stock WHERE product_id = '$ID'";
$data_stock = mysqli_query($connection, $query_stock);
$query_images = "SELECT * FROM product_images WHERE product_id = '$ID'";
$data_images = mysqli_query($connection, $query_images);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Product</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="Style.css">
<style>
.input-container {
margin-bottom: 10px;
}
.text-input {
width: 200px;
padding: 5px;
margin-right: 5px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class=" pt-4 pb-4">
<div class="container pt-3 brdr text-danger " >
<h2 class="text-center mb-4">Update Current Product</h2>
<div class="container p-5 text-dark bg-light" style=" border: 3px solid #dc3545; border-radius: 10px;">
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title" style="font-weight: bold;">Title:</label>
<input type="text" class="form-control" id="title" name="prod_title" value="<?php echo $result_prod['product_name'] ?>">
</div>
<div class="form-group">
<label for="desc" style="font-weight: bold;">Description:</label><br>
<textarea id="desc" name="prod_desc" rows="10" cols="85" ><?php echo $result_prod['description'] ?></textarea>
</div>
<div class="form-group">
<label for="mrf" style="font-weight: bold;">MRP:</label>
<input type="num" class="form-control" id="mrf" name="prod_mrf" value="<?php echo $result_prod['mrf'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Price:</label>
<input type="num" class="form-control" id="price" name="prod_price" value="<?php echo $result_prod['price'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Discount:</label>
<input type="num" class="form-control" id="price" name="prod_discount" value="<?php echo $result_prod['product_discount'] ?>">
</div>
<div class="form-group">
<label style="font-weight: bold;">Stock Quantity for Each Size:</label>
<div class="row">
<?php while($result_stock = mysqli_fetch_assoc($data_stock)){
echo '<div class="col-12 mb-4">';
echo'<input type="text" name="product[size][]" value="'.$result_stock['size'].'" >';
echo'<input type="number" name="product[quantity][]" placeholder="quantity" value="'.$result_stock['stock'].'" >';
echo'</div>';
} ?>
</div>
</div>
<div class="form-group">
<label for="Category" style="font-weight: bold;">Product Category:</label><br>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Start the <select> element
echo '<select name="category" id="category" class="form-control">';
echo '<option disabled>--Select Category--</option>'; // Optional prompt
while ($result = mysqli_fetch_assoc($data_cat)) {
$selected = ($result['category_id'] == $result_prod['category_id']) ? 'selected' : '';
echo '<option value="' . $result['category_id'] . '" ' . $selected . '>' . $result['category_name'] . '</option>';
}
echo '</select>';
?>
</div>
<div class="form-group">
<label for="status" style="font-weight: bold;">Status:</label><br>
<select name="status" id="status" class="form-control">
<option disabled>--Select Status--</option>
<option value="active" <?php echo($result_prod['status'] == 'active')?'selected':'' ?>>active</option>
<option value="inactive" <?php echo($result_prod['status'] == 'inactive')?'selected':'' ?>>inactive</option>
</select>
</div>
<div class="form-group" id="variants-container">
<label for="quantity" style="font-weight: bold;">For Adding Product Varients:</label><br>
<button type="button" class="btn btn-dark mb-3" onclick="addVariant()">Add Variant</button>
<?php echo '<div class="form-group" id="variants-container">';
echo '<label for="variants-container" style="font-weight: bold;">Product Variants:</label>';
// Iterate through each variant
while ($variant = mysqli_fetch_assoc($data_variants)) {
if ($variant['varient_color'] !== null) {
$varient_size = explode(', ', $variant['varient_color']);
} else {
$varient_size = [];
}
// Create a row for each variant
echo '<div class="row mb-3 mt-2">';
echo '<div class="col">';
echo '<input type="text" name="variant[color][]" placeholder="Color" value="' . $variant['varient_color'] . '" class="form-control">';
echo '</div>';
echo '<div class="col">';
echo '<input type="text" name="variant[url][]" placeholder="url" value="' . $variant['product_url'] . '" class="form-control">';
echo '</div>';
// echo "<button type='button' name='delete_variant' value='" . $variant['product_varient_id'] . "'>Delete</button>";
// Close the row
echo '</div>';
}
// Close the variants container
echo '</div>'; ?>
</div>
<div class="form-group" id="images-container">
<label for="images" style="font-weight: bold;">Upload Image:</label><br>
<button type="button" id="images" class="btn btn-dark mb-3" onclick="addImage()">Add Image</button>
<?php
// Iterate through each image and display the input fields
while ($image = mysqli_fetch_assoc($data_images)) {
echo '<div class="mb-3">';
// Display the image with the existing file
echo '<img src="../product-images/' . $image['image_path'] . '" alt="Product Image" style="max-width:200px; max-height: 200px; display: block; margin-bottom: 10px;">';
// Display a hidden input field for the existing image
echo '<input type="hidden" name="existing_images[]" value="../product_images/' . $image['product_image_id'] . '">';
// Provide an option to delete the image
echo '<button type="button" class="btn btn-danger" onclick="deleteImage(this)">Delete Image</button>';
echo '</div>'; // Close the image container
}
?>
</div>
<div class="form-group" id="multiple-text-input">
<label for="images" style="font-weight: bold;">Include Tags:</label><br>
<button type="button" class="btn btn-dark mb-3" id="add-input-btn" onClick="addTextInput()">Add tags</button>
<?php foreach ($prod_tags as $tag) {
echo'<div class="input-container">';
echo '<input type="text" name="tags[]" value="' . $tag . '" class="text-input">';
// echo'<button type="button">Remove</button>';
echo'</div>';
}?>
</div>
<button type="submit" class="btn btn-danger btn-block mb-3" name="user_btn" style="font-size: 1.1rem;">Submit</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous">
</script>
<script>
// Function to add a new product variant
function addVariant() {
var variantsContainer = document.getElementById('variants-container');
// Create a new row for the variant
var newRow = document.createElement('div');
newRow.className = 'row mb-3 mt-2';
var urlCol = document.createElement('div');
urlCol.className = 'col-6';
var colorCol = document.createElement('div');
colorCol.className = 'col-6';
var urlInput = document.createElement('input');
urlInput.type = 'text';
urlInput.name = 'variant[url][]';
urlInput.placeholder = 'url';
urlInput.className = 'form-control';
urlCol.appendChild(urlInput);
// Create and add the quantity input field
var colorInput = document.createElement('input');
colorInput.type = 'text';
colorInput.name = 'variant[color][]';
colorInput.placeholder = 'color';
colorInput.className = 'form-control';
colorCol.appendChild(colorInput);
newRow.appendChild(urlCol);
newRow.appendChild(colorCol);
// Append the row to the variants container
variantsContainer.appendChild(newRow);
}
function addImage() {
// Get the images container
var imagesContainer = document.getElementById('images-container');
// Create a new div for the image input
var newDiv = document.createElement('div');
newDiv.className = 'mb-3';
// Create a new input of type file for the image
var newImageInput = document.createElement('input');
newImageInput.type = 'file';
newImageInput.name = 'images[]'; // Set name as an array
// Append the new input to the new div
newDiv.appendChild(newImageInput);
// Append the new div to the images container
imagesContainer.appendChild(newDiv);
}
function deleteImage(button) {
var div = button.parentElement;
div.style.display = 'none'; // Hide the div containing the image
// Create a hidden input to mark the image for deletion
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'images_to_delete[]';
input.value = div.querySelector('input[name="existing_images[]"]').value;
document.forms[0].appendChild(input);
}
function addTextInput() {
const inputContainer = document.getElementById("multiple-text-input");
const inputDiv = document.createElement("div");
inputDiv.classList.add("input-container");
const inputField = document.createElement("input");
inputField.setAttribute("type", "text");
inputField.setAttribute("name", "tags[]");
inputField.classList.add("text-input");
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
// Add event listener to the remove button
removeButton.addEventListener("click", function () {
inputDiv.remove(); // Remove the input field when the "Remove" button is clicked
});
inputDiv.appendChild(inputField);
inputDiv.appendChild(removeButton);
inputContainer.appendChild(inputDiv);
}
</script>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['user_btn'])) {
// Retrieve the product ID from the form (you need to add the product ID to your form)
$product_id = $ID;
// Retrieve form data
$prod_title = $_POST['prod_title'];
$prod_desc = $_POST['prod_desc'];
$prod_mrf = $_POST['prod_mrf'];
$prod_price = $_POST['prod_price'];
$prod_discount = $_POST['prod_discount'];
$prod_category = $_POST['category'];
$prod_category_value_int = (int) $prod_category;
$prod_status = $_POST['status'];
$tags = implode(', ', $_POST['tags']);
try {
// Begin the transaction
mysqli_begin_transaction($connection);
// Update product information
$updateProductQuery = "UPDATE product SET product_name = ?, category_id = ?, description = ?, price = ?, mrf = ?, product_discount = ?, status = ?, tags = ? WHERE product_id = ?";
$stmt = mysqli_prepare($connection, $updateProductQuery);
mysqli_stmt_bind_param($stmt, 'sisddsssi', $prod_title, $prod_category_value_int, $prod_desc, $prod_price, $prod_mrf, $prod_discount, $prod_status, $tags, $ID);
mysqli_stmt_execute($stmt);
// if (!$data_prod) {
// echo "Error updating product: " . mysqli_error($connection);
// }
// Update Variants:
if (isset($_POST['variant'])) {
foreach ($_POST['variant']['color'] as $index => $color) {
$color = $_POST['variant']['color'][$index];
$url = $_POST['variant']['url'][$index];
// Assuming you have fetched the variants data before this loop
while($result_variants = mysqli_fetch_assoc($data_variants)){
// Check if there are any variants available
if ($result_variants) {
$variant_id = $result_variants['product_varient_id'];
$updateVariantQuery = "UPDATE product_varients SET varient_color = ?, product_url = ? WHERE product_varient_id = ?";
$stmt = mysqli_prepare($connection, $updateVariantQuery);
mysqli_stmt_bind_param($stmt, 'ssi', $color, $url, $variant_id);
mysqli_stmt_execute($stmt);
} else {
echo "No variants found.";
}
}
}
}
// Update Stocks:
if (isset($_POST['product'])) {
// Fetch the initial row of stock data before the loop
$result_stock = mysqli_fetch_assoc($data_stock);
foreach ($_POST['product']['size'] as $i => $size) {
$size = $_POST['product']['size'][$i];
$quantity = $_POST['product']['quantity'][$i];
// Check if there are any stock entries available
if ($result_stock) {
$stock_id = $result_stock['product_stock_id'];
$updateStockQuery = "UPDATE product_stock SET size = ?, stock = ? WHERE product_stock_id = ?";
$stmt = mysqli_prepare($connection, $updateStockQuery);
mysqli_stmt_bind_param($stmt, 'sii', $size, $quantity, $stock_id);
mysqli_stmt_execute($stmt);
// Fetch the next row of stock data
$result_stock = mysqli_fetch_assoc($data_stock);
} else {
echo "No stock entries found.";
}
}
}
// Update product images if provided
if (isset($_FILES['images'])) {
// Loop through each uploaded image
foreach ($_FILES['images']['tmp_name'] as $index => $tmp_name) {
$fileName = basename($_FILES['images']['name'][$index]);
$filePath = $uploadDir . $fileName;
// Move the uploaded file to the desired directory
if (move_uploaded_file($tmp_name, $filePath)) {
$insertImageQuery = "INSERT INTO product_images (product_id, image_path) VALUES (?, ?) ON DUPLICATE KEY UPDATE image_path = ?";
$stmt = mysqli_prepare($connection, $insertImageQuery);
mysqli_stmt_bind_param($stmt, 'iss', $product_id, $filePath, $filePath);
mysqli_stmt_execute($stmt);
} else {
throw new Exception('Failed to upload products images.');
}
}
}
// Commit the transaction if everything is successful
mysqli_commit($connection);
echo '<script>alert("Product Updated Successfully")</script>';
// echo '<meta http-equiv="refresh" content="0; url=https://jbehari.aonebox.in/administration/admin-show-product.php">';
} catch (Exception $e) {
// Roll back the transaction in case of an error
mysqli_rollback($connection);
echo "Error: " . $e->getMessage();
}
}
?>
</code>
<code><?php
session_start();
$userProfile = $_SESSION['AdminLoginId'];
if($userProfile == true){
}
else{
header('location:../admin-login.php');
}
?>
<?php
include("../connection.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$ID = $_GET['id'];
$query_prod = "SELECT * FROM product WHERE product_id = '$ID'";
$data_prod = mysqli_query($connection, $query_prod);
$totalrow_prod = mysqli_num_rows($data_prod);
$result_prod = mysqli_fetch_assoc($data_prod);
$prod_tags = explode(', ', $result_prod['tags']);
$query_cat = "SELECT category_id, category_name FROM categories";
$data_cat = mysqli_query($connection, $query_cat);
$query_variants = "SELECT * FROM product_varients WHERE product_id = '$ID'";
$data_variants = mysqli_query($connection, $query_variants);
$query_stock = "SELECT * FROM product_stock WHERE product_id = '$ID'";
$data_stock = mysqli_query($connection, $query_stock);
$query_images = "SELECT * FROM product_images WHERE product_id = '$ID'";
$data_images = mysqli_query($connection, $query_images);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Product</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="Style.css">
<style>
.input-container {
margin-bottom: 10px;
}
.text-input {
width: 200px;
padding: 5px;
margin-right: 5px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class=" pt-4 pb-4">
<div class="container pt-3 brdr text-danger " >
<h2 class="text-center mb-4">Update Current Product</h2>
<div class="container p-5 text-dark bg-light" style=" border: 3px solid #dc3545; border-radius: 10px;">
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title" style="font-weight: bold;">Title:</label>
<input type="text" class="form-control" id="title" name="prod_title" value="<?php echo $result_prod['product_name'] ?>">
</div>
<div class="form-group">
<label for="desc" style="font-weight: bold;">Description:</label><br>
<textarea id="desc" name="prod_desc" rows="10" cols="85" ><?php echo $result_prod['description'] ?></textarea>
</div>
<div class="form-group">
<label for="mrf" style="font-weight: bold;">MRP:</label>
<input type="num" class="form-control" id="mrf" name="prod_mrf" value="<?php echo $result_prod['mrf'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Price:</label>
<input type="num" class="form-control" id="price" name="prod_price" value="<?php echo $result_prod['price'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Discount:</label>
<input type="num" class="form-control" id="price" name="prod_discount" value="<?php echo $result_prod['product_discount'] ?>">
</div>
<div class="form-group">
<label style="font-weight: bold;">Stock Quantity for Each Size:</label>
<div class="row">
<?php while($result_stock = mysqli_fetch_assoc($data_stock)){
echo '<div class="col-12 mb-4">';
echo'<input type="text" name="product[size][]" value="'.$result_stock['size'].'" >';
echo'<input type="number" name="product[quantity][]" placeholder="quantity" value="'.$result_stock['stock'].'" >';
echo'</div>';
} ?>
</div>
</div>
<div class="form-group">
<label for="Category" style="font-weight: bold;">Product Category:</label><br>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Start the <select> element
echo '<select name="category" id="category" class="form-control">';
echo '<option disabled>--Select Category--</option>'; // Optional prompt
while ($result = mysqli_fetch_assoc($data_cat)) {
$selected = ($result['category_id'] == $result_prod['category_id']) ? 'selected' : '';
echo '<option value="' . $result['category_id'] . '" ' . $selected . '>' . $result['category_name'] . '</option>';
}
echo '</select>';
?>
</div>
<div class="form-group">
<label for="status" style="font-weight: bold;">Status:</label><br>
<select name="status" id="status" class="form-control">
<option disabled>--Select Status--</option>
<option value="active" <?php echo($result_prod['status'] == 'active')?'selected':'' ?>>active</option>
<option value="inactive" <?php echo($result_prod['status'] == 'inactive')?'selected':'' ?>>inactive</option>
</select>
</div>
<div class="form-group" id="variants-container">
<label for="quantity" style="font-weight: bold;">For Adding Product Varients:</label><br>
<button type="button" class="btn btn-dark mb-3" onclick="addVariant()">Add Variant</button>
<?php echo '<div class="form-group" id="variants-container">';
echo '<label for="variants-container" style="font-weight: bold;">Product Variants:</label>';
// Iterate through each variant
while ($variant = mysqli_fetch_assoc($data_variants)) {
if ($variant['varient_color'] !== null) {
$varient_size = explode(', ', $variant['varient_color']);
} else {
$varient_size = [];
}
// Create a row for each variant
echo '<div class="row mb-3 mt-2">';
echo '<div class="col">';
echo '<input type="text" name="variant[color][]" placeholder="Color" value="' . $variant['varient_color'] . '" class="form-control">';
echo '</div>';
echo '<div class="col">';
echo '<input type="text" name="variant[url][]" placeholder="url" value="' . $variant['product_url'] . '" class="form-control">';
echo '</div>';
// echo "<button type='button' name='delete_variant' value='" . $variant['product_varient_id'] . "'>Delete</button>";
// Close the row
echo '</div>';
}
// Close the variants container
echo '</div>'; ?>
</div>
<div class="form-group" id="images-container">
<label for="images" style="font-weight: bold;">Upload Image:</label><br>
<button type="button" id="images" class="btn btn-dark mb-3" onclick="addImage()">Add Image</button>
<?php
// Iterate through each image and display the input fields
while ($image = mysqli_fetch_assoc($data_images)) {
echo '<div class="mb-3">';
// Display the image with the existing file
echo '<img src="../product-images/' . $image['image_path'] . '" alt="Product Image" style="max-width:200px; max-height: 200px; display: block; margin-bottom: 10px;">';
// Display a hidden input field for the existing image
echo '<input type="hidden" name="existing_images[]" value="../product_images/' . $image['product_image_id'] . '">';
// Provide an option to delete the image
echo '<button type="button" class="btn btn-danger" onclick="deleteImage(this)">Delete Image</button>';
echo '</div>'; // Close the image container
}
?>
</div>
<div class="form-group" id="multiple-text-input">
<label for="images" style="font-weight: bold;">Include Tags:</label><br>
<button type="button" class="btn btn-dark mb-3" id="add-input-btn" onClick="addTextInput()">Add tags</button>
<?php foreach ($prod_tags as $tag) {
echo'<div class="input-container">';
echo '<input type="text" name="tags[]" value="' . $tag . '" class="text-input">';
// echo'<button type="button">Remove</button>';
echo'</div>';
}?>
</div>
<button type="submit" class="btn btn-danger btn-block mb-3" name="user_btn" style="font-size: 1.1rem;">Submit</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous">
</script>
<script>
// Function to add a new product variant
function addVariant() {
var variantsContainer = document.getElementById('variants-container');
// Create a new row for the variant
var newRow = document.createElement('div');
newRow.className = 'row mb-3 mt-2';
var urlCol = document.createElement('div');
urlCol.className = 'col-6';
var colorCol = document.createElement('div');
colorCol.className = 'col-6';
var urlInput = document.createElement('input');
urlInput.type = 'text';
urlInput.name = 'variant[url][]';
urlInput.placeholder = 'url';
urlInput.className = 'form-control';
urlCol.appendChild(urlInput);
// Create and add the quantity input field
var colorInput = document.createElement('input');
colorInput.type = 'text';
colorInput.name = 'variant[color][]';
colorInput.placeholder = 'color';
colorInput.className = 'form-control';
colorCol.appendChild(colorInput);
newRow.appendChild(urlCol);
newRow.appendChild(colorCol);
// Append the row to the variants container
variantsContainer.appendChild(newRow);
}
function addImage() {
// Get the images container
var imagesContainer = document.getElementById('images-container');
// Create a new div for the image input
var newDiv = document.createElement('div');
newDiv.className = 'mb-3';
// Create a new input of type file for the image
var newImageInput = document.createElement('input');
newImageInput.type = 'file';
newImageInput.name = 'images[]'; // Set name as an array
// Append the new input to the new div
newDiv.appendChild(newImageInput);
// Append the new div to the images container
imagesContainer.appendChild(newDiv);
}
function deleteImage(button) {
var div = button.parentElement;
div.style.display = 'none'; // Hide the div containing the image
// Create a hidden input to mark the image for deletion
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'images_to_delete[]';
input.value = div.querySelector('input[name="existing_images[]"]').value;
document.forms[0].appendChild(input);
}
function addTextInput() {
const inputContainer = document.getElementById("multiple-text-input");
const inputDiv = document.createElement("div");
inputDiv.classList.add("input-container");
const inputField = document.createElement("input");
inputField.setAttribute("type", "text");
inputField.setAttribute("name", "tags[]");
inputField.classList.add("text-input");
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
// Add event listener to the remove button
removeButton.addEventListener("click", function () {
inputDiv.remove(); // Remove the input field when the "Remove" button is clicked
});
inputDiv.appendChild(inputField);
inputDiv.appendChild(removeButton);
inputContainer.appendChild(inputDiv);
}
</script>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['user_btn'])) {
// Retrieve the product ID from the form (you need to add the product ID to your form)
$product_id = $ID;
// Retrieve form data
$prod_title = $_POST['prod_title'];
$prod_desc = $_POST['prod_desc'];
$prod_mrf = $_POST['prod_mrf'];
$prod_price = $_POST['prod_price'];
$prod_discount = $_POST['prod_discount'];
$prod_category = $_POST['category'];
$prod_category_value_int = (int) $prod_category;
$prod_status = $_POST['status'];
$tags = implode(', ', $_POST['tags']);
try {
// Begin the transaction
mysqli_begin_transaction($connection);
// Update product information
$updateProductQuery = "UPDATE product SET product_name = ?, category_id = ?, description = ?, price = ?, mrf = ?, product_discount = ?, status = ?, tags = ? WHERE product_id = ?";
$stmt = mysqli_prepare($connection, $updateProductQuery);
mysqli_stmt_bind_param($stmt, 'sisddsssi', $prod_title, $prod_category_value_int, $prod_desc, $prod_price, $prod_mrf, $prod_discount, $prod_status, $tags, $ID);
mysqli_stmt_execute($stmt);
// if (!$data_prod) {
// echo "Error updating product: " . mysqli_error($connection);
// }
// Update Variants:
if (isset($_POST['variant'])) {
foreach ($_POST['variant']['color'] as $index => $color) {
$color = $_POST['variant']['color'][$index];
$url = $_POST['variant']['url'][$index];
// Assuming you have fetched the variants data before this loop
while($result_variants = mysqli_fetch_assoc($data_variants)){
// Check if there are any variants available
if ($result_variants) {
$variant_id = $result_variants['product_varient_id'];
$updateVariantQuery = "UPDATE product_varients SET varient_color = ?, product_url = ? WHERE product_varient_id = ?";
$stmt = mysqli_prepare($connection, $updateVariantQuery);
mysqli_stmt_bind_param($stmt, 'ssi', $color, $url, $variant_id);
mysqli_stmt_execute($stmt);
} else {
echo "No variants found.";
}
}
}
}
// Update Stocks:
if (isset($_POST['product'])) {
// Fetch the initial row of stock data before the loop
$result_stock = mysqli_fetch_assoc($data_stock);
foreach ($_POST['product']['size'] as $i => $size) {
$size = $_POST['product']['size'][$i];
$quantity = $_POST['product']['quantity'][$i];
// Check if there are any stock entries available
if ($result_stock) {
$stock_id = $result_stock['product_stock_id'];
$updateStockQuery = "UPDATE product_stock SET size = ?, stock = ? WHERE product_stock_id = ?";
$stmt = mysqli_prepare($connection, $updateStockQuery);
mysqli_stmt_bind_param($stmt, 'sii', $size, $quantity, $stock_id);
mysqli_stmt_execute($stmt);
// Fetch the next row of stock data
$result_stock = mysqli_fetch_assoc($data_stock);
} else {
echo "No stock entries found.";
}
}
}
// Update product images if provided
if (isset($_FILES['images'])) {
// Loop through each uploaded image
foreach ($_FILES['images']['tmp_name'] as $index => $tmp_name) {
$fileName = basename($_FILES['images']['name'][$index]);
$filePath = $uploadDir . $fileName;
// Move the uploaded file to the desired directory
if (move_uploaded_file($tmp_name, $filePath)) {
$insertImageQuery = "INSERT INTO product_images (product_id, image_path) VALUES (?, ?) ON DUPLICATE KEY UPDATE image_path = ?";
$stmt = mysqli_prepare($connection, $insertImageQuery);
mysqli_stmt_bind_param($stmt, 'iss', $product_id, $filePath, $filePath);
mysqli_stmt_execute($stmt);
} else {
throw new Exception('Failed to upload products images.');
}
}
}
// Commit the transaction if everything is successful
mysqli_commit($connection);
echo '<script>alert("Product Updated Successfully")</script>';
// echo '<meta http-equiv="refresh" content="0; url=https://jbehari.aonebox.in/administration/admin-show-product.php">';
} catch (Exception $e) {
// Roll back the transaction in case of an error
mysqli_rollback($connection);
echo "Error: " . $e->getMessage();
}
}
?>
</code>
<?php
session_start();
$userProfile = $_SESSION['AdminLoginId'];
if($userProfile == true){
}
else{
header('location:../admin-login.php');
}
?>
<?php
include("../connection.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$ID = $_GET['id'];
$query_prod = "SELECT * FROM product WHERE product_id = '$ID'";
$data_prod = mysqli_query($connection, $query_prod);
$totalrow_prod = mysqli_num_rows($data_prod);
$result_prod = mysqli_fetch_assoc($data_prod);
$prod_tags = explode(', ', $result_prod['tags']);
$query_cat = "SELECT category_id, category_name FROM categories";
$data_cat = mysqli_query($connection, $query_cat);
$query_variants = "SELECT * FROM product_varients WHERE product_id = '$ID'";
$data_variants = mysqli_query($connection, $query_variants);
$query_stock = "SELECT * FROM product_stock WHERE product_id = '$ID'";
$data_stock = mysqli_query($connection, $query_stock);
$query_images = "SELECT * FROM product_images WHERE product_id = '$ID'";
$data_images = mysqli_query($connection, $query_images);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Product</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="Style.css">
<style>
.input-container {
margin-bottom: 10px;
}
.text-input {
width: 200px;
padding: 5px;
margin-right: 5px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class=" pt-4 pb-4">
<div class="container pt-3 brdr text-danger " >
<h2 class="text-center mb-4">Update Current Product</h2>
<div class="container p-5 text-dark bg-light" style=" border: 3px solid #dc3545; border-radius: 10px;">
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title" style="font-weight: bold;">Title:</label>
<input type="text" class="form-control" id="title" name="prod_title" value="<?php echo $result_prod['product_name'] ?>">
</div>
<div class="form-group">
<label for="desc" style="font-weight: bold;">Description:</label><br>
<textarea id="desc" name="prod_desc" rows="10" cols="85" ><?php echo $result_prod['description'] ?></textarea>
</div>
<div class="form-group">
<label for="mrf" style="font-weight: bold;">MRP:</label>
<input type="num" class="form-control" id="mrf" name="prod_mrf" value="<?php echo $result_prod['mrf'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Price:</label>
<input type="num" class="form-control" id="price" name="prod_price" value="<?php echo $result_prod['price'] ?>">
</div>
<div class="form-group">
<label for="price" style="font-weight: bold;">Discount:</label>
<input type="num" class="form-control" id="price" name="prod_discount" value="<?php echo $result_prod['product_discount'] ?>">
</div>
<div class="form-group">
<label style="font-weight: bold;">Stock Quantity for Each Size:</label>
<div class="row">
<?php while($result_stock = mysqli_fetch_assoc($data_stock)){
echo '<div class="col-12 mb-4">';
echo'<input type="text" name="product[size][]" value="'.$result_stock['size'].'" >';
echo'<input type="number" name="product[quantity][]" placeholder="quantity" value="'.$result_stock['stock'].'" >';
echo'</div>';
} ?>
</div>
</div>
<div class="form-group">
<label for="Category" style="font-weight: bold;">Product Category:</label><br>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Start the <select> element
echo '<select name="category" id="category" class="form-control">';
echo '<option disabled>--Select Category--</option>'; // Optional prompt
while ($result = mysqli_fetch_assoc($data_cat)) {
$selected = ($result['category_id'] == $result_prod['category_id']) ? 'selected' : '';
echo '<option value="' . $result['category_id'] . '" ' . $selected . '>' . $result['category_name'] . '</option>';
}
echo '</select>';
?>
</div>
<div class="form-group">
<label for="status" style="font-weight: bold;">Status:</label><br>
<select name="status" id="status" class="form-control">
<option disabled>--Select Status--</option>
<option value="active" <?php echo($result_prod['status'] == 'active')?'selected':'' ?>>active</option>
<option value="inactive" <?php echo($result_prod['status'] == 'inactive')?'selected':'' ?>>inactive</option>
</select>
</div>
<div class="form-group" id="variants-container">
<label for="quantity" style="font-weight: bold;">For Adding Product Varients:</label><br>
<button type="button" class="btn btn-dark mb-3" onclick="addVariant()">Add Variant</button>
<?php echo '<div class="form-group" id="variants-container">';
echo '<label for="variants-container" style="font-weight: bold;">Product Variants:</label>';
// Iterate through each variant
while ($variant = mysqli_fetch_assoc($data_variants)) {
if ($variant['varient_color'] !== null) {
$varient_size = explode(', ', $variant['varient_color']);
} else {
$varient_size = [];
}
// Create a row for each variant
echo '<div class="row mb-3 mt-2">';
echo '<div class="col">';
echo '<input type="text" name="variant[color][]" placeholder="Color" value="' . $variant['varient_color'] . '" class="form-control">';
echo '</div>';
echo '<div class="col">';
echo '<input type="text" name="variant[url][]" placeholder="url" value="' . $variant['product_url'] . '" class="form-control">';
echo '</div>';
// echo "<button type='button' name='delete_variant' value='" . $variant['product_varient_id'] . "'>Delete</button>";
// Close the row
echo '</div>';
}
// Close the variants container
echo '</div>'; ?>
</div>
<div class="form-group" id="images-container">
<label for="images" style="font-weight: bold;">Upload Image:</label><br>
<button type="button" id="images" class="btn btn-dark mb-3" onclick="addImage()">Add Image</button>
<?php
// Iterate through each image and display the input fields
while ($image = mysqli_fetch_assoc($data_images)) {
echo '<div class="mb-3">';
// Display the image with the existing file
echo '<img src="../product-images/' . $image['image_path'] . '" alt="Product Image" style="max-width:200px; max-height: 200px; display: block; margin-bottom: 10px;">';
// Display a hidden input field for the existing image
echo '<input type="hidden" name="existing_images[]" value="../product_images/' . $image['product_image_id'] . '">';
// Provide an option to delete the image
echo '<button type="button" class="btn btn-danger" onclick="deleteImage(this)">Delete Image</button>';
echo '</div>'; // Close the image container
}
?>
</div>
<div class="form-group" id="multiple-text-input">
<label for="images" style="font-weight: bold;">Include Tags:</label><br>
<button type="button" class="btn btn-dark mb-3" id="add-input-btn" onClick="addTextInput()">Add tags</button>
<?php foreach ($prod_tags as $tag) {
echo'<div class="input-container">';
echo '<input type="text" name="tags[]" value="' . $tag . '" class="text-input">';
// echo'<button type="button">Remove</button>';
echo'</div>';
}?>
</div>
<button type="submit" class="btn btn-danger btn-block mb-3" name="user_btn" style="font-size: 1.1rem;">Submit</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous">
</script>
<script>
// Function to add a new product variant
function addVariant() {
var variantsContainer = document.getElementById('variants-container');
// Create a new row for the variant
var newRow = document.createElement('div');
newRow.className = 'row mb-3 mt-2';
var urlCol = document.createElement('div');
urlCol.className = 'col-6';
var colorCol = document.createElement('div');
colorCol.className = 'col-6';
var urlInput = document.createElement('input');
urlInput.type = 'text';
urlInput.name = 'variant[url][]';
urlInput.placeholder = 'url';
urlInput.className = 'form-control';
urlCol.appendChild(urlInput);
// Create and add the quantity input field
var colorInput = document.createElement('input');
colorInput.type = 'text';
colorInput.name = 'variant[color][]';
colorInput.placeholder = 'color';
colorInput.className = 'form-control';
colorCol.appendChild(colorInput);
newRow.appendChild(urlCol);
newRow.appendChild(colorCol);
// Append the row to the variants container
variantsContainer.appendChild(newRow);
}
function addImage() {
// Get the images container
var imagesContainer = document.getElementById('images-container');
// Create a new div for the image input
var newDiv = document.createElement('div');
newDiv.className = 'mb-3';
// Create a new input of type file for the image
var newImageInput = document.createElement('input');
newImageInput.type = 'file';
newImageInput.name = 'images[]'; // Set name as an array
// Append the new input to the new div
newDiv.appendChild(newImageInput);
// Append the new div to the images container
imagesContainer.appendChild(newDiv);
}
function deleteImage(button) {
var div = button.parentElement;
div.style.display = 'none'; // Hide the div containing the image
// Create a hidden input to mark the image for deletion
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'images_to_delete[]';
input.value = div.querySelector('input[name="existing_images[]"]').value;
document.forms[0].appendChild(input);
}
function addTextInput() {
const inputContainer = document.getElementById("multiple-text-input");
const inputDiv = document.createElement("div");
inputDiv.classList.add("input-container");
const inputField = document.createElement("input");
inputField.setAttribute("type", "text");
inputField.setAttribute("name", "tags[]");
inputField.classList.add("text-input");
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
// Add event listener to the remove button
removeButton.addEventListener("click", function () {
inputDiv.remove(); // Remove the input field when the "Remove" button is clicked
});
inputDiv.appendChild(inputField);
inputDiv.appendChild(removeButton);
inputContainer.appendChild(inputDiv);
}
</script>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['user_btn'])) {
// Retrieve the product ID from the form (you need to add the product ID to your form)
$product_id = $ID;
// Retrieve form data
$prod_title = $_POST['prod_title'];
$prod_desc = $_POST['prod_desc'];
$prod_mrf = $_POST['prod_mrf'];
$prod_price = $_POST['prod_price'];
$prod_discount = $_POST['prod_discount'];
$prod_category = $_POST['category'];
$prod_category_value_int = (int) $prod_category;
$prod_status = $_POST['status'];
$tags = implode(', ', $_POST['tags']);
try {
// Begin the transaction
mysqli_begin_transaction($connection);
// Update product information
$updateProductQuery = "UPDATE product SET product_name = ?, category_id = ?, description = ?, price = ?, mrf = ?, product_discount = ?, status = ?, tags = ? WHERE product_id = ?";
$stmt = mysqli_prepare($connection, $updateProductQuery);
mysqli_stmt_bind_param($stmt, 'sisddsssi', $prod_title, $prod_category_value_int, $prod_desc, $prod_price, $prod_mrf, $prod_discount, $prod_status, $tags, $ID);
mysqli_stmt_execute($stmt);
// if (!$data_prod) {
// echo "Error updating product: " . mysqli_error($connection);
// }
// Update Variants:
if (isset($_POST['variant'])) {
foreach ($_POST['variant']['color'] as $index => $color) {
$color = $_POST['variant']['color'][$index];
$url = $_POST['variant']['url'][$index];
// Assuming you have fetched the variants data before this loop
while($result_variants = mysqli_fetch_assoc($data_variants)){
// Check if there are any variants available
if ($result_variants) {
$variant_id = $result_variants['product_varient_id'];
$updateVariantQuery = "UPDATE product_varients SET varient_color = ?, product_url = ? WHERE product_varient_id = ?";
$stmt = mysqli_prepare($connection, $updateVariantQuery);
mysqli_stmt_bind_param($stmt, 'ssi', $color, $url, $variant_id);
mysqli_stmt_execute($stmt);
} else {
echo "No variants found.";
}
}
}
}
// Update Stocks:
if (isset($_POST['product'])) {
// Fetch the initial row of stock data before the loop
$result_stock = mysqli_fetch_assoc($data_stock);
foreach ($_POST['product']['size'] as $i => $size) {
$size = $_POST['product']['size'][$i];
$quantity = $_POST['product']['quantity'][$i];
// Check if there are any stock entries available
if ($result_stock) {
$stock_id = $result_stock['product_stock_id'];
$updateStockQuery = "UPDATE product_stock SET size = ?, stock = ? WHERE product_stock_id = ?";
$stmt = mysqli_prepare($connection, $updateStockQuery);
mysqli_stmt_bind_param($stmt, 'sii', $size, $quantity, $stock_id);
mysqli_stmt_execute($stmt);
// Fetch the next row of stock data
$result_stock = mysqli_fetch_assoc($data_stock);
} else {
echo "No stock entries found.";
}
}
}
// Update product images if provided
if (isset($_FILES['images'])) {
// Loop through each uploaded image
foreach ($_FILES['images']['tmp_name'] as $index => $tmp_name) {
$fileName = basename($_FILES['images']['name'][$index]);
$filePath = $uploadDir . $fileName;
// Move the uploaded file to the desired directory
if (move_uploaded_file($tmp_name, $filePath)) {
$insertImageQuery = "INSERT INTO product_images (product_id, image_path) VALUES (?, ?) ON DUPLICATE KEY UPDATE image_path = ?";
$stmt = mysqli_prepare($connection, $insertImageQuery);
mysqli_stmt_bind_param($stmt, 'iss', $product_id, $filePath, $filePath);
mysqli_stmt_execute($stmt);
} else {
throw new Exception('Failed to upload products images.');
}
}
}
// Commit the transaction if everything is successful
mysqli_commit($connection);
echo '<script>alert("Product Updated Successfully")</script>';
// echo '<meta http-equiv="refresh" content="0; url=https://jbehari.aonebox.in/administration/admin-show-product.php">';
} catch (Exception $e) {
// Roll back the transaction in case of an error
mysqli_rollback($connection);
echo "Error: " . $e->getMessage();
}
}
?>
i want to update all the data in database
New contributor
Somnath Mondal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.