-1

私はこれをちょっとした仕事に行きますが、私のPASSWORD関数はうまくいかないようです。商品ページの「アイテムを追加」リンクをクリックすると、カートページに移動します。しかし、カートページには何もありません。私は誰かがそれを見て、それを修正するために何をすべきかを教えてくれることを望んでいます。

PASSWORD関数:

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

データベースのcs368_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)

これは、別のphpファイルにあるGetCartIdであり、このPASSWORD関数を使用してphpファイルによって正しく呼び出されます。

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

2 に答える 2

1

挿入クエリを以下のようなものに変更する必要があります。これにより、挿入状態の何が問題になっているのかがわかります(エラーを適切に処理することもお勧めします)。

<?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の例に基づく

于 2012-05-11T23:26:54.733 に答える
1

cartIdテーブルがcartId整数の場合、文字列として挿入しようとしているようです。PHP文字列のSQLを詳しく見てください。

于 2012-05-11T23:27:06.483 に答える