I am using php and sql , I am using this table so it has one serial column
user_id
which is used as primary key (auto_increament). now if user want to delete his profile , i am using
<?php
$sql='DELETE FROM userinfo WHERE user_name="'.$_SESSION["username"].'"';
?>
Its working fine.$_SESSION["username"]
is the name of user who is looged in. . and username are unique as well. But now what is the use of primary key , how i can use that corrosponding primary key to delete the username
. This is just one example. I have comment table now if user delete a comment i have to define 'blog_name','username','comment_time' then it selects the desire comment which i want to delete , so here as well how i can use primary key to delete that content,
Store the primary key value(user_id) in a session variable when user log in and use it in delete query.
For deleting a comment, make the delete link pass the primary key corresponding to that comment to the delete query.
Eg: You have a delete button,when user click on that button redirect to a php page where deletion take place
<a href="deletion.php?pk='put the primary key for that comment here'"><button>Delete</button></a>
deletion.php
You can get the primary key of that comment from $_GET[pk]
. Use it to delete the comment.
Make sure you check the user who is trying to delete the comment is the one who posted the comment, by using the session variables of logged in user.
This method wants a redirection from current page to delete.php and then back to current page. If you want to avoid this use ajax to pass the values in background and refresh only the comment part of the page.