User database structure - php

Im not a webdev, Im learning all this stuff on the go and I stuck with this stage with my project.
I have a website people can register to use it, so I can store users already. I have a table with different items (item_id, item_name, item_owned). My problem is, every users should have the same list of item, but they can select if they have this item or not on thier own profile.
I could do it make the db and the items are selectable, the db shows 0 if you dont have it or 1 if you have it. But every user sees the same item_owned data and I need it to be user specific.
How can achive this? What structure should I use with the database? Do I need any special libraies beside HTML, PHP, SQL?
Thank you!

Related

PHP - Insert as draft

I'm building a CRM and I have a list of customers.
When I create a customer, I usually insert it as a draft as soon as the user clicks on "Insert button". Why? Because I want the user to be able to insert photos and documents on the insertion form. This way, I have the customer ID already and I can process the upload using that ID.
The problem here is that we can have multiple drafts at a certain moment.
My question is: is there any better method to do this? Without creating the draft? For instance, using a temp folder where the photos and documents are temporarily saved?
EDIT:
the issue woth having multiple drafts is that at certain point will may have a lot of trash.
I mean, this works for me ok? And it's a solution I have, I just wanted to know if there's a better solution instead of using drafts.
It depends on what exactly you are using to save the customer in the database.
If you are using an ORM such as Eloquent or Doctrine you just need to have the correct relationships between Entities and you won't need to save the Customer as draft, you can just flush all of the information at once.
If you aren't using an ORM, you can still do it in one request, just insert the Customer record first and get the last inserted ID , for example if using PDO, you can use PDO::lastInsertId https://www.php.net/manual/en/pdo.lastinsertid.php .
After you have the ID just set it to the other columns where you need it.

How to add only one data in database table in laravel

I am creating a site and i am making a general option page for the site that include the site name, logo, social links so that i can manage them dynamically from one place without changing the links and text in all pages where i called them.
What I want is that the table i created in database, i want to have only one row in it. If someone try to add other row of data it should not be added because i have to call only one data for the site.
I hope the query is clear to all.
Thanks for your support. Help me if u can. please :)
By,
if someone try to add other row of data it should not be added
what do you mean? Are you trying to avoid unauthorized access? If yes, then you need to implement roles https://laracasts.com/discuss/channels/general-discussion/roles-and-permissions-in-laravel-5
To manage your site information, I'm sure you set a form that will be used to update the site info. And from the look of things you will only be updating that row when you hit save or update button. I don't think you will be doing full CRUD here just updating so there'll be no chance of inserting a new row of data in the db.
So three things:
you want to create a migration, seed the table with the site details accordingly. run the migration
you want to make sure the user trying to access this route is authorized to do so
you want to pre populate the site info in the form fields when the edit site info page loads and you want to save the form when the authorized user changes any of the site info
This way, there'll be no question of stopping further insertions into the db.

Database Concurrency Problems

I'm currently developing a database/website server interface to facilitate inputting data for a data collection project. There are two types of additions being made to the database: A and B here. Some of the tables in the database that handle these entries are as follows:
dcs_projectname_a
dcs_projectname_b
Each of these have tables for all the required input fields in addition to things like creator, timestamp, etc.
The pages on the website facilitate three different options: add, view, and edit. Each page for each type of entry performs the respective function. That is, the add page adds, view page views, etc.
I am just about done; however, there is a major challenge I haven't really confronted yet: concurrency. There will be multiple users adding content to the database at the same time. Each entry is given its own specific id and there CANNOT be any duplicate id's. That is, the first a entry is A000001, the next is A000002, and so on.
On the add and edit pages, there is a disabled field for the user to view the id for other uses when physically documenting entries.
What I need to figure out is how to implement concurrency management so that when users are concurrently adding a's that they will not be under the same id and row.
The add page automatically generates the id to be used by accessing the database's most recent id and adding one.
My thought was to create a new row in the table every time the add page is opened and give it the calculated id. Then, when information is added it performs a modification to that existing row. This way, if another user opens the add page while another entry is currently being added it will be given the future id, not the same one.
With this method I need a way to delete this entry if the user leaves the add page or closes the browser. Then, I also need other users with open add pages to automatically update their id's to one less when the first user (or any other user less than the most id being used) leaves their add page and cancels the addition.
This is kind of a complicated explanation and if you don't understand let me know and I'll try to answer as best as I can. Any help is much appreciated!
Thanks
There's a number of solutions to your problem, but you seem to have made things harder by having your application generate the record IDs for you.
Instead, you could just be using MySQL's AUTO_INCREMENT functionality to automatically generate/increment the record ID for you (upon insert). MySQL will ensure that there are no duplicates, and you can get rid of the extra database call to retrieve the most recent ID.

