How do I UPDATE Ajax Score in Mysql

  • Hi I am trying to +1 to a Mysql database with the Ajax plug-in,

    anyone got any pointers please ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Have you looked at this tutorial? Should get you started.

    https://www.scirra.com/tutorials/346/on ... -php-mysql

  • Have you looked at this tutorial? Should get you started.

    https://www.scirra.com/tutorials/346/on ... -php-mysql

    Hello farflamex,

    That is the one I am playing with.

    I have got down to " Parameters for AJAX: Post to URL " box and find there is now an extra Variable in it to fill in and am stuck on the code to post into them,

    Tag "wotever" < this is the local Handel to tell the difference ajax events .

    URL: " http://tomysite.com/plus1update.php" < the php file that adds One

    Data " ID.Text " < this is the integer of the Players ID so Ajax knows which ID in the base to update. {this i am now not sure is right}

    Method "UPDATE" <- this I am Guessing at.

    any ideas ?

    thanks.

  • I'm in a similar stage to you, so I may be able to help. I've been working away at communicating with a database for the past week, so I'm still in the early stages, but I'm able to retrieve info, update it etc, just the basics.

    From what I understand, you want to send a value from C2 to your database, to update it, presumably that value has already been collected from the database earlier?

    So (you probably know most of this), if you already have your value in C2 and want to send it to your database, you need to POST (not update), I think. Actually I'll be honest, I haven't even looked at the method section yet. All I do in your situation is, I send the information in the data, using the default method, then collect it in my PHP file and do the MYSql work in PHP. It's fairly simple, just takes a bit of getting used to (PHP isn't very complicated anyway, just taking me a while to get the hang of the syntax).

  • I'm in a similar stage to you, so I may be able to help. I've been working away at communicating with a database for the past week, so I'm still in the early stages, but I'm able to retrieve info, update it etc, just the basics.

    From what I understand, you want to send a value from C2 to your database, to update it, presumably that value has already been collected from the database earlier?

    So (you probably know most of this), if you already have your value in C2 and want to send it to your database, you need to POST (not update), I think. Actually I'll be honest, I haven't even looked at the method section yet. All I do in your situation is, I send the information in the data, using the default method, then collect it in my PHP file and do the MYSql work in PHP. It's fairly simple, just takes a bit of getting used to (PHP isn't very complicated anyway, just taking me a while to get the hang of the syntax).

    thats the bit i am stuck on , how do i send which Id in the base I want to post/update

  • Does your table have an ID column? If it does, then when you retrieve the data, retrieve the ID too and send it to C2, and store it with your object/array/whatever. Then when you send it back to be updated, send back the ID too, so that you know which row you're updating.

    The MySql for updating goes something along the lines of 'UPDATE MyTable SET Age = Value WHERE ID = MyID'.

  • I think i allmost have it , thanks for bearing with me farflamex ,

    I now have the Ajax reading

    "http://fish.net/fishnetFolder/PlussONEupdate.php?ID=" & ID.Text

    and the .php file reads

    $sql = mysql_query("UPDATE `$db`.`myTable` (`hits`) VALUES ('+1') WHERE ID=('$ID') ;");

    I think I have to many ; in there. and i am thinking VALUES should be something like VALUES =('hits =hits+1')

  • I have now go to the part where i get the return "Congrats Your Data was saved",

    HOWEVER when I go an look into the base nothing at all has changed. I did once get the error "Your data was not passed" and I sorted that so there must be data being passed and the right data,

    The .Php line Now reads

    -------------------------------------------------------------------------------------------------

    $sql = mysql_query("UPDATE MyTable SET hits = hits WHERE ID = ID ");

    -------------------------------------------------------------------------------------------------

    And the C2 Ajax Post box reads

    ---------------------------------------------------------------------------------------------

    "http://mywebsite/update.php?ID=" & ID.Text & "&hits=" & hits.Text

    ---------------------------------------------------------------------------------------------

    I decided to add the one to the hits on a different button , so I am thinking dose the Ajax take the number from the text box when asked or from the array that it used to fill the text box in the first place , in which case I would need to change where the hit is recorded and not any of the lines above,

  • Changed the mysql to read

    ----------------------------------------------------

    $sql = mysql_query("UPDATE bogger SET hits = hits+1 WHERE ID =$ID ");

    WORKS to add 1

    -----------------------------------------------------

    $sql = mysql_query("UPDATE bogger SET hits =$hits WHERE ID =$ID ");

    WORKS to add whats in the box

    ----------------------------------------------------

    C2 Ajax POST reads

    "http://mywebsit/update.php?ID=" &ID.Text & "&hits=" & hits

    =====================================================================

  • Which part isn't working? How are you retrieving the C2 post, are you using isset? I always post my data in the 'data' part of the AJAX box, and then retrieve it in PHP with something like $data = file_get_contents("php://input"); which will put the C2 'data' box in a variable called '$data'. Then I split it to retrieve what I need.

    As I said, I'm new to this, spent the entire day trying to work out how I manipulate my loaded objects in PHP (I can retrieve them from my database and put them into objects, but I can't work out how to select different objects in PHP).

  • Which part isn't working? How are you retrieving the C2 post, are you using isset? I always post my data in the 'data' part of the AJAX box, and then retrieve it in PHP with something like $data = file_get_contents("php://input"); which will put the C2 'data' box in a variable called '$data'. Then I split it to retrieve what I need.

    As I said, I'm new to this, spent the entire day trying to work out how I manipulate my loaded objects in PHP (I can retrieve them from my database and put them into objects, but I can't work out how to select different objects in PHP).

    I got the bit i was looking for working, , am trying to adapt it now .

    When you say you want php to select different objects would you like to expand ?

  • I've also worked out my problem too

    I'm used to C2, where I create an object and it just remains in memory as an object forever. I think PHP works like other languages, where the object will be garbage-collected unless you still have at least one reference to it. So in my case, I'm creating an array of objects and then looping through the array to find what I need. It's not as easy as C2 or BASIC but it's useful to learn anyway. I might make most of my game in PHP and just the front-end in C2.

  • I've also worked out my problem too

    I'm used to C2, where I create an object and it just remains in memory as an object forever. I think PHP works like other languages, where the object will be garbage-collected unless you still have at least one reference to it. So in my case, I'm creating an array of objects and then looping through the array to find what I need. It's not as easy as C2 or BASIC but it's useful to learn anyway. I might make most of my game in PHP and just the front-end in C2.

    I am just almost quite nearly there started to explore that which an array is,

    can you not just scroll though one in C2 by a +1 function / button

  • In C2 it's very easy to go through your objects, which is probably my favourite thing about C2. Once you've created your object it lasts for the length of your project, unless you destroy it, and to select certain objects it's very easy to 'pick' the ones you want. It's not so straightforward in other languages, including PHP, but it's not really that hard either. It just feels like a step back to old-fashioned languages.

    So for example, if you had 20 objects and you want to select any object which has a certain value, let's say variable A = 1. You just 'pick' them with 'Object A = 1', then anything you do after that only works on those objects. In PHP, you'd need an array of objects and then you need to loop through them, checking each one with 'If A = 1 then'.... it's not really much harder, it just feels a bit more fiddly. There may be a simpler way too but I don't know of one yet.

    I was definitely tempted to send all of my database information to C2, do all the processing in C2, then send it back to PHP to save to the database. But it's an extra step when I could just do the whole thing in PHP. Also, I can automate my PHP program to run my game every hour or so (it's turn based).

  • to farflamex

    can u give me tutorial for insert to database from construct .

    just simple project

    ex :

    i have 2 textbox - Name and email

    i want to insert name and email to my table on my sql database.

    i need help

    thanks sir

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)