我试图制作一个添加行的发票系统,从教程和数据中正确插入tbl_orderdetail,但我无法将数据插入tbl_order。
这是2个MYSQL表:
CREATE TABLE IF NOT EXISTS `tbl_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`re_name` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `tbl_orderdetail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`quantity` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`discount` int(11) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=73 ;
这是我的连接代码:
<?php
$cn = mysql_connect('localhost','root','');
if($cn)
{
mysql_select_db('mydb',$cn);
}
if(isset($_POST['submit']))
$re_name = $_POST['re_name'];
$location = $_POST['location'];
{
mysql_query ("INSERT INTO tbl_order(re_name,location) VALUES('{$_POST['re_name']}','{$_POST['location']}')");
$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['productname']);$i++)
{
mysql_query("INSERT INTO tbl_orderdetail
SET order_id = '{$id}',
product_name = '{$_POST['productname'][$i]}',
quantity = '{$_POST['quantity'][$i]}',
price = '{$_POST['price'][$i]}',
discount = '{$_POST['discount'][$i]}',
amount = '{$_POST['amount'][$i]}'
");
}
}
?>
我的代码有什么问题?
以下是提交值的表单:
<form action="" method="post">
<div class="box-body">
<div class="form-group">
ReceptName
<input type="text" name="re_name" id="re_name" class="form-control">
</div>
<div class="form-group">
Location
<input type="text" name="location" id="location" class="form-control">
</div>
<input type="submit" class="btn btn-primary" name="submit" id="submit" value="Save Record">
</div>
<table class="table table-bordered table-hover">
<thead>
<th>No</th>
<th>ProductName</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
<th><input type="button" value="+" id="add" class="btn btn-primary"></th>
</thead>
<tbody class="detail">
<tr>
<td class="no">1</td>
<td><input type="text" class="form-control productname" name="productname[]"></td>
<td><input type="text" class="form-control quantity" name="quantity[]"></td>
<td><input type="text" class="form-control price" name="price[]"></td>
<td><input type="text" class="form-control discount" name="discount[]"></td>
<td><input type="text" class="form-control amount" name="amount[]"></td>
<td><a href="#" class="remove">Delete</td>
</tr>
</tbody>
<tfoot>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th style="text-align:center;" class="total">0</th>
</tfoot>
</table>
</div>
</div><!-- /.box -->
</form>
</div><!--/.col (left) -->
答案 0 :(得分:0)
谢谢大家的帮助。 我删掉了tbl_order并创建了一个名为'orcamen'的新文件。
这是有效的PHP代码:
if(isset($_POST['submit']))
{
$razao = $_POST["razao"];
$local = $_POST["local"];
$condicao = $_POST["condicao"];
$estado = $_POST["estado"];
$material = $_POST["material"];
$obs = $_POST["obs"];
mysql_query
("INSERT INTO orcamen ( id , razao , local , condicao , estado , material , obs )
VALUES ( NULL , '$razao', '$local', '$condicao', '$estado', $material', '$obs')");
$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['productname']);$i++)
{
mysql_query("INSERT INTO tbl_orderdetail
SET order_id = '{$id}',
product_name = '{$_POST['productname'][$i]}',
quantity = '{$_POST['quantity'][$i]}',
price = '{$_POST['price'][$i]}',
discount = '{$_POST['discount'][$i]}',
amount = '{$_POST['amount'][$i]}'
");
}
}