如何在“笔记”中让用户成为“笔记”中笔记的“用户”所有者? PHP MySQL

时间:2018-02-03 22:06:00

标签: php mysql pdo

如何让'users'-table中的人成为'notes'-table中笔记的拥有者?  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 '',
  `password` varchar(70) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

和'notes'是:

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

1 个答案:

答案 0 :(得分:0)

解决方案:创建名为" user_id"的列并使其等于" id" '用户' -table。然后,如果您希望它检查SESSION [' id'] == row [' user_id'],请使用if命令进行检查...

相关问题