更新字段为1

时间:2012-04-30 09:04:23

标签: php mysql

当提交此表单时想要将字段completed_status更新为1时,它将保持为0.查询$ submission是users表的更新。 这是我一直在使用的代码:

$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]'";
        }
    }
}

}

4 个答案:

答案 0 :(得分:1)

从上面的代码我们可以看到redirecting之前的页面是update 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);  // to execute the update query              

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


}

答案 1 :(得分:1)

你没有调用mysql_query:

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

答案 2 :(得分:1)

我猜你忘了运行查询了。

    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);

    }

请注意the redirect does not affect anything(即使其他人在这里告诉你)。我只是因为习惯而把它放在查询之后。重定向后的代码仍会运行,您只是在浏览器中看不到输出。如果要在重定向后停止执行,可以使用exit

答案 3 :(得分:0)

将此作为您的最后一个if块:

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);
}

但是,您使用$user_id以及$_SESSION['user_id']时相当模糊。这两个不同的用户ID是什么?