0

I cant seem to work out how to simply add a javascript variable into a mySQL table!

I have a html code which is just making a canvas for my game. I then have a javascript file doing all the game proccess and here is the code i am using to send the javascript variable to a php file:

 var uiStats = $("#gameStats");
var uiHealth = $(".gameHealth");

var health = 10;

$.post('http:/localhost/basic_structure/game.php', { "health" : health});

Then here is the php file to insert the data into the database:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die
('Error connecting to mysql');

$dbname = 'game';
mysql_select_db($dbname, $conn);

$health = $_POST['health'];

mysql_query("INSERT INTO game_table (health)
VALUES ('$health')");

mysql_close($con);

I simply just want to save the health of the player in my game for later uses.

Any help is appreciated

Thanks

Tom Burman
  • 947
  • 3
  • 15
  • 37
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 15 '12 at 17:29
  • thanks for letting me know, this is just on a test environment on my local pc at the moment so this shouldnt cause much problems yet? – Tom Burman Nov 15 '12 at 17:39

2 Answers2

1

If you copy/pasted this from your actual code,

'http:/localhost/basic_structure/game.php'

should be

'http://localhost/basic_structure/game.php'
Casey Foster
  • 5,982
  • 2
  • 27
  • 27
  • Also the variable health which i use to also post to the html document doesnt display the integer anymore? thanks – Tom Burman Nov 15 '12 at 17:37
0

Your URL is incorrect in the post call

Also, use PDO or mysqli_ instead of mysql_ because mysql_ is deprecated.

A third suggestion is you should have a callback in your post (from php to javascript) to allow you to check that the process succeeded/failed and inform the user accordingly.

Greeso
  • 7,544
  • 9
  • 51
  • 77
  • Thanks but this still didnt work? the variable contents have also stop being visible in my html doc? – Tom Burman Nov 15 '12 at 17:38
  • What do you mean by that? Does not display variable contents? – Greeso Nov 15 '12 at 17:46
  • Ok ill try to explain better. I have a div in my html which is empty call uiHealth. The javascript file uses that div to show the players health elsewhere in the code. When i corrected my mistake you pointed out, the javascript failed to post the variable health into my html div, as well as not insert it into the database – Tom Burman Nov 15 '12 at 17:49
  • OK, you have to first check that you are sending the health variable to php correctly. So, in your php, just do the following: `` Now if this works, we know that the value is being sent correctly. – Greeso Nov 15 '12 at 17:52
  • Ok i tried that, but no luck so i guess thats where the problem lies? – Tom Burman Nov 15 '12 at 17:57
  • Yes, you are not making the proper call. The way you are making your post, it has a full path, that is not good. What if for some reason your domain name changes? You have to update your code. Use relative paths in your post call, this might solve your problem. Another check is to make sure that you are actually sending a value for health (ie, you are not sending an empty string) – Greeso Nov 15 '12 at 18:10
  • well as you can see im using $.post() but thats actually underlined as an unresolved function or method, Could this be a reason? – Tom Burman Nov 15 '12 at 18:15
  • This means your code is incorrect. Read the jQuery documentation for post here http://api.jquery.com/jQuery.post/ (did you mport a jQuery library?) – Greeso Nov 15 '12 at 18:19
  • What do you mean? jQuery Post is a form of Ajax call. But before you are able to use jQuery you should load it. Also, it looks like you are not reading documentation! – Greeso Nov 15 '12 at 18:31