I have 2 tables: tbImages, tbUsers
tbImages has lots of paths of images with unique ids
tbUsers has lots of users with unique ids...
I will code a page that make a delivery of images...This page will show all images ordering by id... The admin will select 7 images to send it to some user by email... I will have to register when and how many times some user has received that image (a 3rd table maybe)...
Here is the problem... I'll have to ORDER images by date of register.. Images already sent have to appear at end of that delivery page...
What is the better way to do this? tbImages has lots of columns one for each user? A 3rd table (tbDelivered) one row each user with column 'images received'? ?How to make this SELECT to show already sent images at end?
A 3rd table relating your imageID to a userID (relational table). Then you can use JOIN statements in your query. For example:
relational_table
imageID
userID
sendCount
Query could be:
SELECT sendCount
FROM relational_table
WHERE imageID = $imageID
AND userID = $userID
That would give you back the number of times that image has been sent to that user.
You will have to create a 3rd table that will contain:
The image ID
The user ID
The quantity
And you will make a select statement that will look like this:
SELECT tbImages.imageID, tbDelivered.quantity
FROM tbImages, tbDelivered
WHERE tbImages.imageID = tbDelivered.imageID;
To show the ones that have not already been delivered you will have to add at the end of it:
AND quantity = 0;
I hope this helps
Related
I have a MySQL table that works as a pivot/linking table and it initially contained two columns, both of which were foreign keys. These two columns were a board_id column and an image_id column. This table basically holds the data for when images are allocated to image boards. Obviously a board can hold multiple images, and an image can be be on multiple boards. This table is the called boards_images table. I'm using PHP PDO to update the database.
The Issue
Although a user has to be logged in to add images to the boards, because this data is publicly viewable on the submitting form via value attributes in form elements (i.e. a submit button and an input element) I want to be able to run a check first, to ensure the board (board_id) that the image is being allocated to belongs to the logged in user. A user's user_id is available via a logged in $_SESSION value. If I don't run this check a user could manually make changes to these values in the HTML and add the images to random boards of other users.
My initial approach has been that the boards_images linking table now contains three columns board_id, image_id and user_id which are foreign keys from the boards, images, and users tables respectively.
My question is how do I run the check, to ensure the board_id belongs to the logged in user, prior to adding an image to the board?
The boards table does have the user_id as a foreign key.
The code I'm currently using is below, it uses INSERT IGNORE to prevent duplicates (i.e. it prevents images being added to the same board more than once).
if (isset($_POST['submit-board-name'])) {
$boardIdValue = $_POST['board-name']; // board id value is shown in button's value attribute
$imageIdValue = $_POST['image-id-for-board']; // hidden element that shows image id of selected image
$stmt = $connection->prepare(
"INSERT IGNORE boards_images (board_id, image_id, user_id)
VALUES (:board_id, :image_id, :user_id)"
);
$stmt->execute([
':board_id' => $boardIdValue,
':image_id' => $imageIdValue,
':user_id' => $sessionId
]);
}
Will I need to a do SELECT of joined data first from the boards table?
When deleting an image from the board this is very simple to allocate to the logged in user, because all I need to do is add another condition to the WHERE clause, i.e
"DELETE FROM boards_images
WHERE board_id = :board_id AND image_id = :image_id AND user_id = :user_id"
// do PDO stuff
Question Summary
How do I check that when the board is being allocated an image, this board belongs to the logged in user, prior to allocating the image to that board?
Any help would be amazing.
Well if you say the boards table also have a user_id column...
$stmt = $connection->prepare(
"SELECT user_id
FROM boards
WHERE board_id = :board_id
LIMIT 1"
);
$stmt->execute([':board_id' => $boardIdValue]);
if($stmt->fetchColumn() == $sessionId) {
// The board belongs to the user, add the image
}
else {
// The board does not belong to the user, print error message etc.
}
I want to create a form where the logged author can fill a textarea and then select 1 or more system users from a checkbox
So when every user logs in he Will see that record only if he has been selected in the checkbox before.
How can i do it?
Must i save values of the checkbox in a single record and then split to set the correct data set?
And then how can i show the whole records to the current user as i want him to see a list of records where he is the author or where he is among the users allowed to set that very same record
I know this can be a generic request but the matter is i dont understand how i could do it, so the answer i look for is not "give me the code" but"explain me the main steps i must follow", someone can give me some hints?
How about creating a table named "record" with the following structure:
id author_id allowed_user_id text
Now, when the author with id 1 selects two users with ids 2 and 3, you insert two rows into that table, one for each allowed user
id author_id allowed_user_id text
1 1 2 just a test
2 1 3 just a test
To show the current user the records he authored
SELECT * FROM record WHERE author_id = current_user_id
and records he's allowed
SELECT * FROM record WHERE allowed_user_id = current_user_id
I would like to make somehow latest updates script for my webpage.
I have several tables in my MySQL database:
TABLES:
latest_updates
images
gallery_images
profile_info
questions
Example:
User1 adds new image to "images" table
User2 adds new question in "questions" table
User1 adds changes his profile info in "profile info" table
All these tables are containing user_id which is connected to certain user.
Now what is the best solution to make this status updates script when some user updates or inserts new row to any of these tables
I would like to have some sort of script saying:
User1 has added new image to his gallery
User2 has created new question
User3 changed his profile information
Is it to make another table and every new insert in any tables above, also insert the update in that table or try to make some sort of SQL query to join the tables and read the latest?
If the second option is the best could you please give an example?
Cheerz
Create new table user_updates (or some else) with fields:
id (autoinc, primary)
id_user
id_dest (destination table id)
table_dest (destination table name)
action (modify/create)
changed (timestamp, when happened)
Then you can retrieve all newest rows from this table and show recent updates. You can also join this table to "real" data and show what has been modified.
The easiest way would be to create a new table which would record when things are created/edited. If you prefer the harder route.. I would break down each section into it's own SQL query, and order it by the latest SQL timestamp.
e.g to display the latest updates to images:
$sql="select * from images ORDER BY TIMESTAMP ASC";
$stmt = $connection->prepare($sql);
$stmt->execute();
$res = $stmt->get_result();
$latestimages = $res->fetch_array(MYSQLI_ASSOC);
echo $latestimages['user'] . " has uploaded a new image!";
Doing it this way has some advantages, for example if you had content boxes on your page each with its own header e.g. Latest Images, Latest Forum Posts, Latest Blog Updates etc you could use code similar to this and just add a LIMIT to the SQL query. (to display latest 5 results, add a LIMIT of 5)
MySQL code: jsfiddle.net/HBps8/
I have a table called "Likers", in that table, there is "ID, User FB ID, user FB name, and user Access Token".
At the moment, there are 2487 records in the "Likers" table.
(ID is the number of user, if the user submitted his access token when the table was EMPTIED; his ID will be 1, if another user submits his access token after the first user; his id will be 2 and so on and on)
The problem I am facing is that when the HTML form is submitted to get the IDs; it gets the OLDEST ids first (ID #1, #2, #3, #4, etc...)
What I want is that when the form is submitted, the RECENT ID's should be first (ID #2487, #2486, #2485, #2484, etc...)
Is it possible to do something like that in MySQL database? I am very new to the databases codes and I am trying my best to learn :)
Thanks in advance.
In your MySQL query you can use ORDER BY
For example
SELECT * FROM `Likers` ORDER BY `ID` DESC
you can sort the result by using ORDER BY, e.g. ORDER BY ID DESC. But it is not recommended to determine the latest entries by the ID, you should add a additional field like creationDate and sort by this one.
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.