简体   繁体   English

PHP查询 - 当用户ID存储在另一个表中时,如何在表中添加新列并插入用户信息?

[英]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. 所以我是PHP新手,目前我正在制作一个PHP OOP身份验证系统。 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 哦,我猜它是json

I would create another table, named "users_data", with two fields. 我将创建另一个名为“users_data”的表,其中包含两个字段。 One beeing the user id and the second beeing a BLOB, containing the fields of the user which he created. 一个是用户id,第二个是BLOB,包含他创建的用户的字段。 BLOBs are stored in serialized form, which you then can unserialize in PHP. BLOB以序列化形式存储,然后您可以在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. IMO这种方法更容易实现,然后按照您描述的方式进行。

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 如果您想以某种JSONeqsue格式输出每个用户的属性,您可以尝试使用GROUP_CONCAT例如

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. 我认为你可以创建一个用户的json对象,并在其他表中使用id映射,并将json保存在一列中以防止id。 You can extract json easily and decode it properly. 您可以轻松地提取json并正确解码。

You can create other table for storing additional user details with a table_name like 'user_attributes' or 'user_details' (or) your wish. 您可以使用table_name创建其他表来存储其他用户详细信息,例如'user_attributes'或'user_details'(或)您的愿望。 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. 然后,您必须存储'user_id',同时为用户存储附加数据,以便您可以使用Mysql JOIN获取所有其他用户信息。

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: 你的user_details如下所示:

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 JOIN中的任何一个来获取数据。 Mysql Joins Reference joins Mysql加入引用连接

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 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.] user_attr_tbl [如果任何用户输入了表中不存在的新attr_name,则需要将其插入此处。]

id       attr_name   
1        age              
2        professsion       
3        gender  

user_attr_values 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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