无法添加到其他.php页面上的购物车

Unable to add to cart on other .php pages

本文关键字:购物车 php 添加 其他      更新时间:2023-09-26

我正在尝试创建一个简单的电子商务网站。一切都很顺利,但我只能在index.php中添加产品。当我尝试单击all_products.php页面上的add_cart按钮时,它会将我重定向到index.php页面,并且不会将产品添加到购物车中。

我希望能够在all_products.php页面上添加产品。我的代码有什么问题:

function.php(创建"添加到购物车"按钮的功能以及单击"添加到车"按钮时会发生什么的功能:

function getProd(){
    if(!isset($_GET['cat'])){
    global $con;
    $get_prod = "select * from item order by RAND() LIMIT 0, 6";
    $run_prod = mysqli_query($con, $get_prod);
    while($row_prod = mysqli_fetch_array($run_prod)){
        $prod_id = $row_prod['ItemId'];
        $prod_name = $row_prod['ItemName'];
        $prod_desc = $row_prod['ItemDesc'];
        $prod_price = $row_prod['ItemPrice'];
        $prod_qty = $row_prod['ItemQty'];
        $prod_cat = $row_prod['ItemCat'];
        $prod_img = $row_prod['ItemImg'];
        echo "
            <div id='single_product'>
                <img src='../admin_int/product_images/$prod_img' width='180' height=180'/>
                <h4>Name:</h4><h4> $prod_name</h4>
                <p><b>Price: $ $prod_price</b></p>
                <a href='details.php?prod_id=$prod_id' style='float:left; font-size:19px;padding-top:10px;text-decoration:none'>Details</a>
                <a href='index.php?add_cart=$prod_id'><button style = 'float:right; border:1; border-radius:12px; color:blue;
                 height:50px; width:50px;
                 background-color: #80ff80'>
                Add to Cart</button></a> //THIS IS THE CREATION OF ADD TO CART BUTTON
            </div>
        ";
    }
}// if(!isset($_GET['cat'])){
}//END OF getProd()

cart()//这就是add to cart按钮要执行的操作。

function cart(){
if(isset($_GET['add_cart']))
{   
    global $con;
    $ip = getIp();
    $pro_id = $_GET['add_cart'];
    $check_pro = "select * from cart where ip_add = '$ip' AND p_id = '$prod_id" ;
    $run_check = mysqli_query($con, $check_pro);
        if(mysqli_num_rows($run_check)>0){
            echo "";
        }
        else {
            $insert_pro = "insert into cart (orderId,ip_add) values ('$pro_id','$ip')";
            $run_pro = mysqli_query($con, $insert_pro);
            echo "<script> window.open('all_products.php','_self')</script>";
        }
}
}//cart()

all_products.php

     <!DOCTYPE html>
<?php 
include("../functions/function.php");
?>
<html>
    <head>
    <title>Online Shop</title>
    <link rel='stylesheet' href='../CSS/style.css' media="all"/>
    </head>
<body>  
            <!--Main Wrapper starts here!-->
    <div class="main_wrapper"> 
            <!--Header Wrapper starts here!-->
        <div class="header_wrapper" >
        <a href='index.php'><img id="logo" src="../Pictures/logo.png"/></a>
        </div>
            <!--Header ends here!-->
            <!--Menu Bar starts here!-->
        <div class="menubar">
        <ul id="menu">
            <li><a href="index.php"> Home </a></li> 
            <li><a href="all_products.php"> All Products </a></li>
            <li><a href="customer/my_account.php"> My Account </a></li>
            <li><a href=""> Sign Up </a></li>
            <li><a href="cart.php"> Shopping Cart </a></li>
            <li><a href="#"> Contact Us  </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query"  placeholder="Search Product"/>
                <input type="submit" name="search" value="search" style='background-color:#80dfff; border:none;'/>
            </form> 
        </div>
        </div><!--Menubar ends here!-->
            <!--Content Wrapper here!-->
        <div class="content_wrapper">
            <div id="sidebar"> 
                <div id="sidebar_title"> Categories</div>
                <ul id="cats">
                    <?php getCats(); ?>     
                </ul>   
            </div>
            <!--CONTENT AREA starts here-->
            <div id="content_area">
                <?php cart(); ?>
                <div id='shopping_cart'>
                    <span style='float:right; font-size:18px; padding-right:5px; padding:5px; line-height:40px;'>
                     Welcome Guest!
                      <b style='color:#336600'>SHOPPING CART</b>
                      Total Items:<?php total_items(); ?>
                       Total Price: <?php total_price(); ?>
                     <a href="cart.php" style='color:#336600'>Go to Cart</a>
                    </span>
                </div>
                <div id="product_box">
                <!--CODE IN SHOWING ALL PRODUCTS-->
                <?php   
                    $get_prod = "select * from item order by ItemName";
                    $run_prod = mysqli_query($con, $get_prod);
                    while($row_prod = mysqli_fetch_array($run_prod)){
                        $prod_id = $row_prod['ItemId'];
                        $prod_name = $row_prod['ItemName'];
                        $prod_desc = $row_prod['ItemDesc'];
                        $prod_price = $row_prod['ItemPrice'];
                        $prod_qty = $row_prod['ItemQty'];
                        $prod_cat = $row_prod['ItemCat'];
                        $prod_img = $row_prod['ItemImg'];
                    echo "
                    <div id='single_product'>
                    <img src='../admin_int/product_images/$prod_img' width='180' height=180'/>
                    <h4>Name:</h4><h4> $prod_name</h4>
                    <p><b>Price: $ $prod_price</b></p>
                    <a href='details.php?prod_id=$prod_id' style='float:left; font-size:19px;padding-top:10px;text-decoration:none'>Details</a>
                    <a href='index.php?prod_id=$prod_id'><button style = 'float:right; border:1; border-radius:12px; color:blue;
                     height:50px; width:50px;
                     background-color: #80ff80'>
                    Add to Cart</button></a>
                    </div>
                    ";
                    }
                    ?> <!--END OF ALL PRODUCTS-->
                </div>
            </div><!--END OF CONTENT AREA-->
        </div><!--Content Wrapper ends here!-->
        <div id="footer"><h2 style='text-align:center; padding-top:25px;'>&copy; Jerlon Buenconsejo 2015</h2> </div>
    </div><!--Wrapper-->
</body>
</html>

您被重定向到index.php,因为这是您的链接中指定的页面:

<a href='index.php?prod_id=$prod_id'>

将其更改为页面的名称。此外,您还需要在页面顶部调用cart()函数。

您应该考虑调用一个唯一的页面,例如addCaddy.php,它将重定向到插入产品后所在的页面,或者您可以使用ajax调用它。