Guide on assigning ownership of a note in the 'notes' table to a user in the 'users' table using PHP and MySQL

Is there a way to assign ownership of a note in the 'notes' table to a user from the 'users' table?

For example, how can I make user with id "1" in the 'users' table the owner of the note with id "2" in the 'notes' table?

UPDATE: I am utilizing a logged-in session for 'users'

The structure of the 'users' table is as follows:

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 this is how the 'notes' table is structured:

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;

Below is my current code for handling notes and I need to include functionality to link 'users' as described above:

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

Answer №1

To resolve this issue, create a column called "user_id" and set it equal to the "id" field in the 'users' table. Next, use an if statement to check if SESSION['id'] is equal to row['user_id'] at any desired location...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

No files were found in the request when using PHP's cURL

I need assistance in converting this curl request to PHP. curl -X POST -F "file=@test_img.jpg" "http://127.0.0.1:5000/FileUploading/UploadImage/"test_img.jpg" The above curl request works properly with the following flask-RESTful code class UploadImage( ...

What is the best way to attach every sibling element to its adjacent sibling using jQuery?

I've been working with divs in Wordpress, where each div contains a ul element. <div class="list-item"> <div class="elimore_trim"> Lorem ipsum </div> <ul class="hyrra-forrad-size-list"> <li>Rent 1< ...

Leveraging TypeORM with MySql in a Node.js Lambda Function

I am currently attempting to create lambda functions in node.js using TypeORM with MySql. Despite installing all the necessary node modules, I encounter errors when deploying the lambda (via serverless) and testing it. The error message is as follows: I am ...

Attempting to gather information from a pair of inquiries

When attempting to retrieve data from two requests on two tables, I encountered some challenges in retrieving the expected results. My goal is to obtain a list of all elements included in each group within my dataset. This means acquiring all group IDs and ...

In CodeIgniter, the $this->input->post() function consistently returns an empty value

I'm encountering an issue where the value from an AJAX post always turns out empty. Even after confirming that the value is correct before the post, I'm unable to retrieve it using $this->input->post() HTML <?php if ($product_info->stock ...

Guide on automatically attaching a file to an input file type field from a database

Currently, I am implementing a PHP file attachment feature to upload files. Upon successful upload, the system stores the filename with its respective extension in the database. The issue arises when trying to retrieve and display all entries from the ...

Transforming child nodes into parent nodes during CSV import in Drupal6

In my current Drupal project, I am faced with a situation where I have two types of nodes created using CCK content types - "venues" as parent nodes and "concerts" as child nodes. After successfully importing a list of well-known venues from a CSV file us ...

Laravel's hasManyThrough relationship allows for linking to another model

I am currently facing a situation where the Parent and Student models are in a many-to-many relationship, each of them belonging to the User model. The Branch is connected to many Students through the User via hasManyThrough(Student::class, User::class);. ...

Utilizing jQuery.post to generate a dynamic dropdown menu

I recently designed a dropdown list that functions well on a standalone page. However, I encountered an issue when attempting to display it in a table on another page using jQuery.post(). The contents appear to overflow from the dropdown list. The jQuery ...

Every time I attempt to create a detail page for a table, I am consistently greeted with an error message

Error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/2909kher/entabell/detaljer.php on line 23 <!doctype ...

Monitor a user's activity and restrict the use of multiple windows or tabs using a combination of PHP and

I am looking to create a system that restricts visitors to view only one webpage at a time, allowing only one browser window or tab to be open. To achieve this, I have utilized a session variable named "is_viewing". When set to true, access to additional ...

Retrieve data from a JSON array using either JavaScript or PHP

Check out my code snippet: const newData = [{"new_id":"1","new_no":"1","total":"N.A"},{"new_id":"2","new_no":"3","total":"4"},{"new_id":"2","new_no":"4","total":"5"}]; Now, I need to extract specific details from this JSON data based on the 'new_no& ...

What do I need to do to get this PHP script running smoothly?

I was expecting the following output to be displayed live got here However, it is displaying got here The code snippet provided: $config['env'] = 'live'; sayEnvironment(); function sayEnvironment () { echo $config['env&a ...

Struggling to create a basic CMS using PHP and MySQL, unable to successfully display the data

My latest project involves creating a basic CMS for a website. This is my first time working with MySQL and one of my early experiences with PHP, so I see it as a learning opportunity. Despite consulting numerous resources, the code I've written only ...

Organize based on primary website address

Here is a collection of URLs I need to organize: http://www.example.com/ http://www.another.com/1/hi-hello http://www.example.com/index.php http://www.another.com/1/archive http://www.example.com/about.php http://www.another.com/ All these URLs are curre ...

"Exploring the world of Codeigniter 2 alongside the capabilities of

I have successfully installed CodeIgniter framework version 2 and created a PHPunit test in /tests/unit/PostTest.php with the following code: <?php class PostTest extends PHPUnit_Framework_TestCase { private $CI; public function s ...

Unable to obtain the Twilio "call recording" URL

Currently, I am working on an online campaign management system that utilizes Twilio. Each campaign in the system will have a unique Twilio number and target number. When a call is made to a specific Twilio number assigned to a campaign, it will be forward ...

Handling MethodNotAllowedHttpException in IIS, AngularJS, and Laravel: A Comprehensive Guide

Currently, I am diving into the world of AngularJS 1.5.8 + Laravel 5.3.17 + PHP 7 hosted on IIS/Windows 10 by following this helpful tutorial. However, when attempting to send a HTTP request using AngularJS $resource.delete() to Laravel, I keep encounteri ...

Automatically load and instantiate all classes in PHP

My current setup involves using php spl_autoload_extensions to automatically load all classes. The question I have is whether it's possible to instantiate classes simultaneously, eliminating the need for manual instantiation afterwards. Below is my b ...

There seems to be an issue with your SQL syntax that is preventing the data from being entered correctly into the database using Node.js, MySQL, and Express

After successfully displaying data from my database, I attempted to add data to it using the following code snippet: exports.tambahData = (req, res) => { var keyy = req.body.keyy; var valuee = req.body.valuee; var brand = req.body.brand; ...