-1

I have token system where every time you submit the form it take -1 from your total tokens.

I'm using common.php

$ses_sql="Select * from users where name='testuser' and id='1'";
$result = $conn->query($ses_sql);
$row = $result->fetch_assoc();
$token = $row['tokens'];

So beside the menu bar it views your total Credit so you can see it, no matter which page you are in.

Now come the form part. Page name adduser.php

<? 
// Included on all pages.
include 'include/common.php';
echo $token; 
?>
<form method="post" action="adduser.php?save=all">
<input type="text" name="username">
</form>
<?
$kalk = $token - 1;
if (isset($_GET['save']) && $_GET['save'] == 'all'{
$sqllog = "UPDATE `reseller` set `tokens`='$kalk' where id='1'";
$check1 = mysqli_query($conn,$sqllog);
}
?>

So all this working, perfect. BUT remember the callout on top of the page $token this is not refreshing after the submit, but going into other page OR refreshing the browser the value gets updated. Help is appreciated.

Sami H
  • 51
  • 1
  • 8
  • 2
    *"So all this working, perfect."* - Not with your `$sqllog = '...";` query it won't. Unless that was just a bad paste. Stack even shows you that there's a syntax error. http://php.net/manual/en/mysqli.error.php – Funk Forty Niner Nov 19 '15 at 15:07
  • What do you mean by "not refreshing after submit"? What is the exact sequence of steps that you're observing here? Aside from the aforementioned syntax errors, what exactly is the problem? – David Nov 19 '15 at 15:16
  • @David well that other guy took a chance and posted an answer below. OP probably isn't keen on comments, but "answers". I won't stand around much longer, like someone's personal butler. – Funk Forty Niner Nov 19 '15 at 15:21
  • @Fred-ii- Sorry for being late, you are right the it is missing query, but if i said its working as it is, isn't it understandable instead of giving me -1 on my question. – Sami H Nov 19 '15 at 15:27
  • if that works, update your question with the correct syntax. Someone gave you an answer based on that syntax error. – Funk Forty Niner Nov 19 '15 at 15:28
  • 1
    @SamiH: (1) Voting is anonymous. (2) Arguing with votes is fruitless. (3) No, it's not good enough to just take your word for it that something is working when it clearly would not be working. This indicates that the code posted in the question isn't the code you're actually using. Which makes the question pretty meaningless. – David Nov 19 '15 at 15:29
  • @SamiH quick question: what happens when you "do" refresh the page, does it update your table and show it? and where is `$loginid` defined? – Funk Forty Niner Nov 19 '15 at 15:34
  • @Fred-ii- Done and done. – Sami H Nov 19 '15 at 15:35
  • @SamiH I will ask you again: what happens when you "do" refresh the page, does it update your table and show it? – Funk Forty Niner Nov 19 '15 at 15:41

1 Answers1

1

Sidenote edit: This answer is based on OP's original post https://stackoverflow.com/revisions/33807477/1 and 2nd revision https://stackoverflow.com/revisions/33807477/2 and did an edit after this was posted.


Ok, I'm submitting this since I'm not getting any other feedback from the OP in comments and to point out the syntax error.

This line of code:

$sqllog = 'UPDATE `reseller` set `tokens`='$kalk' where id='1'";
          ^ single                                            ^ double

You are opening your statement with a single quote, then closing it with a double quote.

Error checking would have thrown you a syntax error message.

and needs to read as:

$sqllog = "UPDATE `reseller` set `tokens`='$kalk' where id='1'";
// or where id=1 but doubt that would make a difference.

and having done

$check1 = mysqli_query($conn,$sqllog) or die(mysqli_error($conn));

to signal the syntax error.

"$token this is not refreshing after the submit"

That's because you need to refresh the page yourself or use a redirection method.

Have a look at this Q&A on Stack:

You're also open to an SQL injection. Use a prepared statement.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Additional debugging tools:

and echoing your query.

  • Also make sure you've chosen the right database/table/column(s)
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • If this doesn't solve this, I'll find the nearest bridge. – Funk Forty Niner Nov 19 '15 at 15:49
  • I have double qoutes but wrote it wrong in here, sorry for that. Using header('location: adduser.php'); Gave me Warning Cannot modify header information - headers already sent – Sami H Nov 19 '15 at 15:54
  • @SamiH I believe I have given you enough information in my answer to find out what's not working in your code, including to redirect after the query in order to see the changes, IF your query was successful. There isn't anything I can say or do that will be of further help. – Funk Forty Niner Nov 19 '15 at 15:57
  • My code is working, it is updating the database correctly, refreshing the page will get my $token updated. But i want to get the new value of $token after submitting. – Sami H Nov 19 '15 at 15:59
  • @SamiH I don't see a submit button and fear that you haven't shown us all relevant code. The process is simple, click submit with enclosed query set inside a conditional statement if everything is set/not empty. – Funk Forty Niner Nov 19 '15 at 16:00