Create unique ID for users offer [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a SQL-table for user information and a table for offers. If a user uploads an offer, it gets a unique id, aswell as a "belonging_id" with the users id in it.
Now I want to change an element after the offer is already in my Database. But the only information I have is the user's id. And it is possible for a user to have multiple offers.
So my Question is: How can I call one specific offer with a unique id to Update it?

One solution would be, you can make your table as 3-column table, like this:
+----+--------------+----------+
| id | belonging_id | offer_id |
+----+--------------+----------+
| | | |
id column would be AUTO_INCREMENT and PRIMARY KEY, belonging_id would be user's id and and offer_id would be your unique offer id. This way, you can satisfy the following condition,
And it is possible for a user to have multiple offers.
An example table snippet would be like this:
+----+--------------+----------+
| id | belonging_id | offer_id |
+----+--------------+----------+
| 1 | A | XYZ |
| 2 | A | ABC |
| 3 | A | PQR |
| 4 | B | IJK |
+----+--------------+----------+
Now, you can select a particular <belonging_id, offer_id> pair using the id column value.
Sidenote: If you're planning to use this method and insert records in this table, look at the following links to get last inserted id (pick one as per your extension),
mysqli::$insert_id
PDO::lastInsertId

Please give us your SQL Table schemes.
To help you, I think your request should be something like :
SELECT * FROM offers WHERE belonging_id = id_user
EDIT :
You need to have a unique identifier (something like primary key, or a UNIQUE field) in your table offers if you want to retrieve a specific offer among all the user's offers.
When you insert data in a table, you can retrieve the last inserted id.
For exemple, in PHP (using PDO extension) you can do : echo $db->lastInsertId();
EDIT 2:
Let's suppose the user ends on a page send_offer.php after he sent the offer in the database.
You should then generate a unique link like : http://edit_offer.php/?id_offer=the_id_of_the_offer_just_sent
This unique link should be stored in your table offers, so when the user wants to edit one of his offer, he only has to access your site with this link.
This link should point to a web page where there's a form, in which you'll want to load the data of the offer corresponding to the above link.

A user has multiple offers
You want to edit a specific offer
All you know is the user's identity
What you're asking has no coding solution because it has no logical solution. It's like if I gave you a jar of blue and white marbles and I asked you to bring me a specific blue marble. All you know is the color so without further info there is no way to reliably get the marble I want.
Instead of trying to devise a solution on the server end, turn to the front-end. Figure out how to send info that will uniquely identify the request, along with the userID. For instance, include the offer ID in the request process.php?offer_ID=123 so you can retrieve it with $_GET['offer_id'] and use it in the query.
If you have no way to do it all in one step, you could instead:
SELECT * FROM offers WHERE belonging_id = $userID
Send all results to the front end, with their offer IDs attached
Ask the user to select a specific offer from the matches, and return its ID to the server
SELECT * FROM offers WHERE offer_id = $offerID and process that record

Related

Store multiple values in one db column

I am trying to create a pivot table to help keep track of "challenges" in my applications. Basically I have a challenge_task pivot table that creates a relationship between a challenge and a task. When a user that is in a challenge completes a task I want to be able to tell so I can track a user's progress. How is the best way to store multiple users completing a task on a challenge?
I was thinking in the pivot table adding a json column called user_completed to handle this and store the user_id for every user that completes the task for a challenge.
So challenge_task would look like
challenge_id | task_id | user_completed
Is this a good way? Or is there anything that fits this better?
I'd recommend a database structure something like this:
challenge: challenge_id | other data
task: task_id | other data
user: user_id | other data
challenge_task: challenge_task_id | challenge_id | task_id
| possibly more data (such as deadline for completion)
challenge_task_users: challenge_task_id | user_id
| possibly more data (such as status: accepted, in progress, completed)
I dont recommend Json if you want to index your data, because Json can not be indexed.
I think you should make a pivot table between the users and the tasks too, and create the neccesary relations.
I wouldn't recommend you inserting multiple values in one database column.
Note: This is my opinion. Just sharing the way I use it.
A table called tasks_settings which has the task settings.
I find this way flexible because I can always edit the title, description, and reward easily. I can also add 2 more fields here which are valid_till and valid_for. So you can make it expire after a period of time and only for a special rank like staff or all users.
Another table called users_tasks
This one controls the users. Whether they have completed the task or not. This could also achieve what you are looking for.
id | challenge_id | task_id | username | user_completed
I hope this has helped you!

Storing a list of songs linked to event mysql

I have a database in MySQL that currently lists approximately 1500 concerts and events. Now, the plan is to add setlists (list of the songs performed at the concerts) for all the concerts in the database. Basically this will mean a lot of repeated values (songs performed at many concerts), and I would really appriciate some input on what the best approach would be.
I initially started out with a database similar to this;
| eventID | edate | venue | city | setlist |
The field setlist was basically text data, where I could paste the list of songs and parse through it to put each song on a new line with php. This works, and editing the text and running order was like editing a text document. Now, obviously this was pretty simple, but has drawbacks and limitations. Simple things like getting stats on songs performed is probably very difficult, right?
So, what is the best way to store the setlist value?
Create a new table that adds a new row for each song performed, and that has a foreign key linking to eventID? How would I best retain (and edit, if needed) the running order of the songs in that table? Any other suggestions?
Thanks for any input or advice on this, as I would love to get some help before I start adding all the data.
I would create a table that holds each song performed at a specific event:
| songId | eventID | song |
Where eventID can be duplicated in multiple rows to show each song performed at that event.
This way you can query all the times a specific song was performed, and also get all songs (the setlist) for a specific event by querying on the eventID.

PHP Link user table to another table

Hi I am in the process of making a website that includes a user registration system for my final year of high school major project. The website stores driving logs for learner drivers. I'm kind of confused as to how I should desgin the database. I have a users table which stores the personal information of each user of the site. However, I would like the user to be able to insert information into another table which would be their "logbook" and this to be displayed on the my account page. Do I need to create a table within the database for each user or is there a way of connecting the tables so that i do not have to.
You do not need to create a table for each user. Instead, add a column in your "logbook" table which will contain and refer to the "id" of the user it's tracking. This will likely be the primary key of your "users" table. Then, to get the logs for a specific user, you would query the logbook table for rows only with at specific user ID.
Furthermore, in a more sophisticated setup, you can add constraints to link the two columns. See http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
U will not need table for each user...
Just one table will help u out...
The other table where u want to store user logs should only have reference to user I'd that u have created in users table..
Primary and foreign key - relational table concepts
For eg:
Let ur user has
1.userid
2.first name
3.lastname.... And other colums
Then ur userlogbook wala table should have
1.logid
2.userid
3.other columns...
Hope u got it..
You only need 1 table for the users, each user will be a row inside that table.
As you see in this table below, you need a way to be able to track which logbook record belongs to which user. That's done by storing the users ID (or any unique identifier, usually the primary key) to know exactly which user created the record.
-------------- -----------------
| User Table | | Logbook Table |
------------------------- -----------------------------------
| id | name | ...etc... | | id | user_id | date | ...etc... |
------------------------- -----------------------------------
| |
|_____________________________________|
I don't know how your system works, but I assume you know when a user is logged in right? Probably store their id in a session yeah? Well when you're inserting the logbook record, all you need to do is parse through their user ID in addition:
INSERT INTO logbook (id, user_id, .....) VALUES (NULL, $THE_USER_ID_FROM_SESSION, .....)
The above is pseudo code, you'd need to sanitize the input and actually assign the user id to a variable.
Now for fetching the user-specific information, all you need to do is add a simple WHERE clause:
SELECT id,column_1,column_2,... FROM logbook WHERE `user_id` = $THE_USER_ID_FROM_SESSION
The above is pseudo code, you'd need to sanitize the input and actually assign the user id to a variable.
There are a few questions I have, how much reading/writing are you going to be doing to the table? How are your tables set up?

How can I connect submitted form data with specific page using phpMyAdmin and PHP?

I have a client who wants to have a comment box on each page of her website. The thing is, the website has already been created as a static html site. I've made comment boxes in the past using php and phpMyAdmin, and obviously I'll change each of her pages to .php instead of .html so it'll support my comment box's php code, but I've only ever created comment boxes for a single page before. The website I'm altering now has at least 10 pages that need a comment box, and more will be added in the future.
I like the idea of using a singular database table for all the comments that are submitted on the site (there won't be a high volume). I'm just not sure how to tell the submitted comment which page it's supposed to display on.
I think what I need to do is have the submitted comment store what page it's from in the database, and then I can have a function on each page that looks for all the comments with that particular page's name or id. I'm just not sure how to do the first part where I send in data that identifies the page with the rest of the comment box's data, can anyone point me in the right direction?
Sorry if this a silly question, I'm still pretty new at PHP, thanks!!
you're on the right track with your idea. There are multiple ways of doing it, but a simple solution would be to have a page name field in your database table. when gathering the information for db insertion retrieve the page by putting a hidden field in the comments form, maybe something like...
<input type="hidden" name="page_name" value="<?=$_SERVER['REQUEST_URI']?>" />
then upon fetching comments from the database do a db query like
"SELECT * FROM `comments` WHERE `page_name` = '".$_SERVER['REQUEST_URI']."'";
Always remember to sanitize user information before db insertion!
Whay you need is a tablewith a column that identifies the page you need. For instance:
Table: Comments
---------------------------------
id | page | comment | createdAt |
---------------------------------
1 | 1 | Com1 | 2014-01-01 00:00:00|
2 | 2 | Com2 | 2014-01-01 00:00:22|
3 | 1 | Com3 | 2014-01-01 00:30:00|
4 | 1 | Com4 | 2014-01-01 00:35:00|
Then at the specific page use the column page in your where clause to select only the comments from that page.
For instance:
SELECT comment, createdAt FROM comments WHERE page = '1' ORDER BY createdAt DESC
The ORDER BY will rearrange the order with the newest on top.
When posting to the database insert the page identifier aswell.
INSERT INTO comments (page, comment, createdAt) VALUES ('1','this is a comment', NOW())
Get it?

How to store an array in a database whilst sticking to normalization?

I'm creating a website where people can share and upload things, but I want to add a like/recommend button. I want to check that if the user has recommended something or not, so they can't spam click it. I had an idea of how to do it: I had a column called 'recby' for each post and I would store a list of users who had recommended that post separated by commas. Then I learned about normalisation.
How would I do this whilst sticking to normalisation?
You add a table storing the likes for you:
PostID | UserID
---------------
5 | 6
5 | 15
6 | 8
Something like this. Now you have one entry for each like while keeping everything normalized.

Categories