Excluding Row From MySQL Query If ID is Blocked - php

I'm sure this is MySQL 101 and I apologise if the answer is simple, but I'm a little stuck so here I am.
I have a table of users, named 'site_members' with various fields but importantly, each with a unique id.
I also have a very simple two-field table, named 'blocked_members' which contains the id's of users who have been blocked by the logged in user, which is simply set up to store the user id of the person blocked, and the user id of the blocker:
Field 1 | Blocked
Field 2 | Blocker
Before I run a query on the 'site_members' table, I run a query on the 'blocked_members' table to find out if the current logged in user appears in either column:
$sql = "SELECT * FROM blocked_members WHERE blocker = '$loggedin_id' OR blocked = '$loggedin_id' ";
What I need to do next is get the corresponding user id from each row the logged in user's id appears in, and exclude that(those) from my main query on the 'site_members' table, but I do not know how I would go about this?
Can anyone help or point me in a cleaner direction?

You use a subselect.
SELECT * FROM whatever WHERE posting_user_id NOT IN
(SELECT blockedid FROM blocklist WHERE blockerid = <<current user viewing the page>>)
while inserting the ID of the User whose blacklist shall be applied.

You can do inner query... you cann do
... WHERE blocker NOT IN (SELECT bloker_id FROM ...)

Related

Query and Display referrer details

I have a website built with PHP and mysqli and now I am building administration panel for this website. I am facing some difficulties for querying and displaying referral data I do not have any idea how to do it.
I have a table called user_registration and the fields include, user_name, email, password, referrer. Whenever any of the registered member is referring others the referrer username will be saved in the field "referrer".
Now what I want is to fetch only the rows of the members who has referrals (means referred by my registered members) and also want to count how many referrals a member have and echo it.
For example: I have 20 registered members and from it 5 members have some referrals so I want to query and echo those member's username who has referrals and also count how many referrals they have:
Member's Username Total Referrals
user7 8
user6 6
user1 5
user9 3
user5 2
My solution would be. Make a new column in your user table like "totalreferrals". In this way you can easily keep track of the amount of people they have referred. You could also make a new table "referral". In here you would save the referrals name, the new persons name and a timestamp or something. Now it is even possible to make queries for different periods.
I think that you need to rethink about your database setup, instead of trying to make way to complicated queries, to make your database work.
I am just giving an idea in short which may help you to complete your project -
Suppose your site URL is http://yoursite.com.
1) Whenever a user is registered, you can generate a random key for that user and save it in the database for that particular user. e.g. user1 => abc123
2) Now, you can tell your user (may be on a page after their successful registration) that his/her referrer id is http://yoursite.com/?ref_id=abc123 (by appending it as a new parameter)
or if your register page is http://yoursite.com/register then http://yoursite.com/register/?ref_id=abc123
3) Now, they can share this link to their friends, colleague etc to whomever they want.
4) Now, suppose if a person (may be his friend to whom the user1 referred) clicks on the above link. He will land on your site with the parameter 'ref_id' appended.
5) Just using $_REQUEST['ref_id'] or $_GET['ref_id'], capture its value and store it in a SESSION variable. This you can do on the main page of your site e.g. index.php
6) Now, if that user does the registration, you can make an entry in the database that he has been referred once by the user who has referrer id abc123 i.e. user1.
7) So, you can add count = 1 for user1. If more people come with the same value for the 'ref_id' parameter, you can keep on increment the count.
8) If no such parameter is exists when user lands on your site, then that means he has not referred by anyone. So, that means he is the first kind of user i.e. user in point no. 1).
9) You may also need to take care of some validation part at some places in this.
EDIT:
SELECT user_name, referrer FROM user_registration WHERE referrer > 0;
Assuming that you have a proper insert query ready which gives you the result as you shown the table in your question.

Store comments on networking site with MySQL

