Jump to content

Insert Statement Help!!!


twilitegxa

Recommended Posts

I have two pages that allow a person to choose items to buy and then when they submit, it adds the item to their inventory and subtracts the amount from their money. But the problem I'm having is that I have two shops: item and weapon. The item shop seems to work fine: the user can buy and sell items properly. The problem is that the weapon shop isn't allowing the user to buy items properly. When you choose a quantity to purchase, it subtracts the money properly, and if the user has no items of the kind in their inventory, it adds the item properly. But if user has any of the items already in their inventory, it is adding or subtracting different numbers to the quantity. I'm not sure what I have coded wrong, but obviously something is wrong in the amount for $new_quantity. Here is the code I have:

 

buy.php

<?php
session_start();
//connect to database
$conn = mysql_connect("localhost", "root", "")
    or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$shop = $_GET['shop'];

//ITEM SHOP

if ($shop == 'item'){

$display_block = "<h1>Item Shop - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price,
si.item_desc, si.item_image, si.effect from store_items as si left join
store_categories as c on c.id = si.cat_id where si.id = $_GET[id]";

$get_item_res = mysql_query($get_item) or die(mysql_error());
$item_id = $_GET['id'];

if (mysql_num_rows($get_item_res) < 1) {
    //invalid item
    $display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
    //valid item, get info
    $cat_title = strtoupper(stripslashes(
        mysql_result($get_item_res,0,'cat_title')));
    $item_title = stripslashes(mysql_result($get_item_res,0, 'item_title'));
    $item_price = mysql_result($get_item_res,0, 'item_price');
    $item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc'));
    $item_image = mysql_result($get_item_res,0, 'item_image');
    $desc = stripslashes(mysql_result($get_item_res,0, 'effect'));
    
    //make breadcrumb trail
    $display_block .= "<table cellpadding=3 cellspacing=3>
        <tr>
        <td valign=top align=center><img src=\"$item_image\" style=\"width: 200; height: 200; display: block;\"></td>
        <td valign=top><strong>Item:</strong> $item_title<br /><strong>Description:</strong> $item_desc<br>
        <strong>Attribute Affected:</strong> $desc<br>
        <strong>Price:</strong> $item_price S
        <form method=post action=\"addtoinventory.php?shop=item\">";
                
        $display_block .= "
        <strong>Quantity:</strong>
        <input type=text name=\"sel_item_qty\" size=2>";
        
        $display_block .= "
        <input type=\"hidden\" name=\"sel_item\" value=\"$item_title\">
        <p><input type=\"submit\" name=\"submit\" value=\"Purchase\"> <a href=shop.php?location=item_shop&choice=buy><input type=button value=Cancel></a></p>
        </form>
        </td>
        </tr>
        </table>";
        
    }
    }
    
//WEAPON SHOP

if ($shop == 'weapon'){

$display_block = "<h1>Weapon Shop - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price,
si.item_desc, si.item_image, si.effect from store_items as si left join
store_categories as c on c.id = si.cat_id where si.id = $_GET[id]";

$get_item_res = mysql_query($get_item) or die(mysql_error());
$item_id = $_GET['id'];

if (mysql_num_rows($get_item_res) < 1) {
    //invalid item
    $display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
    //valid item, get info
    $cat_title = strtoupper(stripslashes(
        mysql_result($get_item_res,0,'cat_title')));
    $item_title = stripslashes(mysql_result($get_item_res,0, 'item_title'));
    $item_price = mysql_result($get_item_res,0, 'item_price');
    $item_desc = stripslashes(mysql_result($get_item_res,0, 'item_desc'));
    $item_image = mysql_result($get_item_res,0, 'item_image');
    $desc = stripslashes(mysql_result($get_item_res,0, 'effect'));
    
    //make breadcrumb trail
    $display_block .= "<table cellpadding=3 cellspacing=3>
        <tr>
        <td valign=top align=center><img src=\"$item_image\" style=\"width: 200; height: 200; display: block;\"></td>
        <td valign=top><strong>Item:</strong> $item_title<br /><strong>Description:</strong> $item_desc<br>
        <strong>Attribute Affected:</strong> $desc<br>
        <strong>Price:</strong> $item_price S
        <form method=post action=\"addtoinventory.php?shop=weapon\">";
                
        $display_block .= "
        <strong>Quantity:</strong>
        <input type=text name=\"sel_item_qty\" size=2>";

        $display_block .= "
        
        <input type=\"hidden\" name=\"sel_item\" value=\"$item_title\">
        <p><input type=\"submit\" name=\"submit\" value=\"Purchase\"> <a href=shop.php?location=weapon_shop&choice=buy><input type=button value=Cancel></a></p>
        </form>
        </td>
        </tr>
        </table>";
        
    }

}
    
