比较输入数量值和数据库数量值

时间:2015-03-03 06:52:58

标签: javascript php jquery html css

我想比较用户的输入值和数据库中的值。如果用户的输入值高于数据库值,则警报窗口将通知用户他想要的数量是不够的。

我该怎么做?

这是我的html和php代码

<div class="text">Enter Product Name Here: </div> <br/> 

    <form action="addorder.php" method="post" name="abc">

        <a href="auto.php?id=64">BEER</a>
        <a href="auto.php?id=5">Pizza</a>
        <a href="auto.php?id=57">Egg Pie</a>
        <a href="auto.php?id=63">Ice Tea</a>
        <a href="auto.php?id=66">Combo</a>

    </div> <br/> <br/>


            <?php
                if (isset($_GET['id']))
                    {
                    $con = mysql_connect('localhost','root',"");
                        if (!$con)
                        {
                            die('Could not connect: ' . mysql_error());
                        }

                    mysql_select_db("inventory", $con);

                        $member_id = $_GET['id'];
                        $result = mysql_query("SELECT * FROM productlist WHERE id = $member_id");
                        $row = mysql_fetch_array($result);
                        $name=$row["pdesc"];
                        $qty_left=$row["pleft"];
                        $price=$row["pprice"];
                        $id=$row["id"];
                        $prcoede=$row["pcode"];

                    mysql_close($con);
                    }
            ?>

<div class="order">
<table>
    <tr>
        <td><label>Product Name</label></td>
        <td><label>Product Price </label></td>
        <td><label>Quantity Left </label></td>
    </tr>

    <tr>
        <td><input name="PNAME" type="text" id="pname" value="<?php echo @$name ?>" size="20" readonly="readonly" /></td>
        <td><input name="PPRICE" id="PPRICE" type="text" value="<?php echo @$price ?>" readonly="readonly" /></td>
        <td><input name="AQTY" type="text" value="<?php echo @$qty_left ?>" size="20" readonly="readonly"/></td>
        <td><input name="id" type="hidden" value="<?php echo $id; ?>" readonly="readonly" /></td>
        <td><input name="procode" type="hidden" value="<?php echo $prcoede; ?>" readonly="readonly" /></td>
        <td><input name="less" type="hidden"/></td> 
    </tr>   

        <tr><td></td></tr><tr><td></td></tr>
        <tr><td></td></tr><tr><td></td></tr>

    <tr class="space">
        <td><label>Reciept Code</label></td>
        <td><label>Quantity</label></td>    
        <td><label>Amount</label></td>
    </tr>

    <tr>        
        <td><input name="CODE" type="text" id="CODE" value="<?php echo $_SESSION['SESS_MEMBER_ID']; ?>"  size="20" readonly="readonly"/></td>








------->        <td><input name="QTY" id="QTY" type="text" onkeyup="multiply()" onkeypress="return checkIt(event)" size="20" /></td>


            <?php
                $con = mysql_connect("localhost","root", "");
                mysql_select_db("inventory");

            $qty =  $_POST['QTY'];

            $a = "SELECT pleft from productlist WHERE pdesc = $qty ";

            $results = mysql_query($a);

            if ($qty > $a)
            {
                echo '<script type="text/javascript">'; 
                    echo 'alert("WARNING: Product not enough ");'; 
                    echo 'window.location.href = "auto.php";';
                echo '</script>';
            }


?>








        <td><input name="TOTAL" id="TOTAL" type="text"  size="20" readonly="readonly"/></td>
    </tr>

    <tr>
        <td>
            <div class="cart">
                <input name="submit" type="submit" value="Add to Cart" id="xx" style="cursor:pointer; margin-left: 230px; margin-top: 20px;" />
            </div>
        </td>
    </tr>
</table>
</div>
</form>
</form>

1 个答案:

答案 0 :(得分:0)

            <form action="addorder.php?id=<?php echo $_GET['id'] ? $_GET['id'] : 0?>" method="post" name="abc">

                    <a href="auto.php?id=64">BEER</a>
                    <a href="auto.php?id=5">Pizza</a>
                    <a href="auto.php?id=57">Egg Pie</a>
                    <a href="auto.php?id=63">Ice Tea</a>
                    <a href="auto.php?id=66">Combo</a>


                    <td><input name="QTY" id="QTY" type="text" onkeyup="multiply()" onkeypress="return checkIt(event)" size="20" /></td>


                        <?php
                        if( !empty($_POST['QTY'])){

                            $con = mysql_connect("localhost","root", "");
                            mysql_select_db("inventory");

                            $qty =  $_POST['QTY'];

                            $a = "SELECT pleft from productlist WHERE pdesc = $qty wherer productlist.id = ".$_GET['id'];




                            $results = mysql_query($a);
                            $row = mysql_fetch_assoc( $results);

                            if ($qty > $row[0]['pleft'])
                            {
                                echo '<script type="text/javascript">'; 
                                    echo 'alert("WARNING: Product not enough ");'; 
                                    echo 'window.location.reload()';
                                echo '</script>';
                            }else{
                            /**
                             Do what ever you want
                             */

                            }

                        }


            ?>

检查您的productlist表格中是否有一个名为id

的列