Adding data to existing data in a 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 achieve 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?

+4
source share
3 answers

You need something like:

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

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

+11
source

use mysql update instruction. If you want to execute exeute via php, then

first get the existing value using the select statement,

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

you get a visitor to an 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 will need to use the mysql php functions to execute the above queries

+1
source

I think you better restructure. Make a table:

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

Then select everything from this 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 ); 
0
source

All Articles