Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:27:55+00:00 2026-06-15T16:27:55+00:00

i am working on a mini shopping cart. Everything seems to work, however, it

  • 0

i am working on a mini shopping cart. Everything seems to work, however, it does not extract the right data when i click on a different product for e.g.

I choose Product 1 – It gives me the right information but if i choose Product 2 it will give me the same information as product 1.

I will display my code below, but i believe the error is coming from this line of code:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 

I don’t think its getting the $bikecode

This is the whole code within the php file:

<?php
    $bikecode = $_GET['id'];     //the product id from the URL 
    $action = $_GET['action']; //the action from the URL

if($bikecode && !productExists($bikecode)) {
    die("Product Doesn't Exist");
} 

    switch($action) {   //decide what to do 

        case "add":
            $_SESSION['cart'][$bikecode]++; //add one to the quantity of the product with id $bikecode 
        break;

        case "remove":
            $_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode 
            if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
        break;

        case "empty":
            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;
    }

    if($_SESSION['cart']){

        echo "<table width=\"100%\">";   

          foreach($_SESSION['cart'] as $bikecode => $quantity) { 

              $sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode); 

                $result = mysqli_query($con, $sql);

                if(mysqli_num_rows($result) > 0) {

                    list($model, $price) = mysqli_fetch_row($result);   

                    $cost = $quantity * $price;
                    $total = $total + $cost;

                        echo "<tr><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>";
                        echo "<tr>";
                        echo "<td align=\"center\">$model</td>";
                        echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$bikecode\">X</a></td>";
                        echo "<td align=\"center\">£$cost</td>";
                    echo "</tr>";
                }
            }

            echo "<tr>";
                echo "<td colspan=\"2\" align=\"right\">Total</td>";
                echo "<td align=\"right\">£$total</td>";
            echo "</tr>";

            echo "<tr>";
                echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
            echo "</tr>";       
        echo "</table>";

    }else{
        echo "You have no items in your shopping cart.";
    }

function productExists($bikecode) {
    $sql = sprintf("SELECT * FROM Bike WHERE BikeCode = %d;", $bikecode); 
    return mysqli_num_rows(mysqli_query($con, $sql)) > 0;
}
?>

Help would be extremely appreciated but if you cannot find an error could you please direct me in another way of displaying this code:

$sql = sprintf("SELECT Model, Price FROM Bike WHERE BikeCode = %d;", $bikecode);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:27:56+00:00Added an answer on June 15, 2026 at 4:27 pm

    It looks like you got it but for easy reference to others that might search it the problem is that you were using %d in sprintf() to try and represent a varchar when %d is treated as an integer. Using %s will solve the problem.

    sprintf() – Manual

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The transitions are not working and alse scrolling on smartphones doesnt work too and
I'm working on developing a mini-language in Python (not really, just a few commands
I am working on a mini game for Android. When I click and hold
I'm working on testing a shopping cart, checkout, payment process on Zend Framework with
I am working on a mini-framework for runnable things. (They are experiments, tests, tasks,
I'm working on a mini windows process explorer in C, I have a handle
I am working on a little mini compiler while trying to learn some MIPS
I'm working with some friends on a small private mini MMORPG game server, and
I'm just developing my own mini framework for an application I was working on,
Recently, i am working with jquery for my mini-project. I took a code from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.