-1

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!

Jonas
  • 85
  • 11
  • because your php code get executed before javascript codes. As its server side executon. – Saikat Hajra Mar 07 '18 at 18:46
  • I have the PHP inside a function, that only runs when a condition is true. Doesn't that help? – Jonas Mar 07 '18 at 18:48
  • Are you using AJAX? – Jerome Wiley Segovia Mar 07 '18 at 18:49
  • Index should be AJAX, yes. – Jonas Mar 07 '18 at 18:53
  • Most likely you're now using an updated PHP installation rather than an incredibly old one and therefore don't have the `mysql_` functions. [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – h2ooooooo Mar 07 '18 at 18:59
  • I don't believe this to be the case, since I have ran almost the same code, on the same server before. And it worked, Must be something i have done wrong. – Jonas Mar 07 '18 at 19:02
  • @Jonas Do you own this server and control the underlying software? If you use a webhost and they've upgraded to PHP 7 (like pretty much every host) then these functions will not exist. What does your error log say? Have you tried enabling php errors to see if you're getting an "unknown function mysql_connect"? – h2ooooooo Mar 07 '18 at 19:20
  • I am currently using PHP 5.5 if I don't remember wrong, and there seem to be no problem with the connection. Look at my update, – Jonas Mar 07 '18 at 19:23

2 Answers2

1

The issue was that session_start(); was missing. When a session is null, everything else returns null too, therefore all the problems with everything printing out zeros and other weird things.

Special thanks to @virhonestum For helping me find this issue!

<?php
session_start();
require_once('db.php');
require('var.php');
$email = $_SESSION["email"];
$id = $rid['id'];
$score = $_POST['uscore'];
$sql = "UPDATE users SET score='$score' WHERE id='$id'";
$retval = mysql_query( $sql, $link );
var_dump($id);
   mysql_close($link);
?>
Jonas
  • 85
  • 11
0

Edit: As it turns out the issue was a missing session_start() when accessing the session variables, which resulted in empty variables and wrong SQL-requests. See comments to this answer for more info.


Original answer:

You do not pass the uid POST variable to your PHP script, only uscore. So in your SQL, WHERE id='' is probably false for all rows, no nothing will be updated.

So you'll need to change it to

$.post('scoreadder.php', {uid:id, uscore:score}, function(data){
    ...
});

or get the id from elsewhere.

If you have a different ID from somewhere else, you have to use that one! As long as you have

$id = $_POST['uid'];
...
$sql = "... WHERE id='$id'";

this will not work! Use $sql = "... WHERE id='$rid'"; or whichever variable contains your id.


Additional Notes:

In your snippet $retval is being ignored and the script will return success no matter if it really was successfull or not.

mysql_ is deprecated, and should no longer be used. Use mysqli instead.

petroni
  • 766
  • 1
  • 13
  • 29
  • atm, I'm getting the id from somewhere else. I have tried to change numbers but it doesn't seem to make a difference. I will update my question to make it clearer where i get my ID from. – Jonas Mar 07 '18 at 18:52
  • @Jonas Your edid did not change anything about my answer, but i've edited it to clarify it a little. – petroni Mar 07 '18 at 19:03
  • I changed it, now it should be updated. I should currently be getting my ID from $rid, which is declared in var.php. Other then my ID issue, does it look okay? Cause if it's just the ID I can change it to numbers and try to run the script to see if it helped, What do you think? – Jonas Mar 07 '18 at 19:10
  • @Jonas, okay your newest edit did change things. Maybe it doesn't work, because the database connection is closed with `mysql_close($link);` in your var.php and is not reopened before the query (at least in this snippet). – petroni Mar 07 '18 at 19:11
  • I removed it but it did not help. But! I also tried one more thing. I changed the id='$id' to id='2', and so with the score, and i was able to update the database. – Jonas Mar 07 '18 at 19:16
  • There seem to be something wrong with either getting the ID or getting the score.. – Jonas Mar 07 '18 at 19:16
  • @Jonas Okay, then check what `$id` and/or `$rid` actually contain. The easiest way would beto just `var_dump()` them – petroni Mar 07 '18 at 19:24
  • If it wasn't the devil himself. ID contains !NULL. I belive my way of getting the ID is not working, in the scoreadder.php script. – Jonas Mar 07 '18 at 19:27
  • @Jonas try changing `mysql_fetch_array(...)` to `mysql_fetch_array(..., MYSQL_ASSOC)` to make sure you get an associative array – petroni Mar 07 '18 at 19:31
  • I changed it, but no changes were made. But i tried something else. I added the function to get the ID to my scoreadder.php script and now i get this - !string(2) "id". see update. – Jonas Mar 07 '18 at 19:36
  • @Jonas hmm, that's weird, have you checked what `$rid` contains? – petroni Mar 07 '18 at 19:41
  • And consider adding `LIMIT 1` to your SQL script if you only need one roe, that will improve performance since it will stop searching after the first hit. – petroni Mar 07 '18 at 19:42
  • I have and $rid contains - !bool(false) – Jonas Mar 07 '18 at 19:42
  • `mysql_fetch_array` returns false when there are no more rows, or in this case not any. Could it be, that there is no row in table `users` where the email is `$email`? Maybe the session bariable is not set – petroni Mar 07 '18 at 19:48
  • Wait, I'm a little confused, `$id` contains the string literal "id" after you assign it with `$id = $rid['id'];`? – petroni Mar 07 '18 at 19:56
  • haha sorry, this is what it contains - !NULL. When i var_dump($id); – Jonas Mar 07 '18 at 19:58
  • okay, that seems about right when `$rid` is just `false`. Please insert an `echo mysql_error($link);` directly after `$ids = mysql_query("SELECT id FROM users WHERE email = '$email'");` to check if any sql errors occured. – petroni Mar 07 '18 at 20:04
  • No errors occur, I believe its the part of how I try to get the id that fails from score adder. – Jonas Mar 07 '18 at 20:08
  • Okay, then check whether the query actually returned data with `mysql_num_rows($ids)`, preferably before the fetch. – petroni Mar 07 '18 at 20:11
  • interesting result : 0 rows. $num_rows = mysql_num_rows($ids); echo "$num_rows"; – Jonas Mar 07 '18 at 20:23
  • I have the exact same code but on another side, where it returns a 1, even weirder. There is something i have forgotten to add here. – Jonas Mar 07 '18 at 20:26
  • Session doesnt seem to work here! Since the $email also returns a null. – Jonas Mar 07 '18 at 20:28
  • Okay, then you have the issue. The session probably expired, a new one has not been properly started, so the session variables are empty. – petroni Mar 07 '18 at 20:31
  • don't know about expired, would more call it none existing on this page. Weird that it works on another page with the same code. – Jonas Mar 07 '18 at 20:38
  • I added session_start(), and now it seems to be printing out things. – Jonas Mar 07 '18 at 20:41
  • Oh, yes, you have to call `session_start()` every time you want to use session variables and it has not been called on that page before – petroni Mar 07 '18 at 20:45
  • exactly, thanks, man! really appreciate you spending your time helping me! update your question and i will set it as the answer! :) – Jonas Mar 07 '18 at 20:47
  • No problem! :) It always turns out to be something completely different than anticipated – petroni Mar 07 '18 at 20:52