id没有正确改变

时间:2012-03-28 21:55:56

标签: php mysql

如果我使用此表注册用户:

CREATE TABLE IF NOT EXISTS `users` 
  ( 
     `id`              INT(11) NOT NULL AUTO_INCREMENT, 
     `md5_id`          VARCHAR(200) NOT NULL, 
     `full_name`       TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_ci 
     NOT NULL, 
     `user_name`       VARCHAR(10) NOT NULL, 
     `user_email`      VARCHAR(30) NOT NULL, 
     `user_level`      TINYINT(4) NOT NULL DEFAULT '1', 
     `pwd`             VARCHAR(220) NOT NULL, 
     `nationality`     VARCHAR(30) NOT NULL, 
     `department`      VARCHAR(20) NOT NULL, 
     `birthday`        DATE NOT NULL, 
     `date`            DATE NOT NULL DEFAULT '0000-00-00', 
     `users_ip`        VARCHAR(200) NOT NULL, 
     `activation_code` INT(10) NOT NULL DEFAULT '0', 
     `banned`          INT(1) NOT NULL, 
     `ckey`            VARCHAR(200) NOT NULL, 
     `ctime`           VARCHAR(220) NOT NULL, 
     `approved`        INT(1) NOT NULL DEFAULT '1', 
     PRIMARY KEY (`id`) 
  ) 
ENGINE=INNODB 
DEFAULT CHARSET=latin1 
AUTO_INCREMENT=3;

然后登录到'myaccount.php'后,使用此代码将值输入另一个表,即语言表:

if (empty($_SESSION['$user_id'])) { // user not logged in; redirect to somewhere else }
    if (!empty($_POST['doLanguage']) && $_POST['doLanguage'] == 'Submit') {
        $result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0' order by id desc");
        list($id) = mysql_fetch_row($result);
        session_start();
        $_SESSION['user_id'] = $id;
        foreach ($_POST as $key => $value) if (empty($err)) {
            for ($i = 0;$i < count($_POST["other"]);$i++) {
                $native = mysql_real_escape_string($_POST['native'][$i]);
                $other = mysql_real_escape_string($_POST['other'][$i]);
                $other_list = mysql_real_escape_string($_POST['other_list'][$i]);
                $other_read = mysql_real_escape_string($_POST['other_read'][$i]);
                $other_spokint = mysql_real_escape_string($_POST['other_spokint'][$i]);
                $other_spokprod = mysql_real_escape_string($_POST['other_spokprod'][$i]);
                $other_writ = mysql_real_escape_string($_POST['other_writ'][$i]);
                $sql_insert = "INSERT into `language`
(`user_id`,`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ`  )
VALUES
('$id','$native','$other','$other_list','$other_read','$other_spokint',
'$other_spokprod','$other_writ') ";
                mysql_query($sql_insert, $link) or die("Insertion Failed:" . mysql_error());
            }
            header("Location: myaccount.php?id=' . $_SESSION[user_id] .'");
            exit();
        }
    }
}

一切正常,直到例如我注册id = 3(在用户表中)然后重新登录到id = 1并在语言表中更改它们的详细信息,然后在语言表中更改它们的user_id(这是外键到用户表中的id)为3时应为1.为简单起见,users表中的id应与语言表中的user_id相同。但是当返回并更改languages表中的数据时,user_id与最后注册的id保持一致!

请帮忙!

1 个答案:

答案 0 :(得分:0)

您有以下查询:

$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0' order by id desc");

它的目的是什么?您将$id分配给它找到的第一个值,但查询并未查找用户名或其他任何内容。您可能希望使用$_SESSION['$user_id']代替$id作为用户的ID。