Jump to content

Grabbing Name Vaule From Database


Deanznet

Recommended Posts

  • Replies 63
  • Created
  • Last Reply

Top Posters In This Topic

Ah, I see the problem now. Change ONLY the part of code I am posting

 

<?php

    //check if they submitted the form
    if (isset($_POST['submit'])) {
        
        if ($_POST['action'] == 'buy') {
            
            echo "These are the items that were selected [they chose 'buy']<br>";
            
            //Loop through the selected items
            foreach($_POST['selected'] as $itemID){
                echo $itemID.'<br>';                
                                
                //get the items price
                $get_price = mysql_query("SELECT item_price FROM phpbb_trading WHERE item_name='$itemID'")
                or die(mysql_error());
                $price = mysql_fetch_assoc($get_price);
                
                //Check if they have enough
                if ($user_points['user_points'] > $price['item_price']) {
                     echo "<p><b>Price:</b> - {$price['item_price']}<p>";
                     
                     $update_q = "UPDATE phpbb_trading SET item_in_shop = '0',  itemid = '$user_id'
                     WHERE item_name = '$itemID'";
                     $update_q_final = mysql_query($update_q)or die(mysql_error().'With Query<p>'.$update_q);
                
                     echo "Item Bought<br>";
                     
                    //they have enough, now do a query to subtract their money
                    $subtract = "UPDATE users SET user_points=user_points-{$price['item_price']}
                    WHERE user_id='$user_id'";
                    $subtract_final = mysql_query($subtract)or die('ERROR: '.mysql_error().' with query<br>'.$subtract);
                    
                } else {
                    //they don't have enough money, give them an error...
                    echo "Not Enought Boomies'";
                }
            }
        } else {
            
            echo "They chose 'sell'";
        }
    }

?>

Link to comment
Share on other sites

Worked! But they they can buy an item and hit back and than rebuy and it just subtracts the price tho.. but since the item was already bought all it really dose is subtracts the users cash.

 

Anyway to prevent this..

 

And after this i just gotta work on the selling part but  I think i can do that using stuff i learned from here.. just a quick question.

 

                    $subtract = "UPDATE users SET user_points=user_points-{$price['item_price']}
                    WHERE user_id='$user_id'";

 

Can i add a - to a + will that work?

Link to comment
Share on other sites

                    
$subtract = "UPDATE users SET user_points=user_points-{$price['item_price']}
                  WHERE user_id='$user_id'";

 

Can i add a - to a + will that work?

 

You can change the (-) to a (+), then it would GIVE them points. So yes, if someone sold something, to give them the points you would use a plus instead, but also make sure to do a query to subtract from the buyer as well.

 

But they they can buy an item and hit back and than rebuy and it just subtracts the price tho.. but since the item was already bought all it really dose is subtracts the users cash.

 

Why does this matter? Are they not allowed to buy the same thing twice? If not...instead of trying to enable the back button, make a query checking if they have that item already, if they do, then give them an error saying they already have it.

 

Link to comment
Share on other sites

