English 中文(简体)
增量职能似乎没有发挥作用
原标题:AddItem function does not seem to be working
  • 时间:2012-05-11 22:05:56
  •  标签:
  • php
  • mysql

我这样做是为了打造工作,但我的“附加”职能似乎并没有奏效。 我在产品页上点击“附加项目”链接,然后把我带上车页。 但是,这页上没有任何东西。 我喜欢这样说,我可以看到,告诉我,应该做些什么纠正。

增加项目的功能:

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 


            // 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);  
                    }       
            }

我只检查了我对数据库的368-cart表格,该表是空的。

mysql> select * from cs368_cart
-> ;
Empty set (0.00 sec)

因此,显然没有增加任何东西。 我想知道我的问问问是否正确?

我的表格:

mysql> select * from cs368_products
-> ;
+--------+----------------+---------------------------------------------+-----------+
| itemId | itemName       | itemDesc                                    | itemPrice |
+--------+----------------+---------------------------------------------+-----------+
|      1 | French Vanilla | A medium blend with a hint vanilla          |      9.79 | 
|      2 | Hazelnut Cream | A light blend with a spicy note of Hazelnut |      9.69 | 
|      3 | Columbian      | A medium-dark blend straight up             |      9.89 | 
+--------+----------------+---------------------------------------------+-----------+
3 rows in set (0.00 sec)

和我的图表;

mysql> show columns from cs368_cart;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| cartId   | int(11)     | NO   | PRI | NULL    | auto_increment | 
| cookieId | varchar(50) | NO   |     |         |                | 
| itemId   | int(11)     | YES  |     | NULL    |                | 
| qty      | int(11)     | YES  |     | NULL    |                | 
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

这是我的“智商” Id I have in a seperate php file, which is bieng known better by the php file with this AddItem function.

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

它想像你试图插入<条码>c 页: 1 更仔细地看一下你在PHP的座椅。

问题回答

您应当把问话改为与下文相似之处,这将告诉你你,你插入的教文是错误的(处理任何错误也是良好做法)。

<?php
    $queryResult = mysql_query("insert into cs368_cart(cookieID, itemId, qty) values( " . GetCartID() . " , $itemId, $qty)");
if (!$queryResult) {
    die( Invalid query:   . mysql_error());
}

?>

http://php.net/manual/en/Function.mysql-query.php rel=“nofollow”http://php.net/manual/en/Function.mysql-query.php





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签