すでに在庫テーブルに保存されているすべての在庫。しかし、商品を販売するときに在庫数を更新するにはどうすればよいですか? 在庫表の数量行を更新したいと考えています。私は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('','','','','','','','','');
}
?>