更新数据库的Ajax不起作用

时间:2016-04-18 19:46:53

标签: ajax

我有一个代码应该从其他应用程序接收一个带有点变量的ajax调用来更新数据库上的这个变量,但是出了点问题。

这是我的代码:

变量应该在点上起作用。

我已将其插入查询但似乎有错误,因为它没有更新数据库。

<?php
header('Access-Control-Allow-Origin: *');
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
	header("Location: index.php");
}



if(isset($_GET['points'])){

     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
    
     $points = strip_tags(mysql_real_escape_string($_GET['points']));
     //$sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
     $sql = mysql_query("UPDATE `users` SET user_points = '$points' WHERE user_id = " . $_SESSION['user']);
	 
	 
}
     if($sql){
     
          //The query returned true - now do whatever you like here.
          echo 'Your score was saved. Congrats!';
          
     }else{
     
          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'There was a problem saving your score. Please try again later.';
          
     }
     
}else{
     echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}

mysql_close();//Close off the MySQL connection to save resources.
?>

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:-1)

我终于解决了问题...

<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors',1);
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
	header("Location: index.php");
}



if(isset($_GET['points'])){

     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
    
     $points = strip_tags(mysql_real_escape_string($_GET['points']));
    //$sql = mysql_query("INSERT INTO `publiadd_loginsx`.`users` (`points`) VALUES ('points');");
    $sql = mysql_query("UPDATE `users` SET user_points = user_points +'$points' WHERE user_id = " . $_SESSION['user']);
	
	 
	 
     if($sql){
     
          //The query returned true - now do whatever you like here.
          echo 'Your Points was saved. Congrats!';
          
     }else{
     
          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'There was a problem saving your points. Please try again later.';
          
     }
     
}else{
     echo 'Your points wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}

// close MySQL connection 
mysql_close();
?>
<html>
<head>
</head>
<body>
<body bgcolor="#ffffff">
</body>
</html>

现在这项工作确实有效