pocobueno1388 Posted November 18, 2006 Share Posted November 18, 2006 I have this script where users can buy items from a store, then the item is added to their inventory. For some reason it is adding the item twice to the database, and I can't figure out how to get it to only add once.Here is part of the code that deals with the problem:[code]<?php$item = $_GET['item'];if ($item){$sql = mysql_query("SELECT price, stock, uses, tack_type FROM `items_store` WHERE item_name='$item'");$row = mysql_fetch_assoc($sql);$price = $row['price'];$sql2 = mysql_query("SELECT money FROM players WHERE playerID='$sid'");$row2 = mysql_fetch_assoc($sql2);if ($row2['money'] < $price){echo "<table class='echo'><td class='echo'><b>You can't afford to purchase this item!</b></td></table>";exit;} else if ($row['stock'] == 0){echo "<table class='echo'><td class='echo'><b>This item is not in stock!</b></td></table>";exit;} else {mysql_query("UPDATE players set money=money-$price WHERE playerID='$sid'");mysql_query("UPDATE items_store SET stock=stock-1 WHERE item_name='$item'");mysql_query("INSERT INTO `inventory` (item_name, ownerID, uses, tack_type)VALUES('$item', '$sid', '$row[uses]', '$row[tack_type]')") or die (mysql_error());echo "<table class='echo'><td class='echo'><b>You bought a(n) $item!</b></td></table>";exit;}}//end if print<<<HERE<TD class='main' width='25%' align='center'><b>$col1</b><br>$col2<br>Amount in stock: <b>$col3</b><br>Price: <b>$$col5</b><p><i>$col4</i><p><a href="store.php?store=$store&item=$col1"><h3>Buy</h3></a></TD>\nHERE;?>[/code]Its this part that is entering two entries into the database when its only suppose to enter it once:[code]mysql_query("INSERT INTO `inventory` (item_name, ownerID, uses, tack_type)VALUES('$item', '$sid', '$row[uses]', '$row[tack_type]')") or die (mysql_error());[/code] Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/ Share on other sites More sharing options...
pocobueno1388 Posted November 19, 2006 Author Share Posted November 19, 2006 Anyone? Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-126922 Share on other sites More sharing options...
marcus Posted November 19, 2006 Share Posted November 19, 2006 Try:[code]$query = "INSERT INTO `inventory` (`item_name`, `ownedID`, `uses`, `tack_type`) VALUE ('$item', '$sid', '$row[uses]', '$row[tack_type]')";$result = mysql_query($query) or die(mysql_error());[/code] Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-126925 Share on other sites More sharing options...
pocobueno1388 Posted November 19, 2006 Author Share Posted November 19, 2006 Okay I did that:[code]$query = "INSERT INTO `inventory` (`item_name`, `ownerID`, `uses`, `tack_type`) VALUE ('$item', '$sid', '$row[uses]', '$row[tack_type]')";$result = mysql_query($query) or die(mysql_error());echo "$query - $result";[/code]and this is what I got:INSERT INTO `inventory` (`item_name`, `ownerID`, `uses`, `tack_type`) VALUE ('Dog Food', '1', '4', 'no') - 1(I added the hyphen, so it just says "1" for the $reslut)I don't know if the 1 means it ran only once...but it is running twice still, I don't understand. Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-127064 Share on other sites More sharing options...
pocobueno1388 Posted November 19, 2006 Author Share Posted November 19, 2006 Wow, it almost seems like every script on my site is running twice. Do you think it could be the host? Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-127123 Share on other sites More sharing options...
pocobueno1388 Posted November 20, 2006 Author Share Posted November 20, 2006 Sorry for bumping this so much, but I am desperate for help right now ^^ Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-127270 Share on other sites More sharing options...
kenrbnsn Posted November 20, 2006 Share Posted November 20, 2006 How is the script being invoked?I would put some "trace" statements in to see the flow. You might have a logic problem somewhere that is allowing the code to be executed twice.Ken Link to comment https://forums.phpfreaks.com/topic/27717-script-running-twice-when-its-only-supposed-to-run-once-still-need-help/#findComment-127317 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.