Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add data to existing data in MySQL Database

Tags:

php

mysql

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?

like image 665
Michiel Avatar asked Dec 17 '22 10:12

Michiel


2 Answers

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.

like image 70
Alexander Sulfrian Avatar answered Jan 06 '23 12:01

Alexander Sulfrian


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

like image 21
Ash Avatar answered Jan 06 '23 14:01

Ash