How do I insert into a database with AJAX method

0 favourites
  • 11 posts
From the Asset Store
Sync animations, animation frames, mirror state, sounds and anything else using these methods.
  • I want to save one variable every 10 sec in the user row.

    My php code:

    <?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);
    ?>[/code:tb2wf9on]
    
    event "every 10 sec"
    [code:tb2wf9on]"http://fakefake.com/fakefake/php/insertDistanciaTotal.php?nombreUsuario='"&nombreUsuario&"'&passUsuario='"&passUsuario&"'&distanciaTotal='"&distanciaTotal&"'"[/code:tb2wf9on]
  • After connecting, and after you have done the INSERT query, with your current approach with using a Ajax GET request, the following should work.

    [quote:33zcixpd]$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

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

    [quote:2jh1h120]$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

    I come back with more problems!!

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

    <?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);
    ?>[/code:2jh1h120]
    
    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.
    [img="http://www.therealgeekgirl.com/server1.jpg"]
    [img="http://www.therealgeekgirl.com/server2.jpg"]
  • 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"

  • 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:

    "http://myweb.com/myproject/php/login.php?nombreUsuario=" & nombreUsuario & "&passUsuario=" & passUsuario[/code:3tqmklo3]
    
    An this is my php wich is actually working properly (i tested it):
    
    [code:3tqmklo3]<?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);
    ?>[/code:3tqmklo3]
    
    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.
  • 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:

    "http://myweb.com/myproject/php/login.php?nombreUsuario=" & nombreUsuario & "&passUsuario=" & passUsuario[/code:3dvx20px]
    
    An this is my php wich is actually working properly (i tested it):
    
    [code:3dvx20px]<?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);
    ?>[/code:3dvx20px]
    
    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.
  • 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.

  • 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.

  • 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']

  • Make sure you use PreparedStatements, please!

    Don't end up like this:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Finally i found the error. The php code need "header("Access-Control-Allow-Origin: *");" in their header

    Rookie mistake , thanks for the help.

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