Welcome Guest, Not a member yet? Register   Sign In
Delete from database ONLY if the uid from the database matches the uid in session

My users can create a note that they can later delete, if they wish.

I'm storing the note in the database and these are my columns in my database: id, uid, note, time.

In my model (model_entry), I have this code to delete the note from database

PHP Code:

This, however, can let anyone delete any post id if they have the post id.

This is what I mean.

My view (view_all_entries):

PHP Code:
<a href="<?php echo base_url() . "profile/delete_entry/" . $data->pid; ?>">Delete</a

As you can see, it gets the pid and puts it in the website url and then moves it on to my controller.

My controller (profile.php) :

PHP Code:
function delete_entry() {
       $pid $this->uri->segment(3);

And finally, my model which I have also written the code for above.

Model (model_entry):

PHP Code:

My question: I don't want anyone going in and deleting the notes. The person can only delete the note if IT BELONGS TO THEIR UID.

So if the pid is 3 and the uid is 1, only the user which has the uid 3 can delete the note (pid 3), no one else.

(This post was last modified: 03-03-2015, 06:05 AM by tapan.thapa.)


Once user is logged in, you should store user id (In your case uid) in session.

And once user is pressing delete button which is having pid, should be checked with uid.

For setting uid in session:

For Delete use in model:

$this->db->where('pid', $pid);
$this->db->where('uid', $this->session->userdata('uid'));

Thanks & Regards
Tapan Thapa

(This post was last modified: 03-03-2015, 06:03 AM by _this.)

Perfect solution, was planning to answer the same while reading post on homepage ^^

You just made a little mistake :

PHP Code:

Should be :

PHP Code:

And it's more secure (to me) to make a check before accessing a session var :

PHP Code:
if($this->session->userdata('uid') === false) {
// Throw error
} else {


Theme © iAndrew 2016 - Forum software by © MyBB