duanjiu1894 2012-03-28 21:55
浏览 27

id没有正确改变

If I register a user using this table:

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;

and then once logged in to 'myaccount.php' use this code to enter values into another table, the language table:

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

All is fine until , for example I register id=3 (in users table) and then log back into id=1 and change their details in the language table, then their user_id in the language table (which is foreign key to id in users table) is 3 when it should be 1. To make things simple, the id in users table should be same as the user_id in the language table. But when going back and changing data in the languages table the user_id stays the same as the last id that registered!

Please help!

  • 写回答

1条回答 默认 最新

  • dongzhu7329 2012-03-28 23:43
    关注

    This query you have:

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

    What is the purpose of it? You are assigning to $id the first value it finds, yet the query doesn't look for user name or anything else. You probably want to user $_SESSION['$user_id'] instead of $id as your user's ID.

    评论

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大