I want to create a MySQL database for a project in which users can come and make comments on other profile. Every profile has a unique id to identify it, now when a user comes and makes comment on other profile I'll need to store the user id of the person who made the comment and the person on whose profile the comment was made, along with that I'll need to store the comment in the database.
As many users can make comments on a single profile, I'll need to store all the comments and the users who made them on a single profile. For this how many tables should I create and how many columns should they have? For this I'm thinking about creating a table for named user_comments and that has column user_id, commenter_id (all the commenter who commented their id separated by comma), comments (then all the user comments separated by comma).
Will this work?
For this I'm thinking about creating a table for named user_comments
and that has column user_id ,commenter_id(all the commenter who
commented their id seprated by comma) ,comments(then All the user
comments seprated by comma)
God no! You are almost there:
Table comments
id INT AUTO_INCREMENT
recipient_id
sender_id
message TEXT
[ sent DATETIME ]
[ other meta data ]
Store one message in message. Create one row per message. Never store several records in the same field separated by anything.
I'd have a profile_comment table:
id, text, profile_user_id, commenter_user_id, created_at
And a user table:
id, name, email
You can see here that the first table has two foreign keys to the user table - one points to the owner of the profile, and the other points to the owner of the comment. You can sort them in order of created_at to list them as you would on a blog, either in forward or reverse order.
Now, when you are rendering a profile page, you can get the profile id from your query string:
$profileId = isset( $_GET['profile_id'] ) ? $_GET['profile_id'] : null;
From there, you can add it into a SQL query:
SELECT * FROM profile_comment
WHERE profile_user_id = :profile_id
ORDER BY created_at
The colon mark here is a placeholder you can use with a parameterised query, which helps protect against SQL injection. However, you can build the statement as a string if you are careful to untaint any user input you insert into it.

Show specific content on a page for each member when they log in

I have two tables in my database. One is a list of members each, of course, having a different member ID. The second table is a list of items that have been designated to one member.
Please note that each table has a user_id column. Based on user_id I want only certain items displayed on their webpage.
When Joe logs to the members area, he is taken to a welcome page. On that welcome page I want Joe to be able to click on a link which will take him to a page. This page will show items that only Joe is allowed to see. If another member clicks on the link it shows a message that says something like Sorry, not for your eyes.
I really am only learning and have spent the past couple of days looking for help with this issue.
I am thinking, from what I have read, that I may just be able to amend the top of my "special page" with an if statement which would say something like
if user_id from table_members = user_id from table_items
Show all rows from table_items which = user_id
There are huge number of tutorials available on internet on displaying dynamic data. Just try doing a search on "making dynamic webpages using php" like that.
let $user_id be a variable storing userid taken from user logged into the website.
$query="select * from table_items where user_id=".$user_id ;
$result=mysql_query($result);
To fetch a row:
`$row=mysql_fetch_array($result);`
To fetch specific column from that row:
$column_data=$row[$columnNameString];
Execute that query you will only get rows whose user_id column value equals to user_id variable value.

Updating membership MySQL database automatically in PHP

