Jump to content

Script running twice when its only supposed to run once. *Still need help*


pocobueno1388

Recommended Posts

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>\n

HERE;
?>
[/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]
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]
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.