【问题标题】:Shopping Cart PHP display error购物车 PHP 显示错误
【发布时间】:2012-11-16 20:30:48
【问题描述】:

我正在为大学项目制作购物车,并按照导师的示例代码进行调整以适应我自己的设计。

我的网站产品名称如

cutecupcak.es/product.php?id=vanilla_cupcakes

而不是

cutecupcak.es/product.php?id=2

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

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

但我的产品网址不是数字,因此购物车没有显示结果,正如您在此处看到的那样 -- 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);
    }

?>

【问题讨论】:

  • 您的产品名称可以采用什么格式?你的例子中有字符和下划线,但你能有数字吗?使徒?行情?
  • 所有的url就像vanilla_cupcakes没有数字,基本上是一个字下划一个字
  • 在这种情况下,您可以编写一个函数来检查文本是否采用该格式。

标签: php cart shopping


【解决方案1】:

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

【讨论】:

  • (1) is_numeric('abc123def') will return false` 即使字符串中有数字; (2)理论上,他的情况根本不需要测试,除了测试$id不为空。但是,他的 sql 代码很可能必须更改。
  • 确实,我会更改测试以检查 id 是否为空。
  • !is_numeric 不起作用并抛出错误。如果可以从中找出答案,我已经上传了整个代码?非常感谢
【解决方案2】:

如果您的$id 是字符串而不是数字,则无需检查它是否为数字。其实你只需要检查它是否不为空,比如

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

但是!您几乎肯定需要修改$sql 的构建方式。您没有在代码中显示它,但必须在 mysql_query 行之前的某处完成。你可能会得到类似的东西

$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()) {
        ...
    }
}

请注意,此示例使用的是 PDO 而不是 mysql_ 函数集,无论如何都已弃用。您可能应该开始使用 PDO,因为它更加通用和安全(如果使用得当的话)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多