将输入的用户数量值与数据库中的数量值进行比较

时间:2015-04-28 11:23:36

标签: javascript php html arrays

我遇到一个问题,我不知道如何将用户输入的数量与数据库中的数量进行比较,这样,如果用户将数量超过数据库中的数量,则会发出一个通知警告产品在该数量上缺货的用户。下面是我的代码:

<?php

$con = mysql_connect('localhost', 'root', '') or die (mysql_error());
mysql_select_db('warda',$con) or die(mysql_error());


$page_title= 'View Your Shooping Cart';

if(isset($_POST['submit']))
{

  foreach( $_POST['qty'] as $key => $value)
  { 
    if (is_numeric ($value))
    {
        if(($value == 0) AND (is_numeric($value)))
        {
            unset($_SESSION['cart'][$key]);
        }
        elseif(is_numeric($value)AND($value>0))
        {
            $_SESSION['cart'][$key]=$value;
        }
    }
    else 
    {
      print '<script type="text/javascript">'; 
      print 'alert("Please insert the a number")'; 
      print '</script>';
      echo '<meta HTTP-EQUIV="REFRESH" content="0; url=viewcart.php">';
      exit();   
    }
  }

}


$empty = TRUE;
if(isset($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $key=>$value)
{
    if(isset($key))
    {
        $empty=FALSE;

    }
  }
 }
  //echo $empty.'nilai';`

 if(!$empty)
 {

$query ='SELECT * FROM product WHERE code IN (';//coding dalam database
foreach($_SESSION['cart'] as $key => $value)
{
    $query.="'".$key."'".',';
}

$query = substr($query,0,-1).')';
$result = mysql_query($query) or die("error");
echo "<form action=\"viewcart.php\" method=\"POST\">";
 echo'<table width="800" align="center" cellpadding=4 cellspacing=0 border=0    align="center" class="style25">
<tr>
    <td align="center" width="30%"><b>Code</b></td>
    <td align="center" width="30%"><b>Name</b></td>
    <td align="center" width="10%"><b>Price</b></td>
    <td align="center" width="10%"><b>Quantity</b></td>
    <td align="center" width="10%"><b>Total</b></td>
    </tr>';
echo'<form action="viewcart.php" method="post">';

$total = 0 ;
while($row_Recordset1 = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $subtotal = $_SESSION['cart']         [$row_Recordset1['code']]*$row_Recordset1['price'];
    $price = $row_Recordset1['price'];
    $total+=$subtotal;      
    echo"<tr>
        <td align=\"center\" width=\"30%\">{$row_Recordset1['code']}</td>
        <td align=\"center\" width=\"30%\"><input type=\"hidden\"size=\"3\"         `name=\"name\" value= \"{$row_Recordset1['name']}\">{$row_Recordset1['name']}  </td>`
        <td align=\"center\" width=\"10%\">"; 
        echo "RM " . number_format($price,2);
        echo"</td>
        <td align=\"center\" width=\"10%\"><input type=\"text\"size=\"3\"   name=\"qty[{$row_Recordset1['code']}]\" value=\"{$_SESSION['cart'] [$row_Recordset1['code']]}\"</td>";
    echo'<td align="center">RM'.number_format($subtotal,2).'</td>
            </tr>';             
}   


echo'<tr> 
<td colspan="4" align="right"><b>Total Price:<b></td>
<td align="right">RM'.number_format($total,2).'</td>
</tr>
</table>';
$_SESSION['total'] = $total;


echo "  <div align=\"center\"></p>
<input type=\"submit\"class=\"style1\"  name=\"submit\"                                           
                              value=\"Update My Cart\" />   </div>
        </form>
        <div align=\"center\"> 
          <form name=\"form1\" method=\"post\" action=\"carts.php\">
            <input type=\"submit\" class=\"style1\" name=\"checkout\" 

      value=\"Checkout\"></p>";


 `?>`
 </form>

 <a href="m_custproducts.php">Continue Shopping</a>

        </div>
  <?php

$today = date("d-m-Y"); 

mysql_close();
}
else
{

        echo 'YOUR CART IS EMPTY';
  }



  ?>

0 个答案:

没有答案