我想将表单中的数据插入两个表(购买和库存),如果有相同的项目,我希望它插入购买表(记录购买)并更新到库存表(而不是购买表,所以它凸轮添加购买的项目的数量,并在库存表上没有重复的项目)。问题是我不明白这种行为的代码逻辑。
表单页面代码段:
<form name="purchaseform" method="post" onsubmit="return validateForm()" action="submitpurchaseadmin.php">
<table>
<tr>
<td>Type</td>
<td>
<select name="type">
<option value="opt1">Option1</option>
<option value="opt2">Option2</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="qty"></td>
</tr>
<tr>
<td>Date</td>
<td><input type="text" name="date" value="<?php echo date("d-m-Y"); ?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Save"></td>
</tr>
</table>
</form>
当前提交代码段:
<?php
include("connect.php");
$host="localhost";
$user="root";
$pass="";
$db_name="proyek";
$tbl_name="purchase";
mysql_connect("$host", "$user", "$pass")or die("Cannot connect to SQL.");
mysql_select_db('$db_name');
$type=$_POST['type'];
$qty=$_POST['qty'];
$date=$_POST['date'];
$query=("SELECT * FROM stock");
$result=mysql_query($query);
$row=mysql_num_rows($result);
if ($row==0)
{
mysql_query("INSERT into stock (item, qty) VALUES ('$type', '$qty')");
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
}
else
{
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
mysql_query("UPDATE stock SET qty=qty+'$qty' WHERE item='$type'");
}
header("location:purchasehistory.php");
?>
修改
根据您的反馈,我改变了我的代码的底部位置:
$query=("SELECT * FROM stock where item='$type'");
$result=mysql_query($query);
$row=mysql_num_rows($result);
if($row=0)
{
mysql_query("INSERT into stock (item, qty) VALUES ('$type', '$qty')");
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
}
if($type==$row[1]) //$row[1] is item column in table
{
mysql_query("UPDATE stock SET qty=qty+'$qty' WHERE item='$type'");
}
else
{
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
mysql_query("INSERT into stock (item, qty) VALUES ('$type', '$qty')");
}
header("location:purchasehistoryadmin.php");
在插入相同项目时仍然有重复的库存
答案 0 :(得分:0)
您当前的查询会选择所有行,而不管项目是否相等。您需要修改查询,以便检查项目等于$type
的那些行:
改变这个:
$query=mysql_query("SELECT * FROM stock");
要:
$query=mysql_query("SELECT * FROM stock WHERE item = '$type'");
答案 1 :(得分:0)
您更正后的代码块: -
更正1: -
mysql_select_db('$db_name'); // here db_name treating as a string
应该是: -
mysql_select_db($db_name);
更正2: -
$query=("SELECT * FROM stock");
$result=mysql_query($query);
应该是: -
$query= "SELECT * FROM stock";
$result=mysql_query($query); // or you can directly write query here
完整的代码块: -
<?php
include("connect.php");
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "proyek";
$tbl_name = "purchase";
mysql_connect($host, $user, $pass) or die("Cannot connect to SQL.");
mysql_select_db($db_name);
$type = (isset($_POST['type'])) ? $_POST['type'] : '';
$qty = (isset($_POST['qty'])) ? $_POST['qty'] : '';
$date = (isset($_POST['date'])) ? $_POST['date'] : '';
$query = "SELECT * FROM stock";
$result = mysql_query($query);
$row = mysql_num_rows($result);
if ($row == 0) {
mysql_query("INSERT into stock (item, qty) VALUES ('$type', '$qty')");
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
} else {
mysql_query("INSERT into purchase (type, qty, date) VALUES ('$type', '$qty', '$date')");
mysql_query("UPDATE stock SET qty=qty+'$qty' WHERE item='$type'");
}
header("location:purchasehistory.php");
最重要:请不要使用mysql_,因为它已在最新版本中弃用