Vincent Canales Vincent Canales - 7 months ago 9
SQL Question

PHP: Insert data in MySQL every after the comma is detected

Example

Name:

John, Doe, Jane


Message:
Hi, This is a message


How can i save each data with the name separated with comma in database? output must be.

Name | Message
John | Hi, This is a message
Doe | Hi, This is a message
Jane | Hi, This is a message

Answer

Use explode function to explode and foreach to insert

 $names_all="John, Doe, Jane";
 $names=explode(",",$names_all);
 $msg = 'Hi, This is my msg';
 foreach($names as $name)
  {
   if($name!="")
    {
             //insert query using $name and $msg
    }     
  }
Comments