Page 1 of 1
Question regarding Insert Statements and Databases
Posted: Fri Jul 25, 2008 9:46 am
by Sephirangel
Question: Why would an INSERT statement insert a record twice into a table, when only called once?
Its happened to me a number of times now and it is starting to get on my nerves. In each table i have an auto-incrementing primary key and a unique key.
Any ideas?
Re: Question regarding Insert Statements and Databases
Posted: Fri Jul 25, 2008 11:25 am
by Frozenlight777
Do you have the insert query so we can look at it? And are you positive it isn't being executed twice?
Re: Question regarding Insert Statements and Databases
Posted: Fri Jul 25, 2008 11:38 am
by Sephirangel
I am totally positive that it is not being called twice.
Here are the functions that use the sql statements:
Code: Select all
function insertSaleItem($salesArray, $saleN){
for($i = 1; $i <= $salesArray['noOfItems']; $i++){
$itemCode = $_SESSION['ItemCode'.$i];
$saleQuant = $_SESSION['Quantity'.$i];
$pricePerItem = $_SESSION['PricePerItem'.$i];
$insertSaleItems = "INSERT IGNORE INTO saleitems (Sales_Num, ItemCode, SaleQuantity, PricePerItem) VALUES ('" . $saleN . "','" . $itemCode . "','" . $saleQuant . "','" . $pricePerItem ."')";
mysql_query($insertSaleItems);
}
}
function changeInv($salesArray){
for($i = 1; $i <= $salesArray['noOfItems']; $i++){
$itemCode = $salesArray['ItemCode'.$i];
$saleQuant = $salesArray['Quantity'.$i];
$getInvQuant = "SELECT * FROM stock WHERE ItemCode = '" . $itemCode ."'";
$q = mysql_query($getInvQuant);
$row = mysql_fetch_assoc($q);
$quant = $row['Quantity'];
$newQuant = $quant - $saleQuant;
$changeInvQuant = "UPDATE stock SET Quantity = '" . $newQuant . "' WHERE ItemCode = '" . $itemCode . "'";
mysql_query($changeInvQuant);
}
}
The first function inserts all of the items in a particular sale into the database table, so there could be an infinite number of items in 1 sale, hence the
for statement, but i have checked the statement and it is executing the correct amount of times.
The second function alters the quantity for each item in the stock table, that has been used in the sale. This also executes twice (eg if a customer buys 2 of an item, it will take 4 off the quantity)
Thanks in advance! hope that helps!