<?php
define('IN_PHPBB', true);
define('IN_CASHMOD', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//
// Sorry , only logged users ...
if ( !$userdata['session_logged_in'] )
{
$redirect = "traded.$phpEx";
$redirect .= ( isset($user_id) ) ? '&user_id=' . $user_id : '';
header('Location: ' . append_sid("login.$phpEx?redirect=$redirect", true));
}

if($userdata['session_logged_in'])


{

// Who is looking at this page ?
$user_id = $userdata['user_id'];
if ( (!( isset($HTTP_POST_VARS[post_USERS_URL]) || isset($HTTP_GET_VARS[post_USERS_URL]))) || ( empty($HTTP_POST_VARS[post_USERS_URL]) && empty($HTTP_GET_VARS[post_USERS_URL])))
{ 
$view_userdata = $userdata; 
} 
else 
{ 
$view_userdata = get_userdata(intval($HTTP_GET_VARS[post_USERS_URL])); 
} 
$searchid = $view_userdata['user_id'];
$points = $userdata['user_points'];
$posts = $userdata['user_posts'];

//select the users money
$query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'")or die(mysql_error());
$user_points = mysql_fetch_assoc($query);
    
    
    //check if they submitted the form
    if (isset($_POST['submit'])) {
        
        if ($_POST['action'] == 'sell') {
            
            echo "<br>";
            
            //Loop through the selected items
            foreach($_POST['selected'] as $itemID){
                echo $itemID.'<br>';                
                                
                //get the items price
                $get_price = mysql_query("SELECT item_sellback FROM phpbb_trading WHERE item_name='$itemID'")
                or die(mysql_error());
                $price = mysql_fetch_assoc($get_price);
                
             
                     
                     $update_q = "UPDATE phpbb_trading SET item_in_shop = '1',  itemid = '0'
                     WHERE item_name = '$itemID'";
                     $update_q_final = mysql_query($update_q)or die(mysql_error().'With Query<p>'.$update_q);
                
                     echo "Item Sold<br>";
                     
                    //they have enough, now do a query to add their money
                    $add = "UPDATE phpbb_users SET user_points=user_points+{$price['item_sellback']}
                    WHERE user_id='$user_id'";
                    $add_final = mysql_query($add)or die('ERROR: '.mysql_error().' with query<br>'.$add);
}
        }
    }
    
    $result = mysql_query("SELECT * FROM phpbb_trading WHERE item_in_shop = '0'and itemid = '$user_id' ")or die(mysql_error());
    $num_rows = mysql_num_rows($result);
    
    print "<br>There are $num_rows cards in your inventory <P>";
    print "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
    print '<table width=500 height= 100  border=1>'."\n";
    
    while ($get_info = mysql_fetch_assoc($result)) {
        print "<tr>";
        
        print "\t<td><font face=arial size=1/>";
        
        print "<input type='checkbox' name='selected[]' value='{$get_info['item_name']}' />"
        .$get_info['item_name'].'<br><td><center>'.$get_info['item_price'].'<center>'.Boomies.'<br><td><center>Sell Back Price:<center>'.$get_info['item_sellback'].'<br><td><center>'.$get_info['item_desc']."</font></td>";
        print "</tr>";
    }
    
    echo '<select name="action">'
    .'<option value="sell">sell</option>'
    .'</select><p>';
    echo '<input type="submit" name="submit">';
    echo '</form>';

}

else
{
echo('You are a guest');
}
?>

 

Code for invetory Works Well! But problem is they can hit the back and keep sell items that where already sold and get more money.. How do i fixed this?

 

As for the buying more than one item I was thinking on how i can do this..

 

Is their a way to make a table in a colum called item_quanity and put a vaule in their. But how will you know who buys it because when they buy it, it changes just changes the userid to the persons id who bought it. if you understand lol I can try to explain better

 

Link to comment
Share on other sites

how will you know who buys it because when they buy it, it changes just changes the userid to the persons id who bought it.

 

Uhhh...that just sounds like a bad database setup.

 

Here is how I would do it.

 

You would first setup a table for all the individual items

TABLE items
------------
itemID
name
quantity
price
...whatever else

 

Now for the user owned items...for items that were bought

TABLE user_items
-----------------
itemID (links to the itemID in "items" table)
ownerID (who owns the item)
...whatever else

 

With that setup, you can always control how much is in stock at the store, easily check who owns what item, and how much, or whatever else.

Link to comment
Share on other sites

Hmm i see but i dont understand how to link them..

 

and the the mysql querys.

 

Like for displaying in the store and buying tell me if this is right..

 

This is just part of the code we will say the tables are named what you named them but where going to add item_in_shop to the items table so we can tell if its in shop or now 0 is not 1 is in shop.

 

                       
                //get the items price
                $get_price = mysql_query("SELECT price FROM items WHERE item_name='$itemID'")
                or die(mysql_error());
                $price = mysql_fetch_assoc($get_price);
                
                               
                //Check if they have enough
                if ($user_points['user_points'] > $price['item_price']) {
                     echo "<p><b>Price:</b> - {$price['item_price']}<p>"; 


          [b]          //THis is where i get confused Do i insert a new row with a new item id and owner id?
[/b]
                     $update_q = "UPDATE phpbb_trading SET item_in_shop = '0',  itemid = '$user_id'
                     WHERE item_name = '$itemID'";
                     $update_q_final = mysql_query($update_q)or die(mysql_error().'With Query<p>'.$update_q);
                
                     echo "Item Bought<br>";
                     
                    //they have enough, now do a query to subtract their money
                    $subtract = "UPDATE phpbb_users SET user_points=user_points-{$price['item_price']}
                    WHERE user_id='$user_id'";
                    $subtract_final = mysql_query($subtract)or die('ERROR: '.mysql_error().' with query<br>'.$subtract);
          } else {
                    //they don't have enough money, give them an error...
                    echo "Not Enought Boomies";
               }
            }
        } else {
            
            echo "They chose 'sell'";
        }
    }
    
    $result = mysql_query("SELECT * FROM items WHERE item_in_shop = '1' ")or die(mysql_error());
    $num_rows = mysql_num_rows($result);

