Jump to content

twilitegxa

Members
  • Posts

    1,020
  • Joined

  • Last visited

Posts posted by twilitegxa

  1. Well, I'm not sure how to do it. What I have is a little game I'm trying to make. The player logs in, chooses their character, then goes to the page where they can fight random monsters. So the random monster is generated and then the player chooses attack. Upon pressing the attack button, I want to take the character's acv (attack combat value), which is being pulled from a table using php, and subtract that number from the monster's hp. But I don't want the monster's hp being updated in the table in the database, just on the page when they are fighting it. Then after they beat the monster (getting the hp down to zero), they will get experience and money and can choose to fight again or do something else.

     

     

    So what I need to be able to do is use the php variables in my JavaScript calculations. Does that help explain what I'm trying to do any better? Can anyone help?

  2. How do I use php variables in JavaScript? I have data being pulled from two different tables, and what I want to do is perform a calculation when a button is clicked, then output the result. Say for example I have this:

     

     

    php variable named $a

    php variable named $b

     

     

    So what I'd like to do is this:

     

     

    When this button is clicked, subtract $b from $a, then display that result into a new variable, named $c. Can anyone help get started? I'm very new to JavaScript still.

  3. Well, it is going to be more involved than 6 points each time, I was jut using it as a starting example. Thank you for the suggestion. I will work on some of that now and see if I have any more questions. :-)

  4. What would be the best way to go about this:

     

     

    I have a table that displays a random enemy from a table (hit points, defense, experience earned, etc).

    Let's say to begin with, I have a set number that the enemy's hit points are reduced by if the player chooses to attack the enemy (6).

     

     

    What would be the best way in which to set this up to where the enemy's hit points get reduced each time the player clicks the attack button and the hit points get reduced by the number (6 in this case)?

     

     

    Since the code is generating a random enemy, other players playing at the same time could run the risk of generating the same enemy, so is there a way I could avoid this?

     

     

     

     

    I was thinking there could be some way to not actually alter the table data, but rather just do it within the browser window, but I'm not sure what technology would be best to use. Maybe AJAX? Any suggestions on how would be the best way to go about this?

  5. I am using a tutorial as a guideline from w3s (http://www.w3schools.com/php/php_ajax_xml.asp) but I cannot get the code to work. I'm nto sure if it's a PHP, JavaScript, or XML problem. Can anyone tell me what might be the problem?

     

    HTML:

    <html>
    <head>
    <script type="text/javascript" src="selectcd.js"></script>
    </head>
    
    <body>
    
    <form>
    Select a CD:
    <select name="cds" onchange="showCD(this.value)">
    <option value="Bob Dylan">Bob Dylan</option>
    <option value="Bonnie Tyler">Bonnie Tyler</option>
    <option value="Dolly Parton">Dolly Parton</option>
    </select>
    </form>
    
    <div id="txtHint"><b>CD info will be listed here...</b></div>
    
    </body>
    </html>
    

     

    selectcd.js:

    var xmlhttp
    
    function showCD(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    var url="getcd.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    getcd.php:

    <?php
    $q=$_GET["q"];
    
    $xmlDoc = new DOMDocument();
    $xmlDoc->load("cd_catalog.xml");
    
    $x=$xmlDoc->getElementsByTagName('ARTIST');
    
    for ($i=0; $i<=$x->length-1; $i++)
    {
    //Process only element nodes
    if ($x->item($i)->nodeType==1)
      {
      if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
        {
        $y=($x->item($i)->parentNode);
        }
      }
    }
    
    $cd=($y->childNodes);
    
    for ($i=0;$i<$cd->length;$i++)
    {
    //Process only element nodes
    if ($cd->item($i)->nodeType==1)
      {
      echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
      echo($cd->item($i)->childNodes->item(0)->nodeValue);
      echo("<br />");
      }
    }
    ?> 
    

     

    cd_catalog.xml:

    <!-- Edited by XMLSpy® -->
    −
    <CATALOG>
    
    <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
    </CD>
    
    <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
    </CD>
    
    <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
    </CD>
    
    <CD>
    <TITLE>Still got the blues</TITLE>
    <ARTIST>Gary Moore</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virgin records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1990</YEAR>
    </CD>
    
    <CD>
    <TITLE>Eros</TITLE>
    <ARTIST>Eros Ramazzotti</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>BMG</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
    </CD>
    
    <CD>
    <TITLE>One night only</TITLE>
    <ARTIST>Bee Gees</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1998</YEAR>
    </CD>
    
    <CD>
    <TITLE>Sylvias Mother</TITLE>
    <ARTIST>Dr.Hook</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS</COMPANY>
    <PRICE>8.10</PRICE>
    <YEAR>1973</YEAR>
    </CD>
    
    <CD>
    <TITLE>Maggie May</TITLE>
    <ARTIST>Rod Stewart</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Pickwick</COMPANY>
    <PRICE>8.50</PRICE>
    <YEAR>1990</YEAR>
    </CD>
    
    <CD>
    <TITLE>Romanza</TITLE>
    <ARTIST>Andrea Bocelli</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.80</PRICE>
    <YEAR>1996</YEAR>
    </CD>
    
    <CD>
    <TITLE>When a man loves a woman</TITLE>
    <ARTIST>Percy Sledge</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1987</YEAR>
    </CD>
    
    <CD>
    <TITLE>Black angel</TITLE>
    <ARTIST>Savage Rose</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Mega</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1995</YEAR>
    </CD>
    
    <CD>
    <TITLE>1999 Grammy Nominees</TITLE>
    <ARTIST>Many</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Grammy</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1999</YEAR>
    </CD>
    
    <CD>
    <TITLE>For the good times</TITLE>
    <ARTIST>Kenny Rogers</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Mucik Master</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1995</YEAR>
    </CD>
    
    <CD>
    <TITLE>Big Willie style</TITLE>
    <ARTIST>Will Smith</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
    </CD>
    
    <CD>
    <TITLE>Tupelo Honey</TITLE>
    <ARTIST>Van Morrison</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1971</YEAR>
    </CD>
    
    <CD>
    <TITLE>Soulsville</TITLE>
    <ARTIST>Jorn Hoel</ARTIST>
    <COUNTRY>Norway</COUNTRY>
    <COMPANY>WEA</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1996</YEAR>
    </CD>
    
    <CD>
    <TITLE>The very best of</TITLE>
    <ARTIST>Cat Stevens</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Island</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1990</YEAR>
    </CD>
    
    <CD>
    <TITLE>Stop</TITLE>
    <ARTIST>Sam Brown</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>A and M</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1988</YEAR>
    </CD>
    
    <CD>
    <TITLE>Bridge of Spies</TITLE>
    <ARTIST>T'Pau</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Siren</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
    </CD>
    
    <CD>
    <TITLE>Private Dancer</TITLE>
    <ARTIST>Tina Turner</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Capitol</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1983</YEAR>
    </CD>
    
    <CD>
    <TITLE>Midt om natten</TITLE>
    <ARTIST>Kim Larsen</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Medley</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1983</YEAR>
    </CD>
    
    <CD>
    <TITLE>Pavarotti Gala Concert</TITLE>
    <ARTIST>Luciano Pavarotti</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>DECCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1991</YEAR>
    </CD>
    
    <CD>
    <TITLE>The dock of the bay</TITLE>
    <ARTIST>Otis Redding</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
    </CD>
    
    <CD>
    <TITLE>Picture book</TITLE>
    <ARTIST>Simply Red</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Elektra</COMPANY>
    <PRICE>7.20</PRICE>
    <YEAR>1985</YEAR>
    </CD>
    
    <CD>
    <TITLE>Red</TITLE>
    <ARTIST>The Communards</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>London</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1987</YEAR>
    </CD>
    
    <CD>
    <TITLE>Unchain my heart</TITLE>
    <ARTIST>Joe Cocker</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>EMI</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1987</YEAR>
    </CD>
    </CATALOG>
    

     

    I copied anything from the tutorial, but I cannot get it to work. Can anyone tell me what I've done wrong? I'm not sure whcih section is the problem, so I wasn't sure which forum to put it in. Any help? Please?

     

    I am getting this when I make a selection:

     

     

    Warning:  DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in file:///C:/wamp/www/IP4/cd_catalog.xml, line: 2 in C:\wamp\www\IP4\getcd.php on line 5

     

    Notice:  Undefined variable: y in C:\wamp\www\IP4\getcd.php on line 21

     

    Notice:  Trying to get property of non-object in C:\wamp\www\IP4\getcd.php on line 21

     

    Notice:  Trying to get property of non-object in C:\wamp\www\IP4\getcd.php on line 23

  6. Is there a way to insert 'Null' into a field if a form's posted values has been left empty? I want to allow a user to have the option of filling to a form field or not, but I would like to insert Null instead of a blank space into my form. Is this possible? I have a page that displays the optional field values, and if they are null, I have it set to not show.

  7. I think I was totally over tired last night/this morning when I was doing the calculation. I got it now:

     

    //get points per level
    $get_points = "select * from neutral_attributes where id = '$attribute_id'";
    $get_points_res = mysql_query($get_points, $conn) or die(mysql_error());
    
    while ($points_info = mysql_fetch_array($get_points_res)){
    $points = $points_info['points'];
    
    
    //get attribute names
    $get_names = "select * from neutral_attributes where id = '$attribute_id'";
    $get_names_res = mysql_query($get_names, $conn) or die(mysql_error());
    
    while ($names = mysql_fetch_array($get_names_res)){
    $attribute = $names['attribute'];
    
    $points_used = ($level_id * $points);
    
    $display_block .= "<tr><td>$attribute</td>
    <td>$level_id - $points - $points_used</td>
    <td><a href=remove_attribute.php?id=$_GET[id]>Remove</a></td></tr>";
    
    }
    }
    }
    

     

    Thanks for the help guys. Sorry for the confusion!!!

  8. while ($row = mysql_fetch_array($sum_col_res)){
    $total = 0;
    $sum = $row['SUM(points)'];
       for ($i=0;$i<strlen($row['SUM(points)']);$i++) {
          $total = $total + $row['SUM(points)'][$i];
       }
    }
    
    echo $total;

     

    You example is still bringing the same result: 1114

  9. Perhaps the points column doesn't have a numeric type? Is it VARCHAR or something?

     

    It would be nice if you posted your table structure, and possibly some sample rows.

     

    points is and int type. Here are both table structures:

     

    neutral_attributes:

    CREATE TABLE IF NOT EXISTS `neutral_attributes` (

      `id` int(11) NOT NULL AUTO_INCREMENT,

      `attribute` varchar(150) DEFAULT NULL,

      `points` int(11) NOT NULL,

      PRIMARY KEY (`id`)

    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

     

     

    sample rows:

    (1, 'Acrobatics', 1),

    (2, 'Appearance', 1),

    (3, 'Art Of Distraction', 1)

     

     

    scout_neutral_attributes:

    CREATE TABLE IF NOT EXISTS `scout_neutral_attributes` (

      `id` int(11) NOT NULL,

      `identity` varchar(150) DEFAULT NULL,

      `attribute_id` int(11) DEFAULT NULL,

      `level_id` int(11) DEFAULT NULL,

      `notes` longtext

    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

     

    sample rows:

    (2, 'Sailor Moon', 2, 4, NULL),

    (3, 'Sailor Moon', 3, 4, NULL),

    (4, 'Sailor Moon', 4, 2, NULL)

  10. I have a sort of confusing column I need the sum of. I have the following code:

     

    //get levels
    $get_levels = "select * from scout_neutral_attributes where identity = '$identity'";
    $get_levels_res = mysql_query($get_levels, $conn) or die(mysql_error());
    
    while ($level_info = mysql_fetch_array($get_levels_res)){
    $neutral_id = $level_info['attribute_id'];
    $level_id = $level_info['level_id'];
    
    
    
    //get points per level
    $get_points = "select * from neutral_attributes where id = '$neutral_id'";
    $get_points_res = mysql_query($get_points, $conn) or die(mysql_error());
    
    while ($point_info = mysql_fetch_array($get_points_res)){
    $point_id = $point_info['id'];
    $points = $point_info['points'];
    
    }
    }
    

     

    I need to get the sum of the column that is $points. If I echo $points, it's like 1114 (this is four record values in a line). I need to get the total of this: 7 instead of 1114. How can I do that? Can anyone help? This is driving me crazy! I have been trying for a while and can't get it.

     

    $sum_col = "select *, SUM(points) from neutral_attributes where id = '$neutral_id'";
    $sum_col_res = mysql_query($sum_col) or die(mysql_error());
    
    while ($row = mysql_fetch_array($sum_col_res)){
    $sum = $row['SUM(points)'];
    }
    

     

    But it's still returning 1114 instead of the total of 7 that I want. Can anyone help, please!!!????

  11. What did I do wrong? Every time my form submits, it inserts the record, along with a blank record. I have had this problem before, but I can't remember what caused it. Can anyone help? Here is the form and insert page:

     

    form:

    <?php
    
    $id = $_GET['id'];
    
    //get defects
    $get_defects = "select * from defects";
    $get_defects_res = mysql_query($get_defects, $conn) or die(mysql_error());
    
    $display_block = "<form action=insert_defect.php?id=$id method='post'>
    <table><th>Defect</th><th>Level</th>
    <tr><td><select name='defect'>";
    
    while($defect_info = mysql_fetch_array($get_defects_res)){
    $id = $defect_info['id'];
    $defect = $defect_info['defect'];
    
    $display_block .= "<option value=$id>$defect</option>";
    
    }
    
    $display_block .= "</select></td>";
    
    $display_block .= "<td><select name=level>
    <option value=1>1 BP</option>
    <option value=2>2 BP</option>
    </select></td></tr>
    <tr><td> </td></tr>
    <tr><td colspan=2><b>Description:</b> <i>(optional)</i><br />
    <input type=text name=desc>
    (Ex. Phobia - <u>Storms</u>)</td></tr>
    <tr><td> </td></tr>
    <tr><td colspan=2><b>Notes:</b> <i>(optional)</i><br />
    <textarea name=notes></textarea><br />
    (Ex. Although Rini is not a Sailor Scout when she first arrives,<br />she experiences a large degree of ageism (2 BP). Serena and<br />
    the others simply treat Rini as a young child, forgetting how<br /> mature Rini really is for her age.)</td></tr>
    <tr><td> </td></tr>
    <tr><td><input type=submit value='Add Defect'> <input type=button value=Cancel></td></tr>
    </table>
    
    </form>";
    
    echo $display_block;
    
    ?>
    

     

    insert:

    <?php
    
    //get identity
    $get_identity = "select * from scouts where id = '$_GET[id]'";
    $get_identity_res = mysql_query($get_identity, $conn) or die(mysql_error());
    
    while($identity_info = mysql_fetch_array($get_identity_res)){
    $identity = $identity_info['identity'];
    }
    
    //insert defects
    $insert_defect = "insert into scout_defects values ('', '$identity', '$_POST[defect]', '$_POST[desc]', '$_POST[level]', '$_POST[notes]')";
    $insert_defect_res = mysql_query($insert_defect, $conn) or die(mysql_error());
    
    ?>
    

  12. 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
    
    ?>
    

  13. 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???

  14. Wow, I finally got it working. Here is what I did:

     

    //OPEN TRESURE CHESTS
    if ($action == 'open'){
    
    echo "<h1>Burg Village</h1>
    <h3>Open All Treasure Chests</h3>";
    
    //check acquired table for existing field values
    $check_table_acquired = mysql_query("select * from acquired", $conn);
    $num_rows_acquired = mysql_num_rows($check_table_acquired);
    
    //if acquired table is empty
        if ($num_rows_acquired == 0){
        
    //add the record to the table
    $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')";
    $add_record_res = mysql_query($add_record, $conn) or die(mysql_error());
    
    echo "You have acquired the following items:<br /><br />
    Angel Tear<br />
    Star Light<br />
    Herb<br />
    Dragonfly Wing<br /><br />
    
    The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>.";
    
    //ADD ITEMS HERE
    
    //check inventory for existing field values
    $check_inventory = mysql_query("select * from inventory", $conn);
    $num_rows_inventory = mysql_num_rows($check_inventory);
    
    //if inventory is empty
        if ($num_rows_inventory == 0){
        
        //insert the items
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
        
        }//end inventory is empty if statement
        
        if ($num_rows_inventory >= 1){
        
    //check inventory for existing records of player
    $check_inventory = mysql_query("select * from inventory where identity = '$identity'", $conn);
    $num_rows_inventory = mysql_num_rows($check_inventory);
    
        //if player has records in inventory
        if ($num_rows_inventory >= 1){
        
    //check if the items exist
    $check_inventory = "select * from inventory where identity = '$identity'";
    $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error());
    
    while ($items = mysql_fetch_array($check_inventory_res)){
    $item = $items['item'];
    $quantity = $items['quantity'];
    
    //ADD ITEMS HERE
    
    //if they do, update the quantity
    //if they don't, insert the item
        
    //STAR LIGHT
        if ($item == 'Star Light'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //HERB
    if ($item == 'Herb'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //DRAGONFLY WING
    if ($item == 'Dragonfly Wing'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //ANGEL TEAR
    if ($item == 'Angel Tear'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    }//end while statement for checking for items in inventory
        
        }//end of existing player records in inventory if statement
        
        //if player has no records in inventory
        if ($num_rows_inventory == 0){
        
        //insert the items
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
        
        }//end no player exisiting records in inventory if statement
        
        }//end inventory records exist if statement
        
        }//end of empty acquired if statement
        
        if ($num_rows_acquired >= 1){
        
    //if acquired has existing records, check if player has acquired items from location
    $check_acquired = mysql_query("select * from acquired where identity = '$identity'");
    $num_rows_acquired = mysql_num_rows($check_acquired);
    
    while ($acquired_items = mysql_fetch_array($check_acquired)){
    $location = $acquired_items['location'];
    $acquired = $acquired_items['acquired'];
    }
    
    //if player has records in the acquired table
        if ($num_rows_acquired >= '1'){
        
        //if player has acquired items from location
        if ($location == 'Burg Inn' && $acquired == '1'){
        
        echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>.";
        
        }//end of acquired items from location if statement
        
        }//end check if player has records in acquired table
        
        if ($num_rows_acquired == 0){
        
    //add the record to the table
    $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')";
    $add_record_res = mysql_query($add_record, $conn) or die(mysql_error());
    
    echo "You have acquired the following items:<br /><br />
    Angel Tear<br />
    Star Light<br />
    Herb<br />
    Dragonfly Wing<br /><br />
    
    The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>.";
        
        //INSERT ITEMS HERE
        
        //check inventory for existing field values
    $check_inventory = mysql_query("select * from inventory", $conn);
    $num_rows_inventory = mysql_num_rows($check_inventory);
    
    //if inventory is empty
        if ($num_rows_inventory == 0){
        
        //insert the items
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
        
        }//end inventory is empty if statement
        
        if ($num_rows_inventory >= 1){
        
    //check inventory for existing records of player
    $check_inventory = mysql_query("select * from inventory where identity = '$identity'", $conn);
    $num_rows_inventory = mysql_num_rows($check_inventory);
    
        //if player has records in inventory
        if ($num_rows_inventory >= 1){
        
    //check if the items exist
    $check_inventory = "select * from inventory where identity = '$identity'";
    $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error());
    
    while ($items = mysql_fetch_array($check_inventory_res)){
    $item = $items['item'];
    $quantity = $items['quantity'];
    
    //ADD ITEMS HERE
    
    //if they do, update the quantity
    //if they don't, insert the item
        
    //STAR LIGHT
        if ($item == 'Star Light'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //HERB
    if ($item == 'Herb'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //DRAGONFLY WING
    if ($item == 'Dragonfly Wing'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    //ANGEL TEAR
    if ($item == 'Angel Tear'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        }
        
    $check_items = mysql_query("select * from inventory where item = '$item'");
    $num_rows_items = mysql_num_rows($check_items);
    
        if ($num_rows_items == 0){    
        
    $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }//end if statement for $num_rows_items == 0
        
    }//end while statement for checking for items in inventory
        
        }//end of existing player records in inventory if statement
        
        //if player has no records in inventory
        if ($num_rows_inventory == 0){
        
        //insert the items
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
        
        }//end no player exisiting records in inventory if statement
        
        }//end inventory records exist if statement
        
        }//end check for player not having acquired items from location
        
        }//end of if statement for existing records in acquired table
    
    }//end of action if statement
    

     

    It only took eight tries!!!

  15. I'm still working on this page. So far I have the section that checks the acquired table for records working properly. It checks if the table is empty. If it is not empty, it checks if the player has a record, and if they have a record, it checks if they have acquired the items from the location. If they have, it tells them. If they haven't, it inserts the record into the acquired table. If the acquired table is empty, it inserts the record into the table. Here is the working code so far:

     

    //OPEN TRESURE CHESTS
    if ($action == 'open'){
    
    echo "<h1>Burg Village</h1>
    <h3>Open All Treasure Chests</h3>";
    
    //check acquired table for existing field values
    $check_table_acquired = mysql_query("select * from acquired", $conn);
    $num_rows_acquired = mysql_num_rows($check_table_acquired);
    
    //if acquired table is empty
        if ($num_rows_acquired == 0){
        
    //add the record to the table
    $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')";
    $add_record_res = mysql_query($add_record, $conn) or die(mysql_error());
    
    echo "You have acquired the following items:<br /><br />
    Angel Tear<br />
    Star Light<br />
    Herb<br />
    Dragonfly Wing<br /><br />
    
    The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>.";
    
    //ADD ITEMS HERE
        
        }//end of empty acquired if statement
        
        if ($num_rows_acquired >= 1){
        
    //if acquired has existing records, check if player has acquired items from location
    $check_acquired = mysql_query("select * from acquired where identity = '$identity'");
    $num_rows_acquired = mysql_num_rows($check_acquired);
    
    while ($acquired_items = mysql_fetch_array($check_acquired)){
    $location = $acquired_items['location'];
    $acquired = $acquired_items['acquired'];
    }
    
    //if player has records in the acquired table
        if ($num_rows_acquired >= '1'){
        
        //if player has acquired items from location
        if ($location == 'Burg Inn' && $acquired == '1'){
        
        echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>.";
        
        }//end of acquired items from location if statement
        
        }//end check if player has records in acquired table
        
        if ($num_rows_acquired == 0){
        
    //add the record to the table
    $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')";
    $add_record_res = mysql_query($add_record, $conn) or die(mysql_error());
    
    echo "You have acquired the following items:<br /><br />
    Angel Tear<br />
    Star Light<br />
    Herb<br />
    Dragonfly Wing<br /><br />
    
    The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>.";
        
        //INSERT ITEMS HERE
        
        }//end check for player not having acquired items from location
        
        }//end of if statement for existing records in acquired table
    
    }//end of action if statement
    

     

    Now I need to get the inventory section working. I need to check if the inventory table is empty. If it is, I need to simply insert all items. If it is not empty, I need to check if the player has items. If the player has items, I need to check if each item exists in the table. If it does, I need to update the quantity. If it does not, I need to insert the item into the table. This is where I have the most frustration.

  16. Right now, if the acquired table is empty, the record inserts and the inventory inserts, but the items in the inventory do not update the quantity as expected, they add a whole new record, except for the Angel Tear; it updates that one item properly. But if the acquired table is not empty, if will not insert the record into the acquired table nor will it insert the items into the inventory table. I can't figure this out!!!! Please help me!!!

     

    And if the player already has a record in the acquired table that signifies that they have acquired the items from the location, it doesn't say anything. I know I have coded it wrong, I just can't figure out what I am missing.

  17. I keep doing this code wrong. Can someone please help me get it right? Here is what I have:

     

    //OPEN TRESURE CHESTS
    if ($action == 'open'){
    
    echo "<h1>Burg Village</h1>
    <h3>Open All Treasure Chests</h3>";
    
    //check acquired table for existing field values
    $check_table_acquired = mysql_query("select * from acquired", $conn);
    $num_rows_acquired = mysql_num_rows($check_table_acquired);
    
        //if acquired table is empty
        if ($num_rows_acquired == 0){
        
    //add the record to the table
    $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')";
    $add_record_res = mysql_query($add_record, $conn) or die(mysql_error());
        
        }//end empty acquired table if statement
        
        //if acquired has existing records, check if player has acquired items from location
    $check_acquired = mysql_query("select * from acquired where identity = '$identity'");
    $num_rows_acquired = mysql_num_rows($check_acquired);
    
    while ($acquired_items = mysql_fetch_array($check_acquired)){
    $location = $acquired_items['location'];
    $acquired = $acquired_items['acquired'];
    }
    
        //if player has records in the acquired table
        if ($num_rows_acquired >= '1'){
        
        //if player has acquired items from location
        if ($location == 'Burg Inn' && $acquired == '1'){
        
        echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>.";
        
        }//end of acquired items from location if statement
        
        }//end of $num_rows_acquired =< 1 if statement
        
        //if player has not acquired items from this location
        else {
        
    //check inventory for existing field values
    $check_inventory = mysql_query("select * from inventory", $conn);
    $num_rows_inventory = mysql_num_rows($check_inventory);
    
        //if inventory is empty
        if ($num_rows_inventory == 0){
        
    //insert the items
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
    
    $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); 
        
        }//end of $num_rows_inventory == 0 if statement
        
        //if inventory has existing records
        if ($num_rows_inventory >= 1){
        
    //check if the items exist
    $check_inventory = "select * from inventory where identity = '$identity'";
    $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error());
    
    while ($items = mysql_fetch_array($check_inventory_res)){
    $item = $items['item'];
    $quantity = $items['quantity'];
    }
    
    //if they do, update the quantity
    //if they don't, insert the item
        if ($item == 'Star Light'){
    
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
        
        } else {
        
    $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }
        
        if ($item == 'Herb'){
        
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
    
        } else {
        
    $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }
        
        if ($item == 'Dragonfly Wing'){
        
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
    
        } else {
        
    $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }
        
        if ($item == 'Angel Tear'){
        
    $new_quantity = ($quantity + 1);
        
    $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'";
    $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error());
    
        } else {
        
    $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
        }
        
        }//end of $num_rows_inventory >= 1 if statement
        
        }//end of not acquired items if statement
    
    }//end action if statement
    

     

    What I need to do is this:

     

    If the action is 'open', I need to check if any records exist in the acquired table. If records don't exist, I need to add the record. If records exist, i need to check if the player has already acquired the items from this location. If the player has acquired the items, then tell them so. If the player has not acquire the items, insert the record, then check the inventory table. If the inventory table is empty, insert the items. If the inventory table has existing records, check if the player has existing items. If the player has existing items, check if each item exists. If it exists, update the quantity. If it doesn't, insert the item. Can anyone please help me with what I am doing wrong??? Please, it is driving me CRAZY!!!

     

    I think I am missing the part where it inserts the record for the acquired items if the player does not have a record in the acquire table.

  18. I guess I found a way to do it:

     

    <?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'];
    
    }
    
    //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 - $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]', '1')";
    $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error());
    
    echo "inserted";
    
    }
    
    
    ?>
    

  19. So far I have this:

     

    //check for existing items
    
    if ($item == $_POST['sel_item']){
    
    $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'];
    
    }
    
    //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 - $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());
    
    } else {
    
    //insert item
    
    }
    }
    

     

    But how can I insert the item if the $_POST[sel_item] didn't exist in the table? I tried a regular insert statement, but it is inserting the sel_item for each other item that is already in the inventory table that isn't the sel_item. Can anyone help?

     

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

     

    Ho can I just insert the record once?

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