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 7926323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:44:19+00:00 2026-06-03T18:44:19+00:00

I have a php file that includes two functions, one to connect to the

  • 0

I have a php file that includes two functions, one to connect to the database and one to set cookied for the cart. Here is that file:

<?php
$dbServer="localhost";
$dbName="test";
function ConnectToDb($server, $database){
    $s=@mysql_connect($server);
    $d=@mysql_select_db($database, $s);
    if(!$s || !$d)
    return false;
    else
    return true;
}

function GetCartId(){
    if(isset($_COOKIE["cartId"])){
    return $_COOKIE["cartId"];
}
else {
    session_start();
    setcookie("cartId", session_id(), time()+((3600*24)*30));
    return session_id();
}
}
?>

The function for connecting to the database works well in another php file for this particular program. I am having a problem with it in this file:

<?php
include("db.php");

    switch($_GET["action"]) {
            case "add_item":
            {
                    AddItem($_GET["id"], $_GET["qty"]);
                    ShowCart();
            break;
            }
            case "update_item": {
                    UpdateItem($_GET["id"], $_GET["qty"]);
                    ShowCart();
            break;
            }
            case "remove_item": {
                    RemoveItem($_GET["id"]);
                    ShowCart();
            break;
            }
            default: {
                    ShowCart();
            }
    }

    function AddItem($itemId, $qty) {
            // Will check whether or not this item
            // already exists in the cart table.
            // If it does, the UpdateItem function
            // will be called instead


            $cxn = @ConnectToDb($dbServer, $dbName);
            // Check if this item already exists in the users cart table
            $result = mysql_query("select count(*) from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
            $row = mysql_fetch_row($result);
            $numRows = $row[0];

            if($numRows == 0) {
                    // This item doesn't exist in the users cart,
                    // we will add it with an insert query
                    @mysql_query("insert into cs368_cart(cookieID, itemId, qty) values('" . GetCartID() . "', $itemId, $qty)");
            }
            else {
                    // This item already exists in the users cart,
                    // we will update it instead

                    UpdateItem($itemId, $qty);
                    }
            }

    function UpdateItem($itemId, $qty) {
            // Updates the quantity of an item in the users cart.
            // If the qutnaity is zero, then RemoveItem will be
            // called instead

            $cxn = @ConnectToDb($dbServer, $dbName);

            if($qty == 0) {
                    // Remove the item from the users cart
                    RemoveItem($itemId);
            }
            else {
                    mysql_query("update cs368_cart set qty = $qty where cookieID = '" . GetCartID() . "' and itemId = $itemId");
                    }
            }

    function RemoveItem($itemId) {
            // Uses an SQL delete statement to remove an item from
            // the users cart
            $cxn = @ConnectToDb($dbServer, $dbName);
            mysql_query("delete from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
    }

    function ShowCart() {
            // Gets each item from the cart table and display them in
            // a tabulated format, as well as a final total for the cart
            $cxn = @ConnectToDb($dbServer, $dbName);
            $result = mysql_query("select * from cs368_cart inner join cs368_products on cart.itemId =
                    items.itemId where cart.cookieID = '" . GetCartID() . "' order by items.itemName asc")
                     or die("Query to get test in function ShowCart failed with error: ".mysql_error());
?>

What can I do the remedy this problem? Thanks!

  • 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-03T18:44:20+00:00Added an answer on June 3, 2026 at 6:44 pm

    First: lose the @, and put some proper error handling in there (those functions return false when something goes wrong, and you can use mysql_error and mysql_errno to log it).

    Second: mysql_real_escape_string and intval on those $_GET parameters before someone sneaks in some extra code through the URL.

    Third: you’re accessing $dbServer and $dbName as variables local to the function UpdateItem, rather than global to the script. You should only connect to the database once (in the original db.php file), and let the query functions take care of the rest (since there’s only one connection, they all default to that one anyway).

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

Sidebar

Related Questions

The setup: I have a standard .php file (index.php) that contains two includes, one
i have two php files where one is executed through url and that file
I have a PHP script (index.php) that includes an index.html file in a sub-directory
Within my HTML, I have a php script that includes a file. At that
I have a php file that holds my login details named connect.php. $host =
This question has two parts: #1 I have a functions.php that is filled with
I have a index.php file that will include several external files: content/templates/id1/template.php content/templates/id2/template.php content/templates/id3/template.php
I have a php file that looks like this: <?php include(config.php); // put the
I have a PHP file that contains a lot of if and else statements
I have a php file that contains a form (which contains 2 input boxes

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.