How do I insert into a database with AJAX method

Get help using Construct 2

Post » Mon May 11, 2015 3:47 pm

I want to save one variable every 10 sec in the user row.

My php code:
Code: Select all
<?php
   // Variables
   $nombreUsuario = $_GET['nombreUsuario'];
   $passUsuario = $_GET['passUsuario'];
   $distanciaTotal = $_GET['distanciaTotal'];
   
   $con=mysqli_connect("mysql","database","userid","password");
   // Check connection
   if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
   $query =("INSERT INTO usuarios_aep (distanciaTotal) VALUES (".$distanciaTotal.")
               WHERE nombreUsuario like ".$nombreUsuario."
               AND passUsuario like ".$passUsuario."
            ");
   $mysqli->query($query);
   
   mysqli_close($con);
?>


event "every 10 sec"
Code: Select all
"http://fakefake.com/fakefake/php/insertDistanciaTotal.php?nombreUsuario='"&nombreUsuario&"'&passUsuario='"&passUsuario&"'&distanciaTotal='"&distanciaTotal&"'"
B
22
S
3
Posts: 78
Reputation: 1,139

Post » Mon May 11, 2015 6:02 pm

After connecting, and after you have done the INSERT query, with your current approach with using a Ajax GET request, the following should work.


$query =("UPDATE usuarios_aep SET distanciaTotal = '".$distanciaTotal."' WHERE nombreUsuario = '".$_GET['nombreUsuario']."' AND passUsuario = '".$_GET['passUsuario']."' ");

$mysqli->query($query);


I would suggest switching to post methods, otherwise it would become very easy to cheat :D
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Thu May 14, 2015 11:30 am

lennaert wrote:After connecting, and after you have done the INSERT query, with your current approach with using a Ajax GET request, the following should work.


$query =("UPDATE usuarios_aep SET distanciaTotal = '".$distanciaTotal."' WHERE nombreUsuario = '".$_GET['nombreUsuario']."' AND passUsuario = '".$_GET['passUsuario']."' ");

$mysqli->query($query);


I would suggest switching to post methods, otherwise it would become very easy to cheat :D


I come back with more problems!! :lol:

Actually I have this php in my server, I tested it from chrome and it works, makes the UPDATE correctly.

Code: Select all
<?php
   // Variables
   $nombreUsuario = "XXuser";
   $passUsuario = "XXpass";
   $distanciaTotal = $_GET['distanciaTotal'];
   
   $con=mysqli_connect("blablabla.com","blebleble","bliblibli","blublublu");
   // Check connection
   if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
   $qz =("UPDATE usuarios_aep SET distanciaTotal = '".$distanciaTotal."'
            WHERE nombreUsuario = '".$nombreUsuario."'
            AND passUsuario = '".$passUsuario."'
         ");
         
   if (mysqli_query($con, $qz)) {
      echo "Record updated successfully";
   } else {
      echo "Error updating record: " . mysqli_error($con);
   }
   mysqli_close($con);
?>


The problem appears when c2 has send (POST or GET method) the data to php, it seems that I am not sending data at all... so I think that the problem is in the next images (the AJAX connection) but I don't know how to solve it.
Image
Image
B
22
S
3
Posts: 78
Reputation: 1,139

Post » Thu May 14, 2015 11:48 am

yeah remove the single qoutes in the data entry.

Resulting in : "distanceTotal="&distanciaTotal

To add more

"distanceTotal="&distanciaTotal&"&playerID="&SomeConstruct2Var


Result would look like

"distanceTotal=100&playerID=10"
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Thu May 14, 2015 11:55 pm

lennaert wrote:yeah remove the single qoutes in the data entry.

Resulting in : "distanceTotal="&distanciaTotal

To add more

"distanceTotal="&distanciaTotal&"&playerID="&SomeConstruct2Var


Result would look like

"distanceTotal=100&playerID=10"


Hello again, first of all so many thanks you !!

It seems I can connect properly to send data to mysql db, my problem now is that I can't acces to data from the db!! My problem now is in the loging.

My construct call is this:
Code: Select all
"http://myweb.com/myproject/php/login.php?nombreUsuario=" & nombreUsuario & "&passUsuario=" & passUsuario


An this is my php wich is actually working properly (i tested it):

Code: Select all
<?php
   // Variables
   $nombreUsuario = $_GET['nombreUsuario'];
   $passUsuario = $_GET['passUsuario'];
   
   $con=mysqli_connect("blebleble","bliblibli","blobloblo","blublublu");
   // Check connection
   if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
   $qz = "SELECT idUsuario FROM usuarios_aep where nombreUsuario='".$nombreUsuario."' and passUsuario='".$passUsuario."'" ;
   $qz = str_replace("\'","",$qz);
   $result = mysqli_query($con,$qz);
   
   if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()){
         echo $row["idUsuario"];
      }
   } else {
      echo "Error al acceder a los datos: " . mysqli_error($con);
   }
   mysqli_close($con);