Link to comment
Share on other sites

And how would that be accomplished?

 

It's very hard to explain, here is a link

http://www.tonymarston.net/php-mysql/many-to-many.html#a1

 

Basically it's two or more tables in a database that share one common unique field. That way, you can create a query that can grab information from both tables at once without having to store replicated information.

Link to comment
Share on other sites

Let's start with the basics. Forget about how that can be accomplished.  Just take it slow and digest the concept. I'll try to explain this to the best of my English.

 

For now let's use these definitions, obviously there is more into it.

Database ->  is a collection of tables

Table -> is a collection of records.

Record -> is a collection of data.

Field -> is the property of a table.

 

Now consider a name.  For simplicity it has two parts the firstname and the lastname.  These are properties of a name.  These are the fields of the table called name.  Example of this can be:

 

Pedro Penduko

Enteng Kabisote

 

The preceeding are records.  If you group them together like this:

 

Firstname Lastname
Pedro Penduko
Enteng Kabisote

 

You now have a table.  Consider another table:

 

Company name Owner's Lastname
Company X Penduko
Company Y Penduko
Company Z Kabisote

 

By LOGIC you can link (relate) the firstname of the owner based on the Owner's Lastname of the second table.

 

Wikipedia is your friend: http://en.wikipedia.org/wiki/Database

Link to comment
Share on other sites

Okay.. Thanks.

 

From my understanding and from reading the link pocobueno1388 posted.

 

I need 3 Tables.

 

Items Table

-item_Aid

-item_price

-item_desc

-item_quanity

 

user_item Table

-item_Bid

-item_price

 

and their has to have atleast one same record in the tables witch is item_id

 

and you need a table that connects both right?

 

Itemconnect

-item_Aid

-item_Bid

 

something like that?

Link to comment
Share on other sites

Items Table

-item_id

-item_price

-item_desc

-item_quanity

 

user_item Table

-item_id

-item_price

 

The link pocobueno posted is based on a many-to-many relationship.  I think what you are trying to achieve here is a one-to-many relationship.

Link to comment
Share on other sites

Couldent find anyone on one-to-many nothing explaining it..

 

 

is this anywhere close to it

 

Items Table
-item_id
-item_price
-item_desc
-item_quanity
-item_in_shop

user_item Table
-item_id
-item_price

k those are connected because of the item_id record.

Okay so when pulling information 

It would be something like this

[code]SELECT items.item_id, items.item_price, items.item_quanity
FROM items
LEFT JOIN user_item ON (item.item_id = user_item.item_id)
WHERE (item.item_in_shop = '1')

[/code]

 

 

Link to comment
Share on other sites

Your getting closer. You don't need two item_price fields, if they hold the same information at least.

 

Your query is very close, but there would be no point in doing a join as your not selecting anything from the other table.

 

You could do something like this

 

