4

I have a table called tblActivities. There are two fields ID and Attendees.

ID       Attendees
1        Jon Jhonson
2        Ive Iveson

Which PHP function or MySQL statement do I need to use to get to this result:

ID       Attendees
1        Jon Jhonson, Ive Iveson, Adam Adamer
2        Ive Iveson

In other words, how can I add new data to existing data in my database?

1
  • You probably need to supply the linking table information, you should have another table which shows which activities were attended Commented Aug 23, 2011 at 14:22

3 Answers 3

11

You need something like:

UPDATE tblActivities
SET Attendees = CONCAT(Attendees, "Ive Iveson, Adam Adamer")
WHERE id = 1;

But maybe you should change the database layout. It is preferable to have only one value in one field. You could find more information under http://en.wikipedia.org/wiki/Database_normalization.

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

Comments

1

use mysql's update statement. If you want to exeute through php then

first get the existing value using select statement,

SELECT Attendees from <table-name> WHERE ID = 1

you will get attendees existing value, put it in a php variable, now concatenate your value.. and then update,

UPDATE <table_name>
SET Attendees=<new-value>
WHERE ID=1

you would have to use the php's mysql functions to run the above queries

Comments

0

I think you're better off restructuring. Make a table:

ID (primary key)
ACTIVITY_ID (foreign key to activity table)
ATTENDEE (foreign key to USERS table)

Then select everything from that event and concat in PHP.

$q =  mysql_query( 'SELECT ATTENDEE FROM tblActivities '.
                   'WHERE ACTIVITY_ID = $activity_id' );

$attendees = array();
while( $row = mysql_fetch_assoc( $q ) )
{
    $attendees[] = $row['attendee'];
}
$attendees =implode( ' ', $attendees );

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.