Jump to content

Adding gold from stock on click


Thauwa

Recommended Posts

the MySQL query should be like this

<?php
$query=mysql_query("UPDATE tableName SET fieldName=fieldName+1 WHERE username=$UserName");
//You can can update multiple fields by separating them by comma, like

$query=mysql_query("UPDATE tableName SET Coins=Coins+1, timeField=$Time WHERE username=$UserName");
?>

Link to comment
Share on other sites

Actually yes, it is. I used it because I didn't know what field names do you have, so feel free to replace it with your index field name. The WHERE clause, tells MySQL which rows to update and if you ignore it, MySQL will update all rows in your table, which is not what you want, do you?

Link to comment
Share on other sites

then I could use the same basis to substract gold coins? I read in another forum topic (posted by Kryllster) a code like this..

 

 

<?php
function takeDiamond() {
$diamond = (Run a query to get amount of diamonds in inventory);
if ($diamond > 0) {
   $diamond--;
   (Run new query to update the database with new diamond amount);
   return true;
} else {
   return false;
}
?>

 

or can I simply change the signs in absorbater's code? + to -. like

tableName SET Coins=Coins-1

 

my next problem is the 'on click' part, as in the forum topic. can anyone help me with that?

Link to comment
Share on other sites

About the substract - yes, this

tableName SET Coins=Coins-1

is valid.

 

About the "on click" part - The takeDiamond function you provided is not bad. Try this:

<?php
function takeDiamond() {

   $query=mysql_query("SELECT * FROM tableName WHERE userName=$User");
   $fetch_query=mysql_fetch_array($query);
   $diamond = fetch_query["coins"];
    if ($diamond > 0) {
        $diamond--; //Or $diamond++; if you want to add coins
          if(mysql_query("UPDATE tableName SET coins='$diamond' WHERE userName=$User")) //Remember, I use userName field, but you can use any other field names
           return true;
          else
           return false;
   }
   else {
       return false;
   }
?>

 

Then call the function on the beginning of the files you need. But remember this - You have to specify the User Name as it is required by the function. You can pass it as an argument to the function or refer to the Variable that holds user name or id as a GLOBAL variable like:

<?php
function takeDiamond() {
$User = $GLOBALS["User"]; //This is the new thing.The "User" between "[" and "]" is name of a global variable (means that is outside the function). Replace it with the name of the actual variable that holds user name or user id
   $query=mysql_query("SELECT * FROM tableName WHERE userName=$User");
   $fetch_query=mysql_fetch_array($query);
   $diamond = fetch_query["coins"];
    if ($diamond > 0) {
        $diamond--; //Or $diamond++; if you want to add coins
          if(mysql_query("UPDATE tableName SET coins='$diamond' WHERE userName=$User")) //Remember, I use userName field, but you can use any other field names
           return true;
          else
           return false;
   }
   else {
       return false;
   }
?>

 

One more thing - you have to call mysql_query function as well as mysql_select_db in order to use takeDiamond without errors

Tell us if you get problems...

Link to comment
Share on other sites

ya, you mean like this:

<?php
if (takeDiamond(vars...)){
  do whatever is right...
}else{
  do whatever when it returns false
}
?>

 

or

 

<?php
$var=takeDiamond(vars...)
if ($var){
  do whatever is right...
}else{
  do whatever when it returns false
}
?>

 

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.