如何使“用户”中的用户成为“备注”中备注的所有者?PHP-MySQL
如何使“用户”表中的人成为“备注”表中备注的所有者? e、 g: 我希望表“users”中的id 1是表“notes”中id 2的所有者 编辑:我正在使用“用户”的登录会话 “用户”是:如何使“用户”中的用户成为“备注”中备注的所有者?PHP-MySQL,php,mysql,pdo,Php,Mysql,Pdo,如何使“用户”表中的人成为“备注”表中备注的所有者? e、 g: 我希望表“users”中的id 1是表“notes”中id 2的所有者 编辑:我正在使用“用户”的登录会话 “用户”是: CREATE TABLE `users` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL DEFAULT '', `email` varchar(50) NOT NULL DEFAULT '', `passw
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL DEFAULT '',
`email` varchar(50) NOT NULL DEFAULT '',
`password` varchar(70) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
“注释”是:
CREATE TABLE `notes` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`title` text NOT NULL,
`content` text NOT NULL,
`created` TIMESTAMP DEFAULT now(),
`edited` TIMESTAMP DEFAULT now() ON UPDATE now(),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
这是我的“备注”代码。如上图所述,我如何包括“用户”:
public function fetchNotes($id = null) {
$stmt = $this->pdo->prepare('SELECT title, content FROM notes WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
}
public function create($title, $content) {
$stmt = $this->pdo->prepare('INSERT INTO notes (title, content, created) VALUES (:title, :content, null)');
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->execute();
}
public function delete($id) {
$stmt = $this->pdo->prepare('DELETE FROM notes WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
}
public function edit($id, $title, $content) {
$stmt = $this->pdo->prepare('UPDATE notes SET title = :title, content = :content WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->execute();
}
解决方案:创建一个名为user_id的列,并使其等于“users”表的id。然后使用if命令检查会话['id']==row['user\u id']是否在您希望它检查的任何位置…您需要将userid值传递给公共函数create$title,$content{…}。您是否将登录的“users.”id`存储在会话或其他方法中?为什么不将用户id作为参数传递给create函数,然后以如下方式在fetchNotes中获取它:选择标题、内容、用户id?我照你说的做了,但现在我该如何确切地让users_id of users成为createINSERT下便笺的所有者…?@Sean是的,它是在一个登录帐户中session@Sean是这样的吗?:$user\u id=$\u SESSION['id'];然后插入notes…,user\u id=:user\u id。。。那它呢?