如何将1个表中的id同时插入到另一个表中?

时间:2015-08-11 02:45:27

标签: php mysql pdo

我有两个表登录和用户。在表登录我有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方面苦苦挣扎..

3 个答案:

答案 0 :(得分:0)

您尚未定义$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();
   // make changes here create $user_id
$user_id =  $this->db->insert_id;
$stmt = $this->db->prepare("INSERT INTO sample(username,user_id) VALUES (:username, :user_id)");
    // make changes here in user_id instead of user_i
 $stmt->bindparam(":user_id",$user_id);

 $stmt->bindparam(":username",$username);
 $stmt->execute();
 return true;
}
catch(PDOException $e)
{
  echo $e->getMessage();  
  return false;
}
}

答案 1 :(得分:0)

您可以创建数据库触发器,如

Create or replace trigger <trigger_name> after insert login ...

在插入第一个表时,您可以将记录插入另一个具有相同ID的表中。您可以按指定创建触发器 Here

答案 2 :(得分:0)

您应该可以使用PDO::lastInsertId - &gt; $this->db->lastInsertId();

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

  $user_id = $this->db->lastInsertId(); 

  $stmt = $this->db->prepare("INSERT INTO sample(username,user_id) VALUES (:username, :user_id)");
  $stmt->bindparam(":user_id",$user_id);
  $stmt->bindparam(":username",$username);
  $stmt->execute();
  return true;
}
catch(PDOException $e)
{
  echo $e->getMessage();  
  return false;
}
}

或者你可以进行INSERT ... SELECT ...查询, 看起来像 -

INSERT INTO sample(username,user_id) SELECT :username, user_id FROM login WHERE username = :username1

所以你的功能看起来像 -

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,user_id) SELECT  :username, user_id FROM login WHERE username = :username1");
  $stmt->bindparam(":username",$username);
  $stmt->bindparam(":username1",$username);
  $stmt->execute();
  return true;
}
catch(PDOException $e)
{
  echo $e->getMessage();  
  return false;
}
}