【问题标题】:How to update stock quantity using MySQL in PHP如何在 PHP 中使用 MySQL 更新库存数量
【发布时间】:2013-10-09 12:32:08
【问题描述】:

所有库存已保存在库存表中。但是,当我出售我的产品时,如何更新库存数量?我想更新我的库存表数量行。我正在使用 MySQL。

表名stock:

--------------------------------------------------------------------
    id|username | date      | item     | quantity|  amount
------------------------------------------------------------------
    1 |xyz      |2013-10-09 | computer |25       | 25.00
-----------------------------------------------------------------

这是sale 表:

--------------------------------------------------------------------
    id|username | date      | item     | quantity|  amount
------------------------------------------------------------------
    1 |xyz      |2013-10-09 | computer |25       | 25.00
-----------------------------------------------------------------

这是 sale.php 页面。当我销售产品时,此页面会在销售表中保存记录,但我想更新库存表数量行:

 <?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($date ,$username,$item,$quantity,$amount, $error)

 {
 ?>
 <form id="searchform" action="" method="post" enctype="multipart/form-data">
  <div align="center">
 <fieldset>

   <div align="center">
     <legend align="center" >Stock!</legend>
   </div>
   <div class="fieldset">
     <p>
   <label class="field" for="date">Date: </label>

       <input name="date" type="text" class="tcal" value="<?php echo date("Y-m-d");; ?>" size="30"/>
         </p>
   <p>
     <label class="field" for="username">User Name : </label>

     <input name="username" type="text"  id="username"  value="<?php echo $username; ?>" size="30"/>
   </p>
   <p>
     <label class="field" for="item">Item: </label>

     <input name="item" type="text"  value="<?php echo $item; ?>" size="30"/>
   </p>


       <p>
       <label class="field" >Quantity :</label>
       <input  name="quantity" type="text" value="<?php echo $quantity; ?>"  size="30"/>
     </p> 
       <p>
       <label class="field" >Amount :</label>
       <input  name="amount" type="text" value="<?php echo $amount; ?>"  size="30"/>
     </p> 
  </div>
 </fieldset>
   <p align="center" class="required style3">Please Fill The Complete Form </p>
   <div align="center">
     <input name="submit" type="submit" class="style1" value="Submit">

   </div>
 </form> 


 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 



 <?php 
 }




 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
 $username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
 $item = mysql_real_escape_string(htmlspecialchars($_POST['item']));
 $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity']));
 $amount = mysql_real_escape_string(htmlspecialchars($_POST['amount']));



 // check to make sure both fields are entered
 if ($date == '' || $quantity == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($date ,$username,$item,$quantity,$amount,  $error);

 }
 else
 {
 // save the data to the database
  mysql_query("INSERT  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'")
 or die(mysql_error()); 
 echo "<center>Stock Enter Complete!</center>";
 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','');
 }

         ?>

【问题讨论】:

  • 添加另一个 SQL 查询以减少您的库存(这将是一个更新,并且可能会这样做 UPDATE stock SET quantity = quantity - $quantity WHERE ...。您可以试一试并将该尝试编辑到您的问题中吗?
  • 同一行显示错误

标签: php mysql point-of-sale


【解决方案1】:

您将股票 id 存储在销售表中,并在当时出售股票时获取股票 id 以更新股票表:

<?php
$stockId="Here get stock id from sale table";
$qty="Here get qty from sale table";
$query="UPDATE stocktable SET quantity='".$qty."' WHERE id='".$stockId."'";
 ?>

【讨论】:

  • 销售表中的stockid可以使用select查询从db中获取值
【解决方案2】:

使用 UPDATE 查询作为

UPDATE stock set quantity = quantity - '$quantity' WHERE id = '$id'

【讨论】:

  • 我这样更新,但显示错误 mysql_query("INSERT sale SET date='$date', username='$username',item='$item',quantity='$quantity', amount='$amount'") mysql_query("UPDATE stock set quantity = quantity - '$quantity' WHERE id = '$id'")
  • 解析错误:语法错误,在线 mysql_query("UPDATE stock set quantity = quantity - '$quantity' WHERE id = '$id'")
  • 试试这个 mysql_query("UPDATE stock set quantity = quantity-".$quantity." WHERE id = '".$id."'");
  • 您是否已为变量 $id 赋值,或者您可以在 where 条件中使用 "WHERE item = '".$item."'"
  • 查看我的代码 mysql_query("INSERT sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount '") mysql_query("UPDATE stock set quantity = quantity-".$quantity." WHERE id = '".$id."'");
【解决方案3】:

您需要将quantityonstock 添加到您的数据库中,然后执行以下操作:

$update=UPDATE table SET onstock=onstock-'$quantity' WHERE id='$id';
$result=mysqli_query($conn,$update);

【讨论】:

    【解决方案4】:

    我使用在销售和购买时输入一行库存。当我进行购买时,我将数量设为正数,以及所有其他相关字段,如我的产品、购买 ID,当我出售时,我将数量设为负数,这样我总是有历史记录。 当获取库存列表时,我在返回库存行时使用总和。在关闭期间,我将库存清空到另一个存档表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多