February 25, 2012

PHP Update page on POST

Question by Sami Dz Hamida

I have this little problem that when I POST something the page only updates after you refresh the page or change the page.

Example:

Money:
$1,000 -> (echo $money;)

if($_POST['add_money']){
    mysql_query("UPDATE users SET money=money+ '1000' WHERE username = '".$name."'");
    echo "You added $1,000 to your money";
}

“Money” will still say $1,000 unless I change page or refresh.

I’m sure it something so small but I just cant seem to find out how to do it.

Thank you in advanced.

Answer by Starx

Your code should work the way it is. Try this to confirm any errors

if($_POST['add_money']){
    $query = "UPDATE users SET money=money+ '1000' WHERE username = '".$name."'";
    $result = mysql_query($query) or die(mysql_error());
    if($result) {
       echo "You added $1,000 to your money";
    }
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!