3

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");
6
  • 1
    Please show your code Commented Jun 9, 2014 at 11:29
  • Insert into user(Name,UserName,Password,Valuie)Values("bla bla","blabla","blabla","here's 1st value , here's second"); Commented Jun 9, 2014 at 11:34
  • You would have another table for the wish list. This table would have zero or more rows per user, one for each wish list item for that user. Commented Jun 9, 2014 at 11:35
  • now i have seperated 2 values with single comma how php will display 1st value somewhere and 2nd value somewhere Commented Jun 9, 2014 at 11:36
  • 2
    Comma separated values in a field are a sign of a poorly normalised database. If you split it into 2 tables you can join them together to get the value. When you add a new wish list item you just insert a record without caring too much how many already exist for that user (as you won't be updating the user table) Commented Jun 9, 2014 at 11:37

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.... :)

Comments

0

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.

6 Comments

thats cool but how i will tell that this value is for this user
You can add that field in the same database table. Like: Name,UserName,Password,Value1, Value2. Or you can create a separate table just for wishlist with Username as Primary Key.
This isn't a scalable solution.
with this thing i have to manage 10 or more coloumns for only values it going to make my database a hell
This will work only if you have two or three Value columns. If your wishlist is more than that. In that case, use separate table with Username as primary key and Value as second column.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.