0

I want to implode a textarea name as comma seperated to store in database. I am not getting a exact solution.

HTML code :

<div class="form-group row">
     <label for="input-1" class="col-sm-4 col-form-label">Textarea</label>
     <div class="col-sm-8"> 
          <textarea name="reg_desc" class="form-control" rows="5"></textarea>
     </div>    
</div>   

PHP Code :

$reg_desc = implode(',', $_POST['reg_desc']);

SQL Query :

$sql="INSERT into `regions` (`reg_desc`) VALUES  ('$reg_desc')";

Exactly if i want to insert a record in textarea like (see the screen shot)

enter image description here

It will store with comma seperated in database.

sixthstar
  • 75
  • 1
  • 1
  • 9
  • `comma seperated in database` is not how a db should be set up. https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad If the question is just about PHP processing of CSVs see https://www.php.net/manual/en/function.str-getcsv.php – user3783243 Jan 09 '20 at 03:45
  • Regardless what you are doing with your database... `$_POST['reg_desc']` will not contain an `Array` from just a single textarea. – IncredibleHat Jan 09 '20 at 03:46
  • 2
    What you probably want is `implode(',', explode("\n", $_POST['reg_desc']))` – Barmar Jan 09 '20 at 03:48

1 Answers1

1

For your solution, you have to split string with new line then replace with comma when you find new line.

implode(',', explode("\n", $_POST['reg_desc']))

Madhuri Patel
  • 1,270
  • 12
  • 24