I have set up the database with all the various fields and can carry out all the necessary registration and logins etc. – so far so good!
What I need to do now is as follows:
When a new member attempts to register he/she must provide the email address of an existing member (like a sponsor, as it were). If the sponsor email is a fake or doesn’t exist in the data base, the application to join is rejected.
If genuine, I need to automatically retrieve the record of the sponsor and update his/her record by putting the new member in one of two possible fields. Each member is allowed to sponsor two and only two new members. So if the sponsor has no new members, field-A and field-B will be empty, therefore the email of the new member will be allocated to field-A. If, on the other hand there is already an entry in field-A, the new member email will be stored in field-B. If both are already filled a warning will be flagged and the sponsor will be refused permission to introduce the “third” new member.
I would be most appreciative of any help with this, having spent the best part of a week researching without success. I am sure there is a fairly simple answer from an expert on this wonderful site.
Fang Man,
I would not use 2 columns for this.
I would create a new table member_sponsors for instance.
It would consist of:
id - auto incrementing primary index
sponser_id - id of sponsor
memeber_id - id of new memeber
sponsor_date - date member was sponsored?
This way you can simply query this table and check if it contains less then 2 (or if down the road you want to increase this value this can be done easily).
Then just add the info to this table.
Hope this concept makes sense and is helpful to you.
Tim
Assuming your table with the existing members is called "members" with the following structure:
Field name Type
------------------------------------------
id int (primary, auto)
name varchar
email varchar
submember1 int
submember2 int
When the registration request is submitted, I assume you'll have a value like "sponsor_email" to check. After sanitizing this value and ensuring that it is an email address (hint: use regex), grab the info about your member:
SELECT
id,
name,
submember1,
submember2
FROM
members
WHERE
email = "'.$sponsor_email.'"
... and make sure the user signing up has a valid member's email address. If so, check to see if that member has an open slot:
if ($member['submember1'] > 0 && $member['submember2'] > 0) {
// add warning to sponsor, reject the signup
}
Now, you'll have to figure out which slot to put this new signup in - if $member['submember1'] is greater than zero (already filled), the signup goes in slot 2, else slot 1.
A much more flexible way is to add an associative table linking members and the sponsors, do this instead of adding fields to track this in the member table. There are a few advantages to this approach, most importantly that if you decide you want to increase the limit 5 submembers instead of 2 (for example), you can easily change a few small spots in the code without touching the database structure. Done "your way", you would have to change the database AND the code...
if ($member['submember1'] > 0 && $member['submember2'] > 0 && $member['submember3'] > 0 && $member['submember4'] > 0 && $member['submember5'] >0) {
// add warning to sponsor, reject the signup
}
... yuck! Plus, this will get hairy deciding which slot to put the new signup in. An associative table does not have this problem. Using the associative table route, you still have the members table:
Field name Type
------------------------------------------
id int (primary, auto)
name varchar
email varchar
... and then you have a member_sponsors table:
Field name Type
------------------------------------------
id int (primary, auto)
sponsor_id int
member_id int
The member_id field holds the id of the "sponsoring" member, the sponsor_id field holds the id of the "sponsored" member.
Using these tables in the signup process, again, get your member, but also do a join and count of their current sponsors
SELECT
members.id,
members.name,
COUNT(member_sponsors.id) AS sponsored_count
FROM
members
LEFT JOIN
member_sponsors ON
member_sponsors.member_id = members.id
WHERE
email = "'.$sponsor_email.'"
GROUP BY
members.id
Now, as long as the email matches, you will have a row with the id, name, and current number of sponsored members the given user has. Your php would now look like this:
$limit_on_sponsorships = 2;
if ($member['sponsored_count'] >= $limit_on_sponsorships) {
// add warning to sponsor, reject the signup
}
$limit_on_sponsorships can come from anywhere, make it a constant, make it a database setting, etc... all you have to do now to make a change to the number of allowed sponsors is modify that one value. Using this method makes your code more flexible and ready for changes.

Friends table for a social network website

Now i am working on a social network website and i already built the friends table for this website but i need some suggestion before i moved forward.
I have a user table where user_id is the primary field.
So for friends i made a friends table with fields like
friend1,friend2,is_active
what i do actually when user1 send a friend request to user2 then at that moment i insert the two rows into the friends table like
1- friend1->user1,friend2->user2,inactive
2- friend1->user2,friend2->user1,inactive
So when somebody accept the friend request at that moment i made the both rows as active.
I made this two entries as i want whenever one user block another then easily made that corresponding row belongs to that user will be inactive.
And if a user remove the user then at that time i delete the both entries.
So please i need some suggestion regarding this please help me out to solve this asap.
I need to know is there any other way which will be more optimized than this and giving proper result.
I think that
Friends_table
with
User_id1, User_id2 and Rleationship_id
is easier, index on both first and second column.
relationship id will be 1 - id1 request, 2- id2 request, 3 - friends, 4- id1 blocked, 5 - id2 blocked...
just an idea.

Categories