0

hello friends I want to update a cell in a table whit new content, but retaining the content that has. I need to add the new data separated whit comma or line break (is'nt relevant), preserving the data that have inside.

|id | title | referer|
|--------------------|
| 1 | post1 | google |
| 2 | post2 | yahoo  |
| 3 | post3 | bing   |
| 4 | post4 | google |

The table should look like this with new content added.

|id | title | referer           |
|-------------------------------|
| 1 | post1 | google,yahoo,bing |
| 2 | post2 | yahoo,bing,etc    |
| 3 | post3 | bing              |
| 4 | post4 | google,google     |

this is my code to insert new rows in my Database:

mysql_query("INSERT IGNORE INTO posts (id,title,referer) VALUES (null,'$q','$referer')");

What would be the code to add information in the cell "referer" whit Comma separated or line break ?

thanks in advance

4
  • 1
    I would create a new table for the referrers. As you add each referrer, use the id of the title from the 1st table as the ID for the referrers. Then join both tables to get all referrers for post 1 and use explode to display them Commented Jul 29, 2013 at 3:07
  • How can you tell from your table schema (where title has duplicates) to which row you want to add a referrer? Commented Jul 29, 2013 at 3:11
  • @Brad can you give me a Link with more information about method that you suggest to me? thanks Commented Jul 29, 2013 at 3:57
  • Jose, You want to create 2 databases. One will have the posts in them(1,2,3,4) along with an referrers ID from table 2, and the 2nd will have a list of referrers.(google, yahoo etc) each with an ID that will go into the first table.. lets use post1. everytime you get a new referrer to post1, you would add a new line in the first table with the new referrer id. That way eventually you can add them up as in Google-20 referrers instead of displaying a line with many referrers. Read up on mysql table join, php explode, foreach. Commented Jul 29, 2013 at 20:05

5 Answers 5

1

I am not sure about what you might do with how separate them with commas unless you will want to create an array of variables and concat them into a single variable and use mysql's CONCAT function to update your table in order to keep your last field data:

$referes_list = '';
foreach ($referers as $referer) {
    $referes_list .= $referer . ", ";
} 
UPDATE referers SET referer=CONCAT(referer,'$referes_list') WHERE id='$id' ;
Sign up to request clarification or add additional context in comments.

4 Comments

mysql_query('UPDATE posts SET referer = concat(referer,',',$refkey) WHERE id='$id'"); this code sends error: Parse error: syntax error, unexpected T_VARIABLE
thanks, your new update says: Invalid argument supplied for foreach()
Well you are going to need to manage that by your self and see into what you have to put in the flaw! :)
mysql_query("UPDATE posts SET referer = CONCAT(referer,',','$refkey') WHERE id='$id'"); it works! thanks
0

You'll have to forgive me if my MySQL skills aren't the greatest, but what I would do here is simply get the current value, append a comma and new value to it, then update the cell.

...Or did I misinterpret your question?

1 Comment

yes, is what I need, but this comand not works for me mysql_query("update posts set referer = referer + '$newref' where id ='$id'");
0

In mysql there isnt "comma separated type", that is just a simple string or VARCHAR, so, you will need get previous data and concatenate it like string in php and then update it.

Comments

0

I think you are looking for something like:

update t
    set referrer = (case when referer is null or referer = ''
                         then @newvalue
                         else concat(referer, ',', @newvalue)
                    end)
    where id = @id;

Comments

0

mysql_query('UPDATE tablename SET referer = concat(referer,", '.$value.'") where id="'.$id.'")'; this should do it, it's untested though.

Comments

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.