<?php
session_start();
if(isset($_GET['month'] || isset($_GET['year']){
$mon=$month;
$yr=$year;
}
if (isset($_POST['submit']))
{
foreach ($_POST['u'] as $value)
{
extract($value);
if ($productname == "" || $quantity == "" || $price == "" || $productprice == "" || $image == ""){
echo("<script language='javascript'>alert='Cannot be empty!';</script>");
}
else
{
$sql = "INSERT INTO salesupdate SET batchid = '$batch', partner = '$part', month = '$mon', year = '$yr', image = '$image', productname = '$productname', quantity = '$quantity', price = '$price', total = '$productprice', entrydate = curdate();";
echo $sql . '<br>';
mysql_query($sql) or die('Error : ' . mysql_error());
}
}
}
?>
<form name="co" method="post" action="<?php echo $PHP_SELF; ?>">
<tr>
<td>
Sales Month: <?php echo $mon; ?> / <?php echo $yr; ?>
<br>
<script language="javascript">
function add(){
var table = document.getElementById("table1");
var row = table.insertRow();
var im = row.insertCell();
im.innerHTML = "<img src=\"http://localhost/images/" + document.getElementById("productname").value + ".jpg\">"
var pn = row.insertCell();
pn.innerText = document.getElementById("productname").value;
var q = row.insertCell();
q.innerText = document.getElementById("quantity").value;
var p = row.insertCell();
p.innerText = document.getElementById("price").value;
var total = row.insertCell();
total.innerText = document.getElementById("price").value * document.getElementById("quantity").value;
var r = row.insertCell();
var field = document.createElement("input");
field.type = "hidden";
field.value = document.getElementById("price").value * document.getElementById("quantity").value;
field.name= "u[][productprice]"
r.appendChild(field);
var field = document.createElement("input");
field.type = "hidden";
field.value = document.getElementById("productname").value + ".jpg";
field.name= "u[][image]"
r.appendChild(field);
var button = document.createElement("input");
button.type = "button";
button.value = "Remove";
button.onclick=function(){document.getElementById("table1").deleteRow(row.rowIndex)}
r.appendChild(button);
}
</script>
Product Name<input type="text" id="productname" name="u[][productname]" size="20" />
QTY<input type="text" id="quantity" name="u[][quantity]" size="10" />
Price<input type="text" id="price" name="u[][price]" size="10" />
<input type="button" value="Add" onclick="return add();" />
<br><br>
<table id="table1" border="1">
<tr><td>Image</td><td>ProductName</td><td>QTY</td><td>Price</td><td>Total</td><td>Function</td></tr>
</table>
<br><br>
<input type="submit" name="submit" value="Submit" />
</form>
//SQL
INSERT INTO salesupdate SET month = '05', year = '2010', image = '123.jpg', productname = '12345', quantity = '1', price = '4', total = '6';
INSERT INTO salesupdate SET month = '05', year = '2010', image = '123.jpg', productname = '12345', quantity = '1', price = '4', total = '12';
INSERT INTO salesupdate SET month = '05', year = '2010', image = '1234.jpg', productname = '12345', quantity = '1', price = '4', total = '12';
INSERT INTO salesupdate SET month = '05', year = '2010', image = '1234.jpg', productname = '12345', quantity = '1', price = '4', total = '4';
INSERT INTO salesupdate SET month = '05', year = '2010', image = '12345.jpg', productname = '12345', quantity = '1', price = '4', total = '4';
I input 3 rows of data:
image productname quantity price total
123 123 2 3 6
1234 1234 3 4 12
12345 12345 1 4 4
But, you can see that the code generate 5 SQL, image is duplicated, productname,quantity,price are the same.
So, what is wrong with it?
Moreover, I need to record the sum of quantity and total:
image productname quantity price total
123 123 2 3 6
1234 1234 3 4 12
12345 12345 1 4 4
6 22
How can I get the 6 and 22 values? so that I can input it to separate table.
And, why those 'insert into' statements display on the top of the php page?