【发布时间】:2015-11-03 02:59:01
【问题描述】:
我有两个表登录和用户。在表登录中我有 user_id(主键),用户名(varchar),密码(varchar)和表用户 id(主键),用户名(varchar),user_id(int)..我想要发生的是当我在表登录中插入一条记录 user_id 也应该插入表用户的 user_id 中。请帮助我。
这是我的插入代码
public function create($username,$password,$province)
{
try
{
$stmt = $this->db->prepare("INSERT INTO login(username,password,province) VALUES(:username, :password, :province)");
$stmt->bindparam(":username",$username);
$stmt->bindparam(":password",$password);
$stmt->bindparam(":province",$province);
$stmt->execute();
$stmt = $this->db->prepare("INSERT INTO sample(username) VALUES (:username)");
$stmt->bindparam(":username",$username);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
到目前为止,我所做的是同时向表中插入一条记录..但我有点苦恼如何将表登录中的 user_id 插入表用户中的 user_id..
【问题讨论】:
-
@Fred-ii- 很抱歉粘贴不好
-
你的表是
login和sample;所以user_id from table login to user_id in table users没有意义。user_id是否在login中自动递增?如果是这样,可以从这里开始,php.net/manual/en/pdo.lastinsertid.php。 -
是的。登录表中的user_id是自增的
-
而
sample是users?使用lastinsertid。我不知道您为什么需要sample或users表,但似乎所有数据都已经在login.. -
示例表中的 user_id 设置为整数。我想要的是,当我在登录表中插入记录时,登录中的 user_id 也应该插入到示例表中的 user_id 中