//get identity
$get_identity = "select * from scouts where username = '".$_SESSION['userName']."' and active = '1'";
$get_identity_res = mysql_query($get_identity, $conn) or die(mysql_error());

while ($user_identity = mysql_fetch_array($get_identity_res)) {
$identity = $user_identity['identity'];

}
    //gather gold
$get_gold = "select * from gold where identity = '$identity'";
$get_gold_res = mysql_query($get_gold, $conn) or die(mysql_error());

while ($show_gold = mysql_fetch_array($get_gold_res)) {
$gold = $show_gold['amount'];

$display_block .= "<strong>Gold</strong>: $gold";

}
?>

 

addtoinventory.php

<?php

//connect to database
$conn = mysql_connect("localhost", "root", "")
    or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

//get identity
$get_identity = "select * from scouts where username = '".$_SESSION['userName']."' and active = '1'";
$get_identity_res = mysql_query($get_identity, $conn) or die(mysql_error());

while ($user_identity = mysql_fetch_array($get_identity_res)) {
$identity = $user_identity['identity'];

}

//check items in inventory
$get_items = "select * from inventory where identity = '$identity'";
$get_items_res = mysql_query($get_items, $conn) or die(mysql_error());

while ($items = mysql_fetch_array($get_items_res)) {
$item = $items['item'];
$quantity = $items['quantity'];

}

//check for selected item

$result = mysql_query("select * from inventory where item = '$_POST[sel_item]'");
$num_rows = mysql_num_rows($result);

if ($num_rows == 1){
$new_quantity = ($quantity + $_POST['sel_item_qty']);

//update item
$update = "UPDATE inventory set quantity = 

$new_quantity where item = '$_POST[sel_item]'";
$update_res = mysql_query($update, $conn) or 

die(mysql_error());

//get price of selected item
$get_price = "select * from store_items where item_title = 

'$_POST[sel_item]'";
$get_price_res = mysql_query($get_price, $conn) or 

die(mysql_error());

while ($item_price = mysql_fetch_array($get_price_res)){
$price = $item_price['item_price'];

$new_price = ($price * $_POST[sel_item_qty]);

}

//gather gold
$get_gold = "select * from gold where identity = '$identity'";
$get_gold_res = mysql_query($get_gold, $conn) or 

die(mysql_error());

while ($show_gold = mysql_fetch_array($get_gold_res)) {
$gold = $show_gold['amount'];

}

$new_gold = ($gold - $new_price);

//set gold
$update_gold = "update gold set amount = $new_gold 

where identity = '$identity'";
$update_gold_res = mysql_query($update_gold, $conn) or 

die(mysql_error());

} 

if ($num_rows == 0){

$insert_item = "insert into inventory values ('', '$identity', '$_POST[sel_item]', '$_POST[sel_item_qty]')";
$insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());

//get price of selected item
$get_price = "select * from store_items where item_title = 

'$_POST[sel_item]'";
$get_price_res = mysql_query($get_price, $conn) or 

die(mysql_error());

while ($item_price = mysql_fetch_array($get_price_res)){
$price = $item_price['item_price'];

$new_price = ($price * $_POST[sel_item_qty]);

}

//gather gold
$get_gold = "select * from gold where identity = '$identity'";
$get_gold_res = mysql_query($get_gold, $conn) or 

die(mysql_error());

while ($show_gold = mysql_fetch_array($get_gold_res)) {
$gold = $show_gold['amount'];

}

$new_gold = ($gold - $new_price);

//set gold
$update_gold = "update gold set amount = $new_gold 

where identity = '$identity'";
$update_gold_res = mysql_query($update_gold, $conn) or 

die(mysql_error());

} 

$shop = $_GET['shop'];

if ($shop == 'item'){

header("Location: shop.php?location=item_shop&choice=buy");
exit();

}

