将数据库项添加到购物车

时间:2016-04-29 02:50:13

标签: javascript php cart shopping

我试图弄清楚用户如何将我从数据库中提取的项目添加到他们的"购物车"中。我知道如何将我的数据库中的项目显示到下拉菜单中,但我仍然坚持用户如何选择项目并将其添加到购物车而无需将任何内容重新插入数据库。

这就是我所拥有的,但我很难让它工作......任何帮助??

    <?
session_start();
setlocale(LC_MONETARY, 'en_US');
$product_id = $_POST['Select_Product'];
$action = $_POST['action'];
switch ($action) {
    case "Add":
        $_SESSION['cart'][$product_id] ++;
        break;
    case "Remove":
        $_SESSION['cart'][$product_id] --;
        if ($_SESSION['cart'][$product_id] <= 0)
        unset($_SESSION['cart'][$product_id]);
        break;
    case "Empty":
        unset($_SESSION['cart']);
        break;
    case "Info":
        break;        
}

require_once(''); //the database(taken out for security)

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Cart</title>
        <script type="text/javascript" src="view.js"></script>
    <script>
            function productInfo(key) {

                //creates the datafile with query string
                var data_file = "AdventureCart.php?prodID=" + key;
                //this is making the http request
                var http_request = new XMLHttpRequest();
                try {
                    // Opera 8.0+, Firefox, Chrome, Safari
                    http_request = new XMLHttpRequest();
                } catch (e) {
                    // Internet Explorer Browsers
                    try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            // Something went wrong
                            alert("Your browser broke!");
                            return false;
                        }
                    }
                }
                http_request.onreadystatechange = function () {
                    if (http_request.readyState == 4)
                    {
                        var text = http_request.responseText;

                        //this is adding the elements to the HTML in the page
                        document.getElementById("productInformation").innerHTML = text;
                    }
                }
                http_request.open("GET", data_file, true);
                http_request.send();
            }
        </script>

    <link href="/CSIS2440/CodeEx/view.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <div class="form" id="form_container">
        <form action="AdventureCart.php" method="Post">
            <div>

                <p><span class="text">Please Select a product:</span>
                    <select id="Select_Product" name="Select_Product" onchange="productInfo(this.value)" class="select">
                        <option value=""></option>
                        <?php
                        //setting the select statement and running it
                        $search = "SELECT * FROM AdventureGear order by Item";
                        $return = mysql_query($search);

                        if (!$return) {
                            $message = "Whole query " . $search;
                            echo $message;
                            die("Invalid query " . $mysql_error());
                        }
                        while ($row = mysql_fetch_array($return)){
                            if ($row['idAdventureGear'] == $product_id){
                                echo "<option value='" .$row['idAdventureGear'] . "'selected='selected'>"
                                . $row['Item'] . "</option>";
                            }else{
                                echo "<option value='" . $row['idAdventureGear'] . "'>"
                                . $row['Item'] . "</option>";
                            }
                            }
                        ?>
                    </select></p>
                    <tr>
                        <td>
                            <input id="button_Add" type="submit" value="Add" name="Add" class="button"/>
                        </td>
                        <td>
                            <input name="action" type="submit" class="button" id="button_Remove" value="Remove"/>
                        </td>
                        <td>
                            <input name="action" type="submit" class="button" id="button_empty" value="Empty"/>
                        </td>
                        <td>
                            <input name="action" type="submit" class="button" id="button_Info" value="Info"/>
                        </td>
                    </tr>                    
                </table>

            </div>
            <div id="productInformation">

            </div>
            <div>
                <?php
                if ($infonum > 0){
                    $sql = "SELECT Item, Cost, Weight, ItemImage FROM AdventureGear WHERE idAdventureGear = " . $infonum;
                    echo "<table align ='left' width='100%'><tr><th>Name</th><th>Price</th><th>Weight</th></tr>";
                    $result = mysql_query($sql);
                    if (mysql_num_rows($result) > 0){
                        list($infoname, $infoprice, $infoweight, $infoimage) = mysql_fetch_row($result);
                        echo"<tr>";
                        echo"<td align=\"left\" width=\"450px\">$infoname</td>";
                        echo"<td align=\"left\" width=\"325px\">" . money_format('%(#8n', $infoproce) . " </td>";
                        echo"<td align=\"center\">$infoweight</td>";
                        echo"<td align=\"left\" width=\"450px\"><img src='images\\$infoimage' height=\"160\" width=\"160\"></td>";
                        echo "</tr>";
                    }
                    echo"</table>";
                }
                ?>
                You have no items in your shopping cart.
            </div>
        </form>
        </div>
    <?
    if ($_SESSION['cart']){
        echo"<table border=\"1\" padding=\"3\" width=\"650px\"><tr><th>Name</th><th>Weight</th><th>Price</th>"
        . "<th width=\"80px\">Line</th></tr>";
        foreach ($_SESSION['cart'] as $product_id => $quantity){
            $sql = "SELECT Item, Cost, Weight FROM AdventureGear WHERE idAdventureGear = ". $product_id . "";
            $result = mysql_query($sql);
            if (mysql_num_rows($result) >0){
                list($name, $price, $weight) = mysql_fetch_row($result);
                $weight = $weight * $quantity;
                $line_cost = $price * $quantity;
                $totWeight = $totWeight * $quantity;
                $total = $total + $line_cost;
                echo "<tr>";
                echo"<td align=\"Left\" width=\"450px\">$name</td>";
                echo"<td align ='\"center\" width=\"75px\">" . money_format('%(#8n', $price) . "</td>";
                echo"<td align=\"center\">" . money_format('%(#8n', $line_cost) . "</td>";
                echo"</tr>";
            }
        }
    }
    ?>
    </body>
</html>`enter code here`

1 个答案:

答案 0 :(得分:0)

您似乎在第1行

上缺少<?php