How do I make a person in 'users'-table an owner of a note in 'notes'-table? e.g.:
I want id "1" from table 'users' to be owner of id "2" from table 'notes'.
EDIT: I'm using a logged in session for 'users'
'users' is:
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;
and 'notes' is:
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;
Here is my 'notes' code(how do I include 'users' as explained on top):
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();
}
Solution: Make a column named "user_id" and make it equal to "id" of 'users'-table. Then you check with an if-command if SESSION['id'] == row['user_id'] anywhere you want it to check...