Waqas Tahir
Waqas Tahir

Reputation: 8242

Multiple values for one column PHP and SQL

I'm new to SQL and PHP. Maybe I don't understand what you will answer, so please explain it fully that I can understand so here's my question

I have 5 columns: Id, Name, UserName, Password, valuie

You can understand 4 columns, the fifth is valuie means what the user want to give in wish list. Now when user select a thing and add into wish list, that's good but when he/she adds 2 things in his wish list, how to put that in valuie? how can I display 1st and 2nd value? I mean if I want to display 1st one and if 2nd and if both, what I can do about it?

My PHP is good but MySQL is not good....

Code

Insert into user(Name, UserName, Password, Valuie)
Values("bla bla", "blabla", "blabla", "here's 1st value", "here's second");

Upvotes: 3

Views: 2850

Answers (3)

Amrinder Singh
Amrinder Singh

Reputation: 5492

you can use explode function for it, here is a example below, which will make you easier to understand.. suppose you have a field in your db as value=(value1,value2)

now you can fetch both of the these values one by one as following..

    $data=explode(',',value);

    //An Explode function gives you an array, 
    //by which you can get any desired value just by passing it's index.

    $data1=$data[0];

    $data2=$data[1];

Hopefully this would help you. Thanks.... :)

Upvotes: 1

user399666
user399666

Reputation: 19879

This seems like something that should be handled by a one-to-many relationship. A user can have many items in his wishlist. This means that you will need to split your current table up into two. Example: A user table and a wish list table:

user: id, name, etc.

wishlist: id, item_name, user_id

Whenever the user adds a new wish list item, it should be added to the wishlist table, keyed by his/her user_id.

Seeing as you are new to MySQL, you should make sure that you read up on the concept of database normalization.

Upvotes: 2

Ankit
Ankit

Reputation: 130

For that you can use simple technique. Create two columns Value1 and Value2. If user select first value then store it in Value1 and when user select second the store this value in Value2. Before performing this check if the Value1 field is Null in Database. If not then put that value to Value2.

You can also display both values on different location, if you want.

Upvotes: 0

Related Questions