使用未定义的常量假定数量

时间:2014-08-02 13:18:54

标签: php

我想检查数量是否超过给定值,它应该得到一条消息"它是指定数量的更多数量"。我在订单页面中使用未定义的常量qty假设的数量时出错。

<?php session_start();?>
        <?php
         $a=$_GET['id'];
         $k=0;
         $c_id;
        $link=mysql_connect("localhost","root","")or die("Can't Connect...");

        mysql_select_db("final_shop",$link) or die("Can't Connect to Database...");
 $uid=$_SESSION['userid'];
 $nm=$_SESSION['unm'];
 $id=$_SESSION['bid'];

 $t=$_SESSION['total'];
 $oid=0;

if(isset($_SESSION['cart']))
{

    foreach($_SESSION['cart'] as $id=>$x)
    {   
        $p=$_SESSION['p'][$k];
        $qty=$_SESSION['qty'][$k];

        $bid_var=$x['b_id'];

        $result = mysql_query("SELECT b_subcat FROM `product` WHERE b_id=$bid_var") or die(mysql_error());
        $no_of_rows = mysql_num_rows($result);
        echo "<br>";

        $ii = 0;

        while($ii < $no_of_rows)
            {
                $c_id = mysql_result($result,$ii,'b_subcat');
                $c_id;
                $ii++;
                echo "<br>";
            }

        $query="INSERT INTO `order`(`u_id`, `u_nm`, `b_id`, `qty`, `c_id`, `total_amount`) VALUES ('".$uid."','".$nm."','".$x['b_id']."','".$qty."','".$c_id."','".$p."')";
        $k++;
        mysql_query($query) or die(mysql_error());

    }
    // error in this if condition
    if($qty <= qty)
                {
                mysql_query("UPDATE `product` SET qty = (qty - $qty ) WHERE b_id='$bid_var'") or die(mysql_error()) ;
                }
                else
                {
                    echo "out of stock";
                }       
}

        echo "<script> alert('Your Order has been placed'); location='print.php?' </script>";
?>

1 个答案:

答案 0 :(得分:1)

问题似乎就在于:

if ($qty <= qty)

在php中,每个变量都必须以$开头。 (if ($qty <= $someValue)

假设没有$的变量名是常量。错误消息告诉您必须始终在使用常量之前定义常量。定义这样的常量可以这样做:

define("qty", 100); // define the constant
echo qty; // get value of constant; Note: there is no `$` involved