Skip to content
Advertisement

How can i automatically save the image inside a website when i click the add to cart?

how can I automatically save this pizza image, and save it to my local folder? I can easily save the other information, but I’m encountering automatically saving the image itself in a local folder?

I’m fetching the other data from different table, but the I can’t save the image itself. I’m have no intention of using foreign key for no.

backend.php

if (isset($_POST['addcart'])) {
    $con = connection();
    $fetch = singleInfo();
    $name = $fetch['name'];
    $price = $fetch['price'];
    $image = $fetch['image'];


    $new_image = '../images/' . $image;

    $stmt = $con->prepare("INSERT INTO `cart`(`name`, `price`,`image`) VALUES ('$name','$price','$new_image')");

    $stmt->execute();
}

index.php

  <?php

session_start();


require('../backend/clientbackend.php');
$fetch = singleInfo();
$current_price = $fetch['price'];





?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style/style.css">
    <title>E-Commerce</title>
</head>

<body>
    <nav>
        <div class="left">
            <h4 class="navbar-header"> <a href="../index.php">Branding</a> </h4>
            <ul>
                <li>Home</li>
                <li>Shop</li>
                <li>About</li>
            </ul>
        </div>
        <div class="right">
            <button class="loginButton">Login</button>
        </div>
    </nav>


    <article>
        <form method="post" class="product-description">
            <div class="left">
                <div class="left-title" name="name"> <?php echo $fetch['name']; ?> </div>
                <div class="left-info">
                    <p class="left-description"> <?php echo $fetch['desc']; ?> </p>
                    <span class="price" name="price"> $ <strong> <?php echo $fetch['price']; ?></strong> </span>

                </div>
                <div class="left-increment">
                    <div class="addition">+</div>
                    <input type="number" class="current_value" value="1" min="1">
                    <div class="subtraction">-</div>
                    <button class="gotoCart" name="addcart" type="submit"> Add To Cart </button>
                    <a href="./cart.php">Go to cart</a>

                </div>
            </div>
            <div class="right">
                <div class="right-image">
                    <img name="image" src="<?php echo '../uploads/' . $fetch['image']; ?>" alt="">
                </div>
            </div>
        </form>

    </article>

    <footer>
        <div class="footer-container">
            <div class="box1">
                <h3>Ecommerce Branding</h3>
                <span>School Activity</span>
            </div>
            <div class="box2">
                <h3>Colegio De San Lorenzo
                </h3>
                <span>Congressional Ave, Project 8, Quezon City, Metro Manila</span>
            </div>
            <div class="box3">
                <h3>Emman Cruz</h3>
                <span> zurcemozz@gmail.com</span>
            </div>
        </div>
    </footer>

    <script>
        const addBtn = document.querySelector('.addition');
        const subBtn = document.querySelector('.subtraction');
        let currentValue = document.querySelector('.current_value');


        let stock = 1;


        addBtn.addEventListener("click", function() {
            stock = stock + 1
            currentValue.value = stock;
            console.log(currentValue.value);


        })
        subBtn.addEventListener("click", function() {

            if (stock <= 0) {
                stock = 0;
            } else {
                stock = stock - 1
                currentValue.value = stock;
                console.log(currentValue.value);
            }

        })
    </script>
</body>

</html>

sample image

Advertisement

Answer

you can copy the image file from ‘../uploads/’ to ‘../images/’ and then you save it . you can do this with copy function

copy documentations

copy() example :

<?php
$image = '../uploads/'.$fetch['image'];
$new_image= '../images/'.$fetch['image'];

if (!copy($image , $new_image)) {
    echo "failed to copy $image ...n";
}
?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement