I am trying to insert my Javascript variable "score" into a Mysql database, but it doesn't seem to work. Down below is the Index.php code
var score = 0;
function post() {
$.post('scoreadder.php',{uscore:score},
function(data){
console.log(score +"hello");
});
}
I saw a video and it explained that I could send the Score variable like this, and in a test environment it kinda worked, I was able to update my database values with it, but now it doesn't seem to work at all, I believe that I'm missing something. I have checked and I can see that the function has run since from the console since I logged it.
Scoreadder.php script
<?php
require_once('db.php');
require('var.php');
$id = $rid['id'];
$score = $_POST['uscore'];
$sql = "UPDATE users SET score='$score' WHERE id='$id'";
$retval = mysql_query( $sql, $link );
echo "SUCCESSFUL!";
mysql_close($link);
?>
I'm not really sure I'm doing everything right, but since it kind of worked before I'm thinking that I'm missing something. Is there any other way to pass a javascript variable to PHP? I've also seen that you could send javascript values through a form, but I'm not sure it would make sense to do it here.
Where I get my ID from var.php
require_once('db.php');
$email = $_SESSION["email"];
$ids = mysql_query("SELECT id FROM users WHERE email = '$email'");
$iscore = mysql_query("SELECT score FROM users WHERE email = '$email'");
$rid = mysql_fetch_array($ids);
$ris = mysql_fetch_array($iscore);
$sql = "SELECT id FROM users WHERE email='$email'";
mysql_select_db('matteraknaren');
$retval = mysql_query( $sql, $link );
mysql_close($link);
This ID part should work.
Feel free to ask for more information
//EDIT There seem to be something wrong with getting the ID, because if i update the code to this -
require_once('db.php');
require('var.php');
$id = "2";
//$id = $rid['id'];
$score = $_POST['uscore'];
$sql = "UPDATE users SET score='4321' WHERE id='$id'";
$retval = mysql_query( $sql, $link );
echo "SUCCESSFUL!";
mysql_close($link);
It does actually work. Problem is that the id is changing from user to user, therefore I need it to be changing.
// UPDATE
require_once('db.php');
require('var.php');
$ids = mysql_query("SELECT id FROM users WHERE email = '$email'");
$iscore = mysql_query("SELECT score FROM users WHERE email = '$email'");
$rid = mysql_fetch_array($ids, MYSQL_ASSOC);
//$id = "2";
//$id = $rid['id'];
$score = $_POST['uscore'];
$sql = "UPDATE users SET score='$score' WHERE id='$id'";
$retval = mysql_query( $sql, $link );
echo "SUCCESSFUL!";
var_dump('id');
mysql_close($link);
UPDATE 2 The problem now seems to be when im trying to get the ID from " $id = $rid['id']; " as it returns !NULL when dump();
UPDATE 3 something with the SESSION seems to be the problem as not even the $email will work.
UPDATE 4 The problem occurred because i was missing session_start(); Hope this helps someone with the same issue as i had!