Michiel
Michiel

Reputation: 8083

Add data to existing data in MySQL Database

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?

Upvotes: 4

Views: 8044

Answers (3)

cwallenpoole
cwallenpoole

Reputation: 82028

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 );

Upvotes: 0

Ash
Ash

Reputation: 128

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

Upvotes: 1

Alexander Sulfrian
Alexander Sulfrian

Reputation: 3563

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.

Upvotes: 11

Related Questions