if ($shop == 'weapon'){

header("Location: shop.php?location=weapon_shop&choice=buy");
exit();

}


?>

 

Not sure which page the problem lies on. Can anyone help???

Link to comment
Share on other sites

I dont think you will get the answer on here because you are asking us to debug your code for you. Not many people have the time to do that on an individual basis.

 

What you need to do is to narrow down the bug to a piece of code and then ask about that rather than what you have done.

 

I know this sounds hard, but it is the only way for us to help without having to try to understand the application, design, and code for each question that is posted.

Link to comment
Share on other sites

Thanks for your suggestion. I decided to just re-do the code and now I have got it working. I'm not sure what I did wrong, but it was probably just due to coding too fast and not paying attention to what I was doing. LOL Here is the code fixed, in case anyone is ever interested:

 

addtoinventory.php

<?php

//get identity
$get_identity = "select * from scouts where username = '".$_SESSION['userName']."' and active = '1'";
$get_identity_res = mysql_query($get_identity, $conn) or die(mysql_error());

while ($user_identity = mysql_fetch_array($get_identity_res)) {
$identity = $user_identity['identity'];

}

//check for selected item
$result = mysql_query("select * from inventory where item = '$_POST[sel_item]'");
$num_rows = mysql_num_rows($result);

if ($num_rows == 1){

//get item information from inventory
$get_item = "select * from inventory where identity = '$identity' and item = '$_POST[sel_item]'";
$get_item_res = mysql_query($get_item, $conn) or die(mysql_error());

while ($item_info = mysql_fetch_array($get_item_res)) {
$item = $item_info['item'];
$quantity = $item_info['quantity'];

$new_quantity = ($quantity + $_POST['sel_item_qty']);

//update item
$update = "UPDATE inventory set quantity = $new_quantity where item = '$_POST[sel_item]'";
$update_res = mysql_query($update, $conn) or die(mysql_error());

//get price of selected item
$get_price = "select * from store_items where item_title = '$_POST[sel_item]'";
$get_price_res = mysql_query($get_price, $conn) or die(mysql_error());

while ($item_price = mysql_fetch_array($get_price_res)){
$price = $item_price['item_price'];

$new_price = ($price * $_POST['sel_item_qty']);

//gather gold
$get_gold = "select * from gold where identity = '$identity'";
$get_gold_res = mysql_query($get_gold, $conn) or die(mysql_error());

while ($show_gold = mysql_fetch_array($get_gold_res)) {
$gold = $show_gold['amount'];

$new_gold = ($gold - $new_price);

//set gold
$update_gold = "update gold set amount = $new_gold where identity = '$identity'";
$update_gold_res = mysql_query($update_gold, $conn) or die(mysql_error());

}//end gold while statement

}//end price while statement

}//end item info while statement

}//end check for selected item if statement $num_rows == 1

if ($num_rows == 0){

//insert selected item
$insert_item = "insert into inventory values ('', '$identity', '$_POST[sel_item]', '$_POST[sel_item_qty]')";
$insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());

//get price of selected item
$get_price = "select * from store_items where item_title = '$_POST[sel_item]'";
$get_price_res = mysql_query($get_price, $conn) or die(mysql_error());

while ($item_price = mysql_fetch_array($get_price_res)){
$price = $item_price['item_price'];

$new_price = ($price * $_POST['sel_item_qty']);

//gather gold
$get_gold = "select * from gold where identity = '$identity'";
$get_gold_res = mysql_query($get_gold, $conn) or die(mysql_error());

while ($show_gold = mysql_fetch_array($get_gold_res)) {
$gold = $show_gold['amount'];

$new_gold = ($gold - $new_price);

//set gold
$update_gold = "update gold set amount = $new_gold where identity = '$identity'";
$update_gold_res = mysql_query($update_gold, $conn) or die(mysql_error());

}//end get gold while statement

}//end get selected item price while statement

}//end non-existing item if statement $num_rows == 0

$shop = $_GET['shop'];

if ($shop == 'item'){

header("Location: shop.php?location=item_shop&choice=buy");
exit();

}//end item shop if statement

if ($shop == 'weapon'){

header("Location: shop.php?location=weapon_shop&choice=buy");
exit();

}//end weapon shop if statement

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.