从数据库设置html输入数的最大值

时间:2014-02-19 06:04:13

标签: php html

所以我需要将输入数字的最大值设置为数据库中剩余的数量。是否有可能或有某些方法可以做到这一点因为我需要首先验证输入值是否等于或小于剩余的股票,如果大于它将提示输入必须小于或等于最大值。

这是我的代码:

<?php    
   include('config.php'); 
   $resulta = mysql_query("SELECT * FROM inventory WHERE id = '$id'");

   while($row = mysql_fetch_array($resulta)) 
   {
     echo 'Stock Left: ' . $row['qtyleft'];
   }

   $stockstring = $row['qtyleft'];

   //$row['qty'] > = $_post['input_value'];

   echo '<label style="color:#000000;">Qty:';
   echo '<input type="number" min="1" max="'.$row['qtyleft'].'" id="qty" name="qty" required = "required" /><br>';
?>

2 个答案:

答案 0 :(得分:1)

从表中获取Max id的最大函数:

$getMaxID=mysql_query("SELECT max(qtyleft) FROM inventory WHERE id = '$id'");
$row = mysql_fetch_array($getMaxID);
$Maxno=$row['max(qtyleft)']; 

echo $Maxno;
echo '<label style="color:#000000;">Qty:';
echo '<input type="number" min="1" max="'.$Maxno.'" id="qty" name="qty" required = "required" /><br>';

答案 1 :(得分:0)

您需要将所有这些移动到while循环

$resulta = mysql_query("SELECT * FROM inventory WHERE id = '$id'");
while($row = mysql_fetch_array($resulta)){
    echo 'Stock Left: ' . $row['qtyleft'];
    echo '<label style="color:#000000;">Qty:';
    echo '<input type="number" min="1" max="'.$row['qtyleft'].'" id="qty" name="qty" required = "required" /><br>';
}