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?
Question regarding Insert Statements and Databases
Moderator: General Moderators
-
Sephirangel
- Forum Commoner
- Posts: 45
- Joined: Tue Jul 15, 2008 1:37 pm
- Frozenlight777
- Forum Commoner
- Posts: 75
- Joined: Wed May 28, 2008 12:59 pm
Re: Question regarding Insert Statements and Databases
Do you have the insert query so we can look at it? And are you positive it isn't being executed twice?
-
Sephirangel
- Forum Commoner
- Posts: 45
- Joined: Tue Jul 15, 2008 1:37 pm
Re: Question regarding Insert Statements and Databases
I am totally positive that it is not being called twice.
Here are the functions that use the sql statements:
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!
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 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!