PHP购物车产品ID

时间:2015-08-06 07:42:12

标签: php mysql

所以,我试图为我的php类创建一个简单的购物车。我已经停止了,因为我注意到产品ID没有正确运行。现在,我有一个信息按钮,应该从数据库中提取信息,但它似乎不断为我选择的每个产品提取相同的信息。我有20个产品,其ProductID列从1-20开始,但它似乎将每个产品分配给" 1"。现在,我只想在点击信息按钮时提取信息,因为我确信解决这个问题对其他人有帮助。这是代码:

<?php

session_start();
require_once 'DBLogin.php';

echo "<form action='Catalogue.php' method='Post'>";

$sql = "SELECT * FROM ACME.Products";
$result = mysql_query($sql);

echo "<select name='Select_Product'>";
while ($row = mysql_fetch_array($result)) {
    echo "Please select a product: <option value='" . $row['ProductID'] .     "'>" . $row['Name'] . "</option>";
}
echo "</select>";

echo <<<HTML

<table>
<tr> <td>
<input id="button_Add" type="submit" value="Add" name="action"/>                          
</td> <td> <input id="button_Remove"
type="submit" value="Remove" name="action"/>
</td> <td> <input id="button_empty"
type="submit" value="Empty" name="action"/>
</td> <td> <input id="button_Info"
type="submit" value="Info" name="action"/>
</td> </tr>
</table>
</form>
HTML;

//code to attempt to pull the info
$product_id = $_POST['Select_Product']; 
$action = $_POST['action'];

switch ($action) {
    case "Add":
        echo $_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":
        $sql = "SELECT * FROM ACME.Products";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        echo "<table>";
        echo "<tr><td>Name</td><td>Description</td ><td>Price</td><td>Image</td>     </tr>";
        echo "<td>" . $row['Name'] . "</td>";
        echo "<td>". $row['Description'] . "</td>";
        echo "<td>". $row['Price'] . "</td>";
        echo "<td align=\"center\"><img alt=\"\" src=\"productImages/".  $product_id . ".jpg\ width=\"120\" height=\"120\"/></td></tr>";
        echo "</table>";
        break;
}

以下是该网站的链接,以便您了解该怎么做: http://ec2-52-24-105-81.us-west-2.compute.amazonaws.com/Catalogue.php

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你忘记了where条款

case "Info":
    $sql = "SELECT * FROM ACME.Products Where id = $product_id Limit 1";