简体   繁体   中英

PHP queries - How to add new column and insert user info in table, when user id is stored in another table?

so I'm new to PHP, currently I'm making an PHP OOP authentification system. The login and registration works fine, but now I need to have an option for users to add unlimited attributes(input column name and value) to his profile, but the trick is that I need to store the attributes in another database table. So this is my users table.

uid upass      fullname      uemail
1   md5(pass)  John Snow     john@gmail.com
2   md5(pass)  John Doe      jdoe@gmail.com

And i think my new table should look something like this.

id  uid  age  professsion   gender
1   1    17   haha          male
2   2    62   eee           male

and so on. This system is just for educational purposes, it probably doesn't make any sense, that user can add unlimited attributes, but well it is what needs to be done. I'm hoping someone can help. Thanks in advance.

EDIT

Ok i have made this function, but what do i have to echo out?

        public function get_property($uid)
{
        //$query = "SELECT property,value FROM info WHERE uid='$uid' AND property='Gender'";

        $query = "SELECT uid, CONCAT('{', GROUP_CONCAT(property, ': ', value), '}') AS json FROM info GROUP BY uid";

        $result = $this->db->query($query) or die($this->db->error);

        $user_data = $result->fetch_array(MYSQLI_ASSOC);
            echo $user_data['property'];
}

OH i guess it's json

I would create another table, named "users_data", with two fields. One beeing the user id and the second beeing a BLOB, containing the fields of the user which he created. BLOBs are stored in serialized form, which you then can unserialize in PHP.

Doing it this way, you dont have to edit the database, rather then just adding data to the users data array or removing it.


You can select it by using something like this:

SELECT users.*, users_data.data FROM users JOIN users_data ON users_data.user_id = users.id

IMO this approach is even simpler to accomplish then doing it the way you described.

I don't think that your proposed new table is a good choice, in particular because you mentioned that the user needs to be able to store unlimited attributes. Your suggested design places each attribute into a separate column, but this could become unwieldy as the number of attributes really starts to get large. Instead, I propose something like this:

uid | property   | value
1   | age        | 17
1   | profession | haha
1   | gender     | male
2   | age        | 62
2   | profession | eee
2   | gender     | male

Now adding a new property just means inserting a record. Note that this table can easily be queried. If you wanted to find all male users you could do:

SELECT uid
FROM yourTable
WHERE
    property = 'gender' AND value = 'male'

This design is flexible and we could index the property and value columns to improve performance.

Edit:

If you wanted to output each user's properties in a sort of JSONeqsue format, you could try using GROUP_CONCAT eg

SELECT
    uid,
    CONCAT('{', GROUP_CONCAT(property, ': ', value), '}') AS json
FROM yourTable
GROUP BY uid;

Output:

    uid json
1   1   {age: 17,profession: haha,gender: male}
2   2   {age: 62,profession: eee,gender: male}

Demo

I think you can create a json object of user and map with id in other table and save json in one column against id. You can extract json easily and decode it properly.

You can create other table for storing additional user details with a table_name like 'user_attributes' or 'user_details' (or) your wish. Then You have to store 'user_id' while storing the additional data for user So that you can fetch all the additional user information by using Mysql JOIN's.

users

uid          upass              full_name        uemail 
 1         md5('something')    James Anderson   jamesanderson@mail.com
 2         md5('123456')        Brett Lee       brettlee@mail.com

Your user_details would be like below:

id   uid      age      professsion       gender
 1    1       25        Faculty           Male
 2    2       26        Entreprenuer      Male

You can fetch the data by using any one of Mysql JOIN's like below. Mysql Joins Reference joins

SELECT users.*,user_details.age,user_details.profession FROM users INNER JOIN user_details ON users.uid=user_details.uid

If the User can input anynumber of dynamic attributes then you need to alter your table structure as below

user_tbl

uid upass      fullname      uemail
1   md5(pass)  John Snow     john@gmail.com
2   md5(pass)  Adolf Hitler  ahitler@gmail.com

user_attr_tbl [If any user inputs a new attr_name that's not existing in the table then you need to insert it to here.]

id       attr_name   
1        age              
2        professsion       
3        gender  

user_attr_values

id uid  user_attr_id    value
1   1       1            17
2   1       2            haha
3   1       3            male
4   2       1            62
5   2       2            eee
6   2       3            male

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM