有一个ID为ID的用户的表1。
我创建了一个表2来保存和读取其中的数据。表2中的外键正在工作。
表2具有id,date_from,date_to,users_id作为外键
要保存在表1中的查询就像...
//创建新的用户记录 函数create(){
// insert query
$query = "INSERT INTO
" . $this->table_name . "
SET
firstname = :firstname,
lastname = :lastname,
// prepare the query
$stmt = $this->conn->prepare($query);
$this->firstname=htmlspecialchars(strip_tags($this->firstname));
$this->lastname=htmlspecialchars(strip_tags($this->lastname));
// bind
$stmt->bindParam(':firstname', $this->firstname);
$stmt->bindParam(':lastname', $this->lastname);
$password_hash = password_hash($this->password, PASSWORD_BCRYPT);
$stmt->bindParam(':password', $password_hash);
$stmt->bindParam(':access_level', $this->access_level);
$stmt->bindParam(':access_code', $this->access_code);
$stmt->bindParam(':status', $this->status);
$stmt->bindParam(':created', $this->created);
// execute the query, also check if query was successful
if($stmt->execute()){
return true;
}else{
$this->showError($stmt);
return false;
}
}
查询在用户类中。 如何将数据保存在表2中?