Page 1 of 1

Selling Problem

Posted: Sun Sep 08, 2013 11:58 pm
by Epiales
Okay, I've looked and looked and looked as to why it's not deducting the correct amount of gold when purchasing food. The food is added properly when you buy, but the gold is not deducting properly. There is no reason as to why that I see, so I'm bringing it to ya'll :) Thanks!

Code: Select all

<?php
session_start();
include("functions.php");
include("header.php");
connect();
include("safe.php");
if(!isset($_SESSION['uid'])){
 $id=$_SESSION['id'];
    echo "<script type='text/javascript'>window.location='index.php';</script>";
}else{

if(isset($_POST['buy'])){
    $buyfood = protect($_POST['buyfood']);    
    $gold_needed = (0.5 * $buyfood);
    
    if($buyfood == ""){
       
            output("You must enter an amount!");
 
    }elseif($stats['gold'] < $gold_needed){
        echo "You do not have enough gold!";
    
   }elseif($stats['gold'] = 0){
        echo "Please come back when you have gold!";
        
    }else{    
    $buyfood = protect($_POST['buyfood']);    
    $gold_needed = (0.5 * $buyfood);        
    $stats['gold'] = $stats['gold'] - $gold_needed;
    $stats['food'] += $buyfood;    
    $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' 
                                        WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
    $update_food = mysql_query("UPDATE `stats` SET `food`='".$stats['food']."' 
                                        WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());                                    
    }
}
?>
If you put in 100 then it takes you -50 and adds the 100 to the food. Then the -50 goes back to 0, so if you have 10K gold and buy 100 food, your gold goes all the way to zero LOL... Expensive food huh? :mrgreen: But I don't see anywhere that's causing it :(

Re: Selling Problem

Posted: Mon Sep 09, 2013 12:28 am
by Epiales
That was a quickie :mrgreen:

I removed the below and it works fine:

Code: Select all

   }elseif($stats['gold'] = 0){
        echo "Please come back when you have gold!";
         

Re: Selling Problem

Posted: Mon Sep 09, 2013 12:57 am
by Epiales
What I would like to do now is to setup where they have a 50/50 chance of making a successful transaction and an unsuccessful transaction. On unsuccessful, they would lose their gold and get nothing. Got me somewhere to start please? :? :?

Re: Selling Problem

Posted: Mon Sep 09, 2013 1:31 am
by Xaos
Epiales wrote:What I would like to do now is to setup where they have a 50/50 chance of making a successful transaction and an unsuccessful transaction. On unsuccessful, they would lose their gold and get nothing. Got me somewhere to start please? :? :?
May I ask why?
And it would be a simple rand.

Code: Select all

$roll = rand(0,1);
if($roll == 0){
transaction = succesful;
}elseif($roll == 1){
transaction = failed;
} 

Re: Selling Problem

Posted: Mon Sep 09, 2013 4:51 pm
by Epiales
Xaos wrote:
Epiales wrote:What I would like to do now is to setup where they have a 50/50 chance of making a successful transaction and an unsuccessful transaction. On unsuccessful, they would lose their gold and get nothing. Got me somewhere to start please? :? :?
May I ask why?
And it would be a simple rand.

Code: Select all

$roll = rand(0,1);
if($roll == 0){
transaction = succesful;
}elseif($roll == 1){
transaction = failed;
} 
Oh, I plan on making a regular market where players can buy/sell, but a black market where they will pay more and take a chance of losing LOL... That's for those peeps that can't wait for others :mrgreen:

Thanks for the reply. I'll see what I can do YAY

Re: Selling Problem

Posted: Mon Sep 09, 2013 5:41 pm
by Epiales
Thanks again YAY, I have it working. Now to make a player/player buy sell like market. :mrgreen:

Re: Selling Problem

Posted: Tue Sep 10, 2013 11:54 pm
by Epiales
Okay, now when trying to purchase something that a player has placed for sell, it's not doing anything. I'm really not sure why it's not. Maybe I've been at it too long today and I'm just missing something basic LOL... But this is what I have:

Code: Select all

if(isset($_GET['purchase'])){
$foodinfo="SELECT * from market";
$foodinfo2=mysql_query($foodinfo) or die("could not get market stats!");
$foodinfo3=mysql_fetch_array($foodinfo2);

$amount = $foodinfo3['amount'];
$playeramount = $stats['food'] + $amount;
$id = $foodinfo3['id'];

$updateplayer="update stats set food=$playeramount where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

 $updatemarket="DELETE from market where id='$id' limit 1";
  mysql_query($updatemarket) or die("Could not delete item");
  
  echo "Transaction completed";

}
And here's the form:

Code: Select all

<form action="market.php" method="get">
<div>             
    <div align="left" class="left"><?php echo $row["name"]; ?></div>			
    <div align="center" class="center"><?php echo $row["amount"]; ?></div>
	<div align="center" class="right"><?php echo $row["price"]; ?></div>
	<div class="link" align="center"><input type="submit" id="purchase" name="purchase" value="Purchase"></div>
<br><br></div>
</form>

Re: Selling Problem

Posted: Wed Sep 11, 2013 3:57 am
by Epiales
Okay, well I thought there might of been a conflict with id's, so I changed the id in the market to mid. New code below, but it's a no go either :cry: :cry:

Code: Select all

if(isset($_GET['purchase'])){
$foodinfo="SELECT * from market ORDER BY mid";
$foodinfo2=mysql_query($foodinfo) or die("could not get market stats!");
$foodinfo3=mysql_fetch_array($foodinfo2);

$amount = $foodinfo3['amount'];
$playeramount = $stats['food'] + $amount;
 $id=$_SESSION['uid'];
 $mid=$foodinfo3['mid'];

$update_gold = mysql_query("UPDATE `stats` SET `food`='".$playeramount."' 
                                        WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());

 $updatemarket="DELETE from market where mid='$mid'";
  mysql_query($updatemarket) or die("Could not delete item");
  
  echo "Transaction completed";

} 
Form:

Code: Select all

<form action="market.php" method="get">
<div>             
    <div align="left" class="left"><?php echo $row["name"]; ?></div>			
    <div align="center" class="center"><?php echo $row["amount"]; ?></div>
	<div align="center" class="right"><?php echo $row["price"]; ?></div>
	<div class="link" align="center"><input type="submit" id="purchase" name="purchase" value="Purchase"></div>
<br><br></div>
</form>
Aight, it's bedtime. Night everyone!

Re: Selling Problem

Posted: Wed Sep 11, 2013 4:47 pm
by Epiales
Okay, it's deleted records, but the very last record :D ... no matter what record you choose to purchase grrr. I also switched it to POST instead of GET :geek:

Code: Select all

//THIS IS THE BEGINNING OF THE CODE FOR WHEN YOU PUT PURCHASE SOMETHING ON THE REGULAR MARKET!!!//

if(isset($_POST['purchase'])){
$foodinfo="SELECT * from market ORDER BY amount";
$foodinfo2=mysql_query($foodinfo) or die("could not get market stats!");
$foodinfo3=mysql_fetch_array($foodinfo2);

$amount = $foodinfo3['amount'];
$playeramount = $stats['food'] + $amount;
 $id=$_SESSION['uid'];
 $mid=$foodinfo3['mid'];

$updatefood="update stats SET `food`='".$playeramount."'";
mysql_query($updatefood) or die ("Could not update player");
    
$updatemarket="DELETE from market where mid='$mid'";
mysql_query($updatemarket) or die("Could not delete item");
  
  echo "Transaction completed";

} 
I added a hidden field for the id in the form:

Code: Select all

<form action="market.php" method="post">
<div>             
    <div align="left" class="left"><?php echo $row["name"]; ?></div>			
    <div align="center" class="center"><?php echo $row["amount"]; ?></div>
	<div align="center" class="right"><?php echo $row["price"]; ?></div>
	<input type="hidden" id="hidden" name="hidden" value="<?php echo $row["mid"]; ?>">	
	<div class="link" align="center"><input type="submit" id="purchase" name="purchase" value="Purchase"></div>
<br><br></div>
</form>

Re: Selling Problem

Posted: Wed Sep 11, 2013 4:58 pm
by Xaos
I don't see where your actually purchasing. IE a check to make sure the user has enough money. Also, remember PHP is server side, so there has to be someway to communicate to the server "do this" , like a submit button.