如何使用PDO在两个不同的表中更新记录?

时间:2015-08-10 03:36:11

标签: php pdo

我有一个名为update.php的页面,我想要做的是我想同时在两个表中更新记录记录。我现在在这里只是更新一个表,任何人都可以请帮帮我吗?

****这是我的class.user.php **

的代码
public function update($user_id,$username,$password,$province)
{
try
{
  $stmt=$this->db->prepare("UPDATE login SET  username=:username,password=:password,province=:province WHERE user_id=:user_id");
  $stmt->bindparam(":username",$username);
  $stmt->bindparam(":password",$password);
  $stmt->bindparam(":province",$province);
  $stmt->execute();

  return true;
}
catch(PDOException $e)
{
  echo $e->getMessage();
  return false;
}
}

现在适用于update.php

include_once 'dbconfig.php';

if(isset($_POST['btn-update']))
    {
    $ID = $_GET['ID'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $province = $_POST['province'];

if($crud->update($ID,$username,$password,$province))
    {
    echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
    }
    else
    {
    echo "<script type='text/javascript'>alert('Updating Failed!'); </script>";
    }
}
    if(isset($_GET['ID']))
    {
    $ID = $_GET['ID'];
    extract($crud->getID($ID)); 
}
?>

1 个答案:

答案 0 :(得分:1)

试试这个..

  UPDATE login, login2
    SET login.username =            :username, 
        login.password =           :password,
        login.province =        :province, 
         login2.username =            :username, 
        login2.contacts =           :contacts            
    WHERE login.user_id = :user_id and login2.user_id = :user_id ;