0

我正在为大学项目制作购物车并跟随导师示例代码,但对其进行调整以适合我自己的设计。

我的网站产品被命名为

cutecupcak.es/product.php?id=vanilla_cupcakes

而不是

cutecupcak.es/product.php?id=2

购物车页面显示购物车中产品的代码是

if(is_numeric($id)) {
    $cartresult = mysql_query($sql);

但是我的产品 url 不是数字的,所以购物车没有显示结果,正如你在这里看到的那样——http: //cutecupcak.es/shoppingcart.php——如果你测试一下。

如果你有products.php?id=2等,它会起作用。

我需要改变什么 -is_numeric使其适用于我的 url 格式?

完整代码

<?php

session_start(); // start new session or call exisiting session
include('include/dbconnect.php'); // include the database connections

function renderCartForm()
    {
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="shoppingcart.php?action=update" method="post" id="cart">';
        $output[] = '<div class="form">';
        $output[] = '<table border="2" class="formcontainer">';
        $output[] = '<tr class="formlabels">';
        $output[] = '<td width="400">Cake Name</td>';
        $output[] = '<td width="75">Price</td>';
        $output[] = '<td width="75">Quantity</td>';
        $output[] = '<td width="100">Total</td>';
        $output[] = '<td width="100">Remove?</td>';
        $output[] = '</tr>';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM `CAKE` WHERE `cake_url` = '.$id;

            if(is_numeric($id)) {
                $cartresult=mysql_query($sql);

            while($cartrow = mysql_fetch_array($cartresult)) {
                $output[] = '<tr>';
                $output[] = '<td>'.$cartrow['cake_name'].'</td>';
                $output[] = '<td>'.$cartrow['cake_price'].'</td>';
                $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
                $output[] = '<td>&pound;'.($cartrow['cake_price'] * $qty).'</td>';
                $output[] = '<td><a href="shoppingcart.php?action=delete&id='.$id.'">Remove</a></td>';
                $total += $cartrow['cake_price'] * $qty;
                $output[] = '</tr>';
            }
            }
        }
        $output[] = '</table>';
        $output[] = '</div>';
        $output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
        $output[] = '<div class="\"formlabels\"><button type="submit">Update Cart</button></div>';
        $output[] = '</form>';
    } else {
        $output[] = '<p>Your shopping cart is empty.</p>';
    }
    echo join('
                ',$output);
    }

?>

4

2 回答 2

0

PHP 的is_string函数正是您要寻找的。如果您的产品名称都不包含数字,您也可以简单地执行!is_numeric.

于 2012-11-28T21:10:53.167 回答
0

If your $id is a string rather than a number, then you don't need to check whether it's numeric or not. In fact, you only need to check whether it's not empty, such as

if(strlen(trim($id)) {
    $cartresult = mysql_query($sql);

However! You will almost certainly need to modify how your $sql is built. You're not showing it in your code, but it has to be done somewhere before the mysql_query line. You'll probably end up with something like

$dbh = new PDO('mysql:host=hostname;port=portnum;dbname=databasename', 'username', 'password');

if(strlen(trim($id)) {
    $sql = "SELECT * FROM product WHERE id=?";
    $sth = $dbh->prepare($sql);
    $sth->bindParam(1, $id, PDO::PARAM_ISTR);
    $sth->execute();

    while($row = $sth->fetch()) {
        ...
    }
}

Note that this example is using PDO rather than mysql_ set of function, which is deprecated anyway. You should probably start using PDO, as it's a lot more versatile and secure (if used properly).

于 2012-11-28T21:21:40.280 回答