SELECT i.item_id, i.item_quantity, ui.item_price
FROM items i
LEFT JOIN user_items ui
ON i.item_id = ui.item_id

Link to comment
Share on other sites

Whats i and ui stand for?  ???

 

Items Table

-item_id

-item_price

-item_desc

-item_quanity

-item_in_shop

-item_sellbackprice

 

user_item Table

-item_id

-item_owner_id

 

 

and what dose that query selects both tables?

 

Also when a user lets says buys an item how dose it take away from the quanity ? dose it subtract 1 from the quanity and than inserts a record into user_item?

 

Link to comment
Share on other sites

Almost there...

 

SELECT i.item_id, i.item_quantity, ui.item_price
FROM items i
LEFT JOIN user_items ui
ON i.item_id = ui.item_id

 

SELECT                 database command

i.field1, i.field2 ....   the fields

FROM                    database command

item                      the table

i                           the alias

LEFT JOIN              database command

users_items            another table

ui                        alias

ON                        database command

i.item_id = ui.item_id condition

 

www.tizag.com/mysqlTutorial/mysqljoins.php

www.http://dev.mysql.com/doc/refman/5.0/en/join.html

Link to comment
Share on other sites

God this confuses me haha..

 

Okay i get what i and ui their just names,

 

Items Table

-item_id

-item_price

-item_desc

-item_quanity

-item_in_shop

-item_sellbackprice

 

user_item Table

-item_id

-item_owner_id

 

 

SELECT i.item_id, i.item_quantity, ui.item_price
FROM items i
LEFT JOIN user_items ui
ON i.item_id = ui.item_id

 

This code selects item_id, Item_quantity and item_price than it joins both tables right? man to be honest i am lost HaHA my head just spinning i cant grasp a hold of anything

Link to comment
Share on other sites

Okay so like this

 

Items Table

-item_id

-item_price

-item_desc

-item_quanity

-item_in_shop

-item_sellbackprice

 

user_item Table

-item_id

-item_owner_id

 

SELECT item.item_id
FROM item, user_item
WHERE Item.item_id=user_item_id
AND user_item.item_owner_id='$user_id'

 

 

haha if thats correct that should be the one for the invivotiry.. Should display  the items with the user_id

Link to comment
Share on other sites

Okay, once again your selecting ONLY from one table in that query...so there is no need for a join.  You only need to join the tables if you are selecting information from TWO or more tables with some sort of relationship.

 

Don't worry about not understanding it right away. It took me FOREVER to be able to do joins. I finally got so frustrated with not being able to perform more advanced queries that I sat down and just kept reading and practicing. It will come to you eventually, you just have to be patient when your first learning something ;)

 

Also, the query you just did will substitute fine for a join, but I've heard it isn't as fast or as efficient.

Link to comment
Share on other sites

Items Table

-item_id

-item_price

-item_desc

-item_quanity

-item_in_shop

-item_sellbackprice

 

user_item Table

-item_id

-item_owner_id

 

 

SELECT item.item_id, item.item_desc, item.item_sellbackprice, user_item.item_id
FROM item, user_item
WHERE Item.item_id=user_item_id
AND user_item.item_owner_id='$user_id'

 

That right?

Link to comment
Share on other sites

Now what will that display.. everything that i selected? so that would basicly be the invetory part..

 

But im still stuck!

 

because even if that displays it..

 

How would i sell it.. and updata the mysql?

 

 

            //get the items price
                $get_price = mysql_query("SELECT item_sellback FROM Items WHERE Item.item_id=user_item_id
AND user_item.item_owner_id='$user_id'")
                or die(mysql_error());
                $price = mysql_fetch_assoc($get_price);
                
             
                     
                     $update_q = "UPDATE items SET item_in_shop = '1',  itemid = '0'
                     WHERE item_name = '$itemID'";
                     $update_q_final = mysql_query($update_q)or die(mysql_error().'With Query<p>'.$update_q);
               

 

I guess Prob not right HAHAHAHA lol im going crazy lol..

 

Okay and if that is right..

 

How about the buy system what makes it be able to check how much quanity is left.

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.