First, you need to check the input, and your code will be available for sql injection . Check How to prevent SQL injection in PHP?
Please do not use the mysql_* functions in the new code . They are no longer supported and are officially outdated . Read more about prepared instructions and use PDO or MySQLi
So, keeping in mind, here is a PDO script that does the same thing, and I understand its path longer, but you can use it as a class if necessary, as this is just an example.
<?php // create connection to database $conn = new PDO('mysql:dbname=DATABASE_NAME;host=localhost;port=3306', USERNAME, PASSWORD); // prepare query $pdo = $conn->prepare("UPDATE ss_character SET location = :location WHERE id = :session_id"); // set up parameters $params = ['location' => (int)$_POST['location'], 'session_id' => $_SESSION['id']]; // loop through the paramaters to determine the type foreach ($params as $key => $value) { switch ($value) { case is_int($value): $param = PDO::PARAM_INT; break; case is_bool($value): $param = PDO::PARAM_BOOL; break; case is_null($value): $param = PDO::PARAM_NULL; break; default: $param = PDO::PARAM_STR; break; } // bind paramter to query $pdo->bindValue(":$key", $value, $param); } // execute the query $result = $pdo->execute($params); // echo result for ajax echo ($result) ? true : false;
And you need some jQuery to do ajaxing for you so that the page doesn't reload
<script> function updatePlayerLocation(location) { // ensure location is numeric or stop if !isNaN(location) return false; // update location via ajax $.ajax({ url: 'http://your_url/to/php/script.php', type: 'POST', data: 'location=' + location, success: function(data) { // log result to console for error trapping purposes console.log(data); } }); // stop link from being processed return false; } </script>
HTML of course includes jQuery , the script above and at least one link:
<a href="#" onclick="return updatePlayerLocation(0);">Location name</a><br />
source share