0

I am a beginner. I am developing an e-commerce website. My problem is that I can't seem to add multiple items in my cart. If I clicked an item it will be added to the cart but when I clicked another item it will just replaced the first item that I picked. Any suggestions on how I can fixed that? Thank you

<!-- This is my index.html. You can browse my products here. This is just a sample of my code. It's too long. Sry. -->

<form method = "GET" action = "php/addtocartprocess.php">
  <p>Price: 9,199 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart1" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p>
  <p>Price: 28,890 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart2" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p>
  <p>Price: 46,995 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart3" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p></form>

<?php

// this is my php code. I don't know if it right or not
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
$connection = mysql_connect("localhost", "root", "");

if(isset($_GET['add_to_cart1'])){
 $_SESSION['prod_id'] = 1;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart2'])){
 $_SESSION['prod_id'] = 2;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart3'])){
 $_SESSION['prod_id'] = 3;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart4'])){
 $_SESSION['prod_id'] = 4;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart5'])){
 $_SESSION['prod_id'] = 5;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart6'])){
 $_SESSION['prod_id'] = 6;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart7'])){
 $_SESSION['prod_id'] = 7;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart8'])){
 $_SESSION['prod_id'] = 8;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart9'])){
 $_SESSION['prod_id'] = 9;
 header("location: ../android.php");
}
if(isset($_GET['add_to_cart10'])){
 $_SESSION['prod_id'] = 10;
 header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart11'])){
 $_SESSION['prod_id'] = 53;
 header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart12'])){
 $_SESSION['prod_id'] = 54;
 header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart13'])){
 $_SESSION['prod_id'] = 55;
 header("location: ../windows.php");
}
if(isset($_GET['add_to_cart14'])){
 $_SESSION['prod_id'] = 56;
 header("location: ../windows.php");
}
if(isset($_GET['add_to_cart15'])){
 $_SESSION['prod_id'] = 57;
 header("location: ../windows.php");
}
if(isset($_GET['add_to_cart16'])){
 $_SESSION['prod_id'] = 58;
 header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart17'])){
 $_SESSION['prod_id'] = 59;
 header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart18'])){
 $_SESSION['prod_id'] = 60;
 header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart19'])){
 $_SESSION['prod_id'] = 61;
 header("location: ../computer.php");
}
if(isset($_GET['add_to_cart20'])){
 $_SESSION['prod_id'] = 62;
 header("location: ../computer.php");
}
if(isset($_GET['add_to_cart21'])){
 $_SESSION['prod_id'] = 64;
 header("location: ../computer.php");
}
$gadgets = $_SESSION['prod_id'];
mysql_close($connection);
?>

<!-- This is my cart.php code -->

<?php
include('php/userloginprocess.php'); // Includes Login Script
include('php/addtocartprocess.php');

/*$id = $_SESSION['prod_id'];*/
$gadgets = $_SESSION['prod_id'];
if($_SESSION['login'] == FALSE){
 header("location: login.php");
}
if($_SESSION['login'] == TRUE)
?>
<?php
//connection
mysql_connect("localhost","root", "") or die(mysql_error());
//database connection
mysql_select_db("marketech_db") or die(mysql_error());
//sql query
$sql = "SELECT * FROM user_tbl";
$records=mysql_query($sql);
?>
<html>
<head>
<title> Marketech | Buy Genuine Gadgets Online </title>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="shortcut icon" href="images/marketechlogo.ico" />
</head>
<body>
<!-- First Navigation Bar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Marketech</a>
    </div>
<!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
  <li><a href="#"><span class = "glyphicon glyphicon-shopping-cart"></span> Cart</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $_SESSION ['fname']; ?><span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="useraccountsettings.php">Account Settings</a></li>
            <li><a href="php/logout.php">Logout</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>
