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 Quote 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 Quote 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 - ben@hotmail.com - 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. Quote 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. Quote 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 Quote 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 Quote 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! Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.