How to store and retrieve an image and description from a database and make it view able by the a set member

This is a pretty long and hefty question. First off I am new to PHP but I know javascript and HTML as well as CSS. I have created the static elements of a webpage and have managed to integrate it with a login and signup premade php layout, so currently users can signup and login for the site. However the key element remains; the user should be able to select an item from the database and upon selection that item should be "pinned" to a specific div.
e.g. The user clicks on a button below "image.div", when the user clicks on this button a modal pops up with some choices, the user have to select a choice which then loads further information for the user to see. Upon getting this information the user clicks "pin" and the modal closes and that specific choice is "pinned" to "image.div". Each individual "image.div" will have it's own category, e.g. hotels, car rentals, restaurants etc. Ence when selected only the assigned category is displayed by "image.div".
The "pinned" object should only be view able by the user that pinned it when that user logs in.
I also want the choice within the popup modal to be searchable by keywords e.g. kingston, New York etc.
The database that came with the php login scripts have the following table: fgusers
The table has the following columns: id_user, name, email, phone_number, username and password.
In addition to the above information request I would also like to know how to structure the database for this, should I extend the fgusers table? or should I create separate tables for each?
A response would be greatly appreciated.
If I understand your question correctly then, the best way to achieve the effect you are after is to use AJAX to access database info via a PHP script. Then use javascript to manipulate the xmlhttp result.
As far as the MySQL database tables go, it is better to create separate tables for the different information. You will need to create a column in each table that references an identifying column in the table you want to link that particular tables information to.
Then you just have to use the JOIN commands when referencing the table data.
Example: if you want to link data to specific users you can add a username column to the table and add the users name to the data row. Later when you want to retrieve that users data, just reference the users name. If there are multiple tables of user data, use the JOIN command to link the tables together.

Let users choice modules they want

I'm making a social gaming network, for learning but mostly because php is alot of fun!
Anyway I want this future..
I wanna let my users chose which "modules" they want activated, like a message board on their profile, polls, buddy list, contact box. Some modules will have to be purchased with points.
Im thinking on something like this:
id
username
passwd
points
msgboard_activated
poll_activated
buddylist_activated
contactbox_activated
Just check if msgboard_activated equals to 1, then include that module on members profile
Some modules (feel free to come with suggestions of modules):
Message board (lets users leave messages on a specific users page)
Buddy list
Poll
I pretty much got the idea from (awesome site!):
http://www.fpsbanana.com/modules
Anyway, Let me hear your thought and idea of this!
Thanks alot and have a good day!
ps.(im BR so expect spelling errors etc)
Oie, Agamemon!
I would encourage you to try a different structure. For instance, three tables, as opposed to a single table.
First, your users table which represents everybody on your site. Second, a modules table which represents all possible modules. This way you can add more in the future without having to add columns to the user table, etc. And lastly we bring them together into a relationship with the usersmodule table, which shows what modules belong to each particular user.
I hope this gives you some direction.
table:users
userid
username
...
table:modules
moduleid
modulename
...
table:usermodules
userid
moduleid
...

Categories