0

I am making a shopping cart for university project and following tutors sample code but tweaking it to fit my own design.

my site products are named such as

cutecupcak.es/product.php?id=vanilla_cupcakes

rather than

cutecupcak.es/product.php?id=2

The code for the shopping cart page to display the products in your cart is

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

but my product urls are NOT numeric and so the shopping cart is not displaying the results, as you can see here -- http://cutecupcak.es/shoppingcart.php - if you test it out.

it will work if you have products.php?id=2 etc. though

what do i need to change from - is_numeric to make it work with my url format?

full code

<?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

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

この例ではmysql_、関数のセットではなく PDO を使用していることに注意してください。おそらく、PDO の使用を開始する必要があります。PDO の方が汎用性が高く安全です (適切に使用すれば)。

于 2012-11-28T21:21:40.280 に答える
0

PHP のis_string関数が探しているものです。製品名に数字が含まれていない場合は、単に!is_numeric.

于 2012-11-28T21:10:53.167 に答える