<!-- Second Navigation Bar -->
<br><br><br>
<div class = "container-fluid">
<nav class="navbar navbar-inverse navbar-lower">
  <div class="container-fluid">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
      <ul class="nav navbar-nav col-lg-12">
        <li class="col-md-2"><a href="android.php">Android Phone</a></li>
        <li class="col-md-2"><a href="iphone.php">iPhone</a></li>
        <li class="col-md-2"><a href="windows.php">Windows Phone</a></li>
        <li class="col-md-2"><a href="smartwatch.php">Smart Watch</a></li>
        <li class="col-md-2"><a href="computer.php">Computers</a></li>
        <li class="dropdown col-md-2"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Popular Brands <span class="caret"></span></a>
   <ul class="dropdown-menu col-xs-2"> 
    <li><a href="android.php#samsung">Samsung</a></li>
    <li><a href="iphone.php">Apple</a></li>
    <li><a href="windows.php">Microsoft</a></li>
    <li><a href="android.php#sony">Sony</a></li>
    <li><a href="android.php#asus">Asus</a></li>
   </ul>
  </li>
      </ul>
  </div>
    </div>
  </div>
</nav>
<!-- Marketechs Third Navigation (with glyphicons) -->
<div class="container">
 <ul class="list-unstyled">
   <li class = "col-md-3"><span class="glyphicon glyphicon-transfer"></span> 30 days Free Returns</li>
   <li class = "col-md-3"><span class="glyphicon glyphicon-plane"></span> Free Delivery Above 999 php</li>
   <li class = "col-md-3"><span class="glyphicon glyphicon-usd"></span> Cash on Delivery</li>
   <li class = "col-md-3"><span class="glyphicon glyphicon-earphone"></span> Call (049) 557 2681</li>
 </ul>
</div>
</div>
<!-- Cart -->

<div class = "container-fluid">
<form method = "GET" action = "deletecartproduct.php">
 <div class = "col-md-9">
  <table class = "table table-responsive table-hover">
   <tr>
    <th>Delete</th>
    <th>Item Name</th>
    <th>Item Price</th>
    <th>Image</th>
   </tr>
<?php
//connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("marketech_db") or die (mysql_error());
$sql = "SELECT * FROM product_tbl WHERE prod_id='$gadgets'";
$prod_records = mysql_query($sql);
while($products=mysql_fetch_assoc($prod_records)){
  echo "<tr>";
  echo  "<td>" .'<input type="checkbox" name="products[]" value='.$products['prod_id'].'>'."</td>";
  echo "<td>".$products['item_name']."</td>";
  echo "<td>".$products['item_price']."</td>";
  echo "<td>".$products['item_image']."</td>";
  echo "</tr>";
}
?>
  </table>
  <input type ="submit" class = "btn btn-danger" name = "delete_cart_item" value = "Delete"></form>
 </div>
</div>
<br><br><br><br><br>
<!-- Footer Navigation -->
<nav class="navbar navbar-default">
  <div class="container">
  <ul class ="list-unstyled">
   <li class = "col-md-2"><h5><b>Call Us: (049) 557 2681</b></h5></li>
   <li class = "col-md-2"><h5><b><a href = "about.html"><font color = "black">About Us </b></h5></font></a></li>
   <li class = "col-md-2"><h5><b><a href = "contact.html"><font color = "black">Contact Us</b></h5></font></a></li>
   <li class = "col-md-2"><h5><b><a href = "privacy.html"><font color = "black">Privacy</b></h5></font></a></li>
   <li class = "col-md-2"><h5><b><a href = "#"><font color = "black">Terms of Service</b></h5></font></a></li>
   <li class = "col-md-2"><h5><b>Copyright (c) 2016<h5></li>
 </ul>
  </div>
</nav>
<!-- 
Members:
Marlon Mendoza
-->
</body>
</html>
Pete
  • 57,112
  • 28
  • 117
  • 166
  • What do you mean generic function? Sorry newbie here. – Marlon Mendoza Dec 07 '16 at 12:09
  • You should definitely consider creating a form field for each product, containing a button with name="action" & value="add_to_cart", as well as a hidden field containing the ID of the current product. That way you dont need that giant function, and can instead look up the ID of the product that is passed to the PHP script. – Emil Rosenius Dec 07 '16 at 12:12
  • You may get an idea for a shopping cart from here http://www.peachpit.com/articles/article.aspx?p=1962481 – dimis283 Dec 07 '16 at 12:20
  • but i am having problem in showing all the added items in the cart.php in a table. – Marlon Mendoza Dec 08 '16 at 02:14

