Samvh Posted July 7, 2011 Share Posted July 7, 2011 Hello, I'm trying to insert multiple data into 1 field. My table is called 'members' with a couple of fields in it, One of them is friends. So I've made a little community system where you can add a friend. The problem is that I'm trying to insert the id's from the friends into 1 field. for example: 1,2,3,4,... So later on I can select all the ID's from that field and display them. At the moment I simply use: (UPDATE members SET friends='$friend_id' WHERE id='$userid'). But everytime I click on "add friend" it replaces ID's (pretty normal). I heard it's possible using arrays. (I dont know alot about arrays so keep it simple ) Thanks in advance Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/ Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 well if you want to store them as an array in one row, use serialize and unserialize However I would recommend that you insert each id separately, but i'm not sure exactly how your table is set up Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239638 Share on other sites More sharing options...
Samvh Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks for the quick response. My table is pretty basic id - fullname - country - email - password - friends 1 - Ben Vletren - Belgium - [email protected] - 123 - 0 So as you can see under friends is 0. If you add a friend it should become the id of that friend (example 5), add another friend (with id 12) and it should become 5, 12 (and so one). If there's a better way to do this please let me know. Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239644 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2011 Share Posted July 7, 2011 You'll want another table to store the friends relationships. Storing multiple pieces of data in one field makes querying against that data rather a pain. Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239648 Share on other sites More sharing options...
Samvh Posted July 7, 2011 Author Share Posted July 7, 2011 A simple table like? : ID - Friend 1 2 4 6 3 4 9 5 Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239652 Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 yes something like that, more information can be added but that is the basic concept...normally using the serialization methods with databases is a pain in the butt Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239655 Share on other sites More sharing options...
Samvh Posted July 7, 2011 Author Share Posted July 7, 2011 Oke, problem solved. Thanks for the helpful responses! Link to comment https://forums.phpfreaks.com/topic/241335-inserting-multiple-data-into-1-field/#findComment-1239658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.