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

Time:05-02

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 >
            <h4 > <a href="../index.php">Branding</a> </h4>
            <ul>
                <li>Home</li>
                <li>Shop</li>
                <li>About</li>
            </ul>
        </div>
        <div >
            <button >Login</button>
        </div>
    </nav>


    <article>
        <form method="post" >
            <div >
                <div  name="name"> <?php echo $fetch['name']; ?> </div>
                <div >
                    <p > <?php echo $fetch['desc']; ?> </p>
                    <span  name="price"> $ <strong> <?php echo $fetch['price']; ?></strong> </span>

                </div>
                <div >
                    <div > </div>
                    <input type="number"  value="1" min="1">
                    <div >-</div>
                    <button  name="addcart" type="submit"> Add To Cart </button>
                    <a href="./cart.php">Go to cart</a>

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

    </article>

    <footer>
        <div >
            <div >
                <h3>Ecommerce Branding</h3>
                <span>School Activity</span>
            </div>
            <div >
                <h3>Colegio De San Lorenzo
                </h3>
                <span>Congressional Ave, Project 8, Quezon City, Metro Manila</span>
            </div>
            <div >
                <h3>Emman Cruz</h3>
                <span> [email protected]</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

CodePudding user response:

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";
}
?>
  • Related