3 Answers3

1

You are using primitive session variable: $_SESSION['prod_id'], instead it should be an array.

Initialise it like this:

$_SESSION['prod_ids'] = [];

Then when a product is added/removed to cart, update $_SESSION['prod_ids']

array_push($_SESSION['prod_ids'],/*product_id*/)
Kalpesh Patel
  • 2,772
  • 2
  • 25
  • 52
  • how can I use it in cart.php to show the items in a ? Can I use this: include("addtocartprocess.php"); $prod_id = $_SESSION['prod_ids']; and then in my sql syntax "SELECT * FROM product_tbl where prod_id = '$prod_id'"; I tried using that but it didn't work.
    – Marlon Mendoza Dec 08 '16 at 02:11
  • @MarlonMendoza, can you post ur code somewhere, I am not able to understand your scenarios, exactly. – Kalpesh Patel Dec 08 '16 at 05:01
  • here's my android.php (you can browse the items here): https://gist.github.com/anonymous/4d127e07e456d0655644e5ee293b4cf2 here's my addtocartprocess.php: https://gist.github.com/anonymous/e78d6e2a0f96d18b54ec0763801cb1a5 and here's my cart.php (where you can see the items that you added in the cart): https://gist.github.com/anonymous/a67abaaa3f2c0728a70c3e527335f474 I am having problem in storing multiple items in the cart and displaying it. – Marlon Mendoza Dec 08 '16 at 09:55
  • You have to convert array to comma separated string using implode() function. And them use mysql IN keyword to select product ID's in array. – Kalpesh Patel Dec 08 '16 at 10:04
  • Refer this: http://stackoverflow.com/questions/9476146/mysql-php-select-where-id-array – Kalpesh Patel Dec 08 '16 at 10:05
  • I did but then I got this error: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given – Marlon Mendoza Dec 08 '16 at 10:08
  • Here is the addtocartprocess.php: http://phpfiddle.org/api/raw/46514909ab9f9aa9746797aca0b2d5f4 here is the android.php: http://phpfiddle.org/api/raw/1cef51435c65917cbdf18391e9bee0c0 here is the cart.php: http://phpfiddle.org/api/raw/aaec3a61d8186f1d8e88b8a725aba914 – Marlon Mendoza Dec 08 '16 at 10:50
0

You should consider doing the following:

Add a hidden field to your HTML containing the product ID if the current product.

<p>Price: 9,199 Php<br>
<input class="btn btn-success" type="submit" name="action" value="add_to_cart">
<input type="hidden" name="product_id" value="1">
<span class="glyphicon glyphicon-shopping-cart"></span>
</p>

Then you can refactor your PHP to this

<?php

if (session_status() == PHP_SESSION_NONE) {
    session_start();
    $_SESSION['cart'] = array();
}

$connection = mysql_connect("localhost", "root", "");

if(isset($_GET['add_to_cart'])){
    array_push($_SESSION['cart'], $_GET['product_id'];
    header("location: ../" . $_GET['product_id'] . ".php");
}

mysql_close($connection);

Notice how you can initialize $_SESSION['cart'] as an array, when creating the session object. Instead of assigning the product ID to $_SESSION['cart'], you can push it into the array.

Refactor the name of the php file which you set in your header, or add an additional hidden field containing the name of the file.

Emil Rosenius
  • 961
  • 1
  • 9
  • 24
  • I tried using this but I don't know what is the needed variable if I want to show the items in a table in cart.php Should I use $prod_id = $_GET['product_id']; ? Because I need it in my select statement. "SELECT * FROM product_tbl where prod_id = $prod_id"; – Marlon Mendoza Dec 08 '16 at 01:54
0

If you have 1000 items to add to cart you can not write 1000 if else statement.Better use loops.I created a sample code for what you want take reference from that and use your own logic to create what you want.happy coding.

check  https://jsfiddle.net/qun3ajL9/