无法从其他页面添加到购物车

时间:2015-12-30 20:28:01

标签: php

我能够在我的索引页面上添加产品,但在我的所有产品页面中,以及在我搜索时,它只刷新页面但不添加产品。所以我试图为我的所有产品页面创建一个不同的功能。但它仍然没有进入购物车但只刷新页面。为什么呢?

add to cart按钮的代码

<?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_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>
                    <p><b>Quantity</b><input type='number' value='1' name='prod_quan' placeholder='1' min='1' max='9' size='2'/></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='all_products.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>
                    ";
                    }
                    ?>

点击add to cart后要执行的操作。

    function AllProdcart(){
if(isset($_GET['add_cart']))
{   
    global $con;
    $ip = getIp();
    $pro_id = $_GET['add_cart'];
    $prod_quan = $_POST['prod_quan'];
    $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,qty) values ('$pro_id','$ip','$prod_quan')";
            $run_pro = mysqli_query($con, $insert_pro);
            echo "<script> window.open('all_products.php','_self')</script>";
        }
}
}//cart()

这是我调用我的函数AllProdcart();

的地方
 <!DOCTYPE html>
<?php 
session_start();
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="cart.php"> Shopping Cart </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 AllProdcart(); ?>
                <div id='shopping_cart'>
                    <span style='float:right; font-size:18px; padding-right:5px; padding:5px; line-height:40px;'>
                <?php 
                    if(isset($_SESSION['customer_email'])){
                        echo "<b>Welcome: </b> ". $_SESSION['customer_email'] .  "<b style='color:#336600;'> Your</b>";
                    }else{
                        echo " 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>
                     <?php 
                     if(!isset($_SESSION['customer_email'])){
                        echo "<a href='checkout.php' style='color:#336600;'>Login</a>";
                     }
                     else{
                        echo "<a href='logout.php' style='color:#336600;'>Logout</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_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>
                    <p><b>Quantity</b><input type='number' value='1' name='prod_quan' placeholder='1' min='1' max='9' size='2'/></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='all_products.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>     

1 个答案:

答案 0 :(得分:1)

  

请参阅以下链接php.net/manual/en/mysqli.error.php&lt;(对您的查询)和php.net/manual/en/function.error-reporting.php并将其应用于您的代码。 - Fred -ii- 7分钟前

我强烈建议您使用这些链接(及其上的函数)来确定代码中是否存在MySQL错误。

此外,在SQL语句中使用*绝对不是一个好习惯,因为它可能会导致服务器上出现不必要的负载。