?>


I try to get as data "idUsuario" and I save this id in a variable, but when I run my project in debug mode the content of that variable is NaN (not a Number). I'm not sure what is happening, any idea??

thanks.
B
22
S
3
Posts: 78
Reputation: 1,139

Post » Thu May 14, 2015 11:56 pm

lennaert wrote:yeah remove the single qoutes in the data entry.

Resulting in : "distanceTotal="&distanciaTotal

To add more

"distanceTotal="&distanciaTotal&"&playerID="&SomeConstruct2Var


Result would look like

"distanceTotal=100&playerID=10"


Hello again, first of all so many thanks you !!

It seems I can connect properly to send data to mysql db, my problem now is that I can't acces to data from the db!! My problem now is in the loging.

My construct call is this:
Code: Select all
"http://myweb.com/myproject/php/login.php?nombreUsuario=" & nombreUsuario & "&passUsuario=" & passUsuario


An this is my php wich is actually working properly (i tested it):

Code: Select all
<?php
   // Variables
   $nombreUsuario = $_GET['nombreUsuario'];
   $passUsuario = $_GET['passUsuario'];
   
   $con=mysqli_connect("blebleble","bliblibli","blobloblo","blublublu");
   // Check connection
   if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
   $qz = "SELECT idUsuario FROM usuarios_aep where nombreUsuario='".$nombreUsuario."' and passUsuario='".$passUsuario."'" ;
   $qz = str_replace("\'","",$qz);
   $result = mysqli_query($con,$qz);
   
   if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()){
         echo $row["idUsuario"];
      }
   } else {
      echo "Error al acceder a los datos: " . mysqli_error($con);
   }
   mysqli_close($con);
?>


I try to get as data "idUsuario" and I save this id in a variable, but when I run my project in debug mode the content of that variable is NaN (not a Number). I'm not sure what is happening, any idea??

thanks.
B
22
S
3
Posts: 78
Reputation: 1,139

Post » Fri May 15, 2015 8:08 am

NaN means Not a Number. Save the output in a text variable and check what it says. Also I would suggest to use PDO rather than mysqli.
B
12
S
3
Posts: 211
Reputation: 1,477

Post » Fri May 15, 2015 10:46 am

Fireche wrote:NaN means Not a Number. Save the output in a text variable and check what it says. Also I would suggest to use PDO rather than mysqli.


If I change the variable to text the return is "" too, I think the problem is in the request but I can't find the error.
B
22
S
3
Posts: 78
Reputation: 1,139

Post » Fri May 15, 2015 12:15 pm

If you still use GET methods you should be able to use that link:

"http://myweb.com/myproject/php/login.php?nombreUsuario=" & nombreUsuario & "&passUsuario=" & passUsuario

as

http://myweb.com/myproject/php/login.ph ... sUsuario=1

in the browser and see if you get the results you are expecting. (just replace 10 and 1 with actual numbers)

You could perhaps enable error reporting in PHP, or add a : echo mysql_error(); below the query.



But, if you switched from GET methods to POST methods, you should update your PHP to reflect that.

ie

the $_GET['meh']

should become

$_POST['meh']
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Fri May 15, 2015 6:13 pm

Make sure you use PreparedStatements, please!

Don't end up like this: Image
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests