I'm trying to make an invoice system that adds rows, from a tutorial and data is correctly inserted into tbl_orderdetail, but I can't make it work to insert data to tbl_order.
These are the 2 MYSQL tables:
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 ;
And this is my connection code:
<?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]}'
");
}
}
?>
What is wrong in my code?
Here is the form that submits the values:
<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) -->