1

When wanting to update field completed_status by one when this form is submitted, it is staying at 0. The query $submission is the update for the users tables. This is the code I have been using:

$user_id = intval($_SESSION['user_id']);
if (isset($_POST['doPersonal'])) {

    if (empty($err)) {

        $Sex = mysql_real_escape_string($_POST['Sex']);
        $Second_Nationality = mysql_real_escape_string($_POST['Second_Nationality']);
        $Birth_Place = mysql_real_escape_string($_POST['Birth_Place']);
        $Birth_Country = mysql_real_escape_string($_POST['Birth_Country']);
        $children = mysql_real_escape_string($_POST['children']);

        $the_query =   "INSERT INTO personal (user_id, Sex, Second_Nationality, 
Birth_Place, Birth_Country, children)
VALUES ('$user_id', '$Sex', '$Second_Nationality','$Birth_Place', '$Birth_Country',
'$children')
ON DUPLICATE KEY UPDATE Sex=VALUES(Sex), Second_Nationality=VALUES(Second_Nationality),
Birth_Place=VALUES(Birth_Place) , children=VALUES(children)";



        // query is ok?
        if (mysql_query($the_query, $link) ){

            // redirect to user profile
            header('Location: myaccount.php?id=' . $user_id);

            $submission= "update users set completed_status=completed_status+1 where 
id='$_SESSION[user_id]'";
        }
    }
}

}

user1296762
  • 512
  • 2
  • 7
  • 21

4 Answers4

1

From the above code we can see that you are redirecting the page before the update query so please modify it as,

if (mysql_query($the_query, $link) ){

           $submission= "update users set completed_status=completed_status+1 where 
id='$_SESSION[user_id]'";

           mysql_query($submission,$link);  // to execute the update query              

          // redirect to user profile
          header('Location: myaccount.php?id=' . $user_id);


}
Prabhuram
  • 1,268
  • 9
  • 15
  • 1
    In fact, code after `header` will still run. It just instructs PHP to send out a specific header, that is why you normally write `exit` after your redirects. [More info](http://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php). – kapa Apr 30 '12 at 09:23
  • Don't trust an unsanitised session, consider using prepared statements and escaping anything that could come from a browser. – Tom Hallam Apr 30 '12 at 09:30
1

You haven't call the mysql_query:

$submission= "update users set completed_status=completed_status+1 where 
id='$_SESSION[user_id]'";
mysql_query($submission, $link);
Gavriel
  • 18,880
  • 12
  • 68
  • 105
1

I guess you forgot to run the query.

    if (mysql_query($the_query, $link)){

        $submission= "update users set completed_status=completed_status+1 where id='$_SESSION[user_id]'";
        mysql_query($submission, $link);

        // redirect to user profile
        header('Location: myaccount.php?id=' . $user_id);

    }

Please note that the redirect does not affect anything (even though others told you here). I just put it after the query because of a habit. Code after the redirect will still run, you just don't see the output in the browser. If you want to stop execution after the redirect, you can use exit.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
0

Try this as your last if block:

if (mysql_query($the_query, $link)) {
  $query = 'update users set completed_status=completed_status+1 where id = ' . $_SESSION[user_id];
  mysql_query($query, $link);

  // redirect to user profile
  header('Location: myaccount.php?id=' . $user_id);
}

However, it's rather ambigous that you're using $user_id as well as $_SESSION['user_id']. Are these two different user_id's?

janosrusiczki
  • 1,920
  • 2
  • 20
  • 41