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

2
  • 1
    You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Nov 15, 2012 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? Commented Nov 15, 2012 at 17:39

2 Answers 2

1

If you copy/pasted this from your actual code,

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

should be

'http://localhost/basic_structure/game.php'
Sign up to request clarification or add additional context in comments.

1 Comment

Also the variable health which i use to also post to the html document doesnt display the integer anymore? thanks
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.

9 Comments

Thanks but this still didnt work? the variable contents have also stop being visible in my html doc?
What do you mean by that? Does not display variable contents?
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
OK, you have to first check that you are sending the health variable to php correctly. So, in your php, just do the following: <?php echo $_POST["health"]; ?> Now if this works, we know that the value is being sent correctly.
Ok i tried that, but no luck so i guess thats where the problem lies?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.