0

Would appreciate somebody's help? Rookie here.

I wish to create a cart table for the user when they add an item to cart and quantity is set. This table will be named 'CART_6' if it is user 6, and if a table already exists, then a new row will be added instead. A cookie will then take the sum of quantities in the cart (not sure how to do this, but I will figure that out later). For the moment I am having trouble with the SQL syntax especially around the cart table name that reflects the php variable $user. In particular it is throwing an error for the first @ symbol which I have never used before.

if(isset($_POST["quantity"])){

    $quantity = $_POST['quantity'];

    $query = mysqli_query($db_conx, 'CART_'.$user);
    if ($query === FALSE) {

        DECLARE @table_name varchar(max)
        SET @table_name = 
        (SELECT 'CART_' + mysql_real_escape_string($user) )

            $nquery = "CREATE TABLE " + @tablename" (
                    ID int(11) AUTO_INCREMENT,
                    product_name varchar(255) NOT NULL,
                    price int(11) NOT NULL,
                    quantity int(5) NOT NULL,
                    added DATETIME NOT NULL,
                    PRIMARY KEY (ID)
                      )";
            $result = mysqli_query($db_conx, $nquery);
    }

    INSERT INTO @table_name (product_name,price,quantity,added)
    VALUES ($name,$p,$quantity,now());

    $cookie_name = "$user";
    $cookie_value = "sum of quantities";
    setcookie($cookie_name, $cookie_value, time() + (86400), "/"); // 86400 = 1 day

}

1 Answers1

0

Your @ usage is related to using table names are variables which is not allowed as stated by @dcp, see Table name as variable

Community
  • 1
  • 1