How do I UPDATE Ajax Score in Mysql

Get help using Construct 2

Post » Wed Jul 02, 2014 9:49 pm

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

anyone got any pointers please ?
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Thu Jul 03, 2014 4:00 am

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

https://www.scirra.com/tutorials/346/on ... -php-mysql
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 11:35 am

farflamex wrote: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.
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Thu Jul 03, 2014 11:42 am

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).
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 3:09 pm

farflamex wrote: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
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Thu Jul 03, 2014 5:03 pm

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'.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Fri Jul 04, 2014 8:53 am

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')
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Fri Jul 04, 2014 10:32 pm

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,
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Sat Jul 05, 2014 8:24 pm

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

=====================================================================
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Sat Jul 05, 2014 10:49 pm

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).
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 29 guests