title: PHP: insert array with seperate lines into database as values
body: <p>I am passing an array through ajax to PHP where I want to insert them as a row in mysql. Is there a way i can do that?</p>
  <p>Javascript:</p>
  <pre><code>var textarray = [];
  for(var i = 0; i&lt;data.length; i++){
    var sms = $('#TxtMessage').val();
    var brand = data[i].brand;
    var type = data[i].type;
    var csvLine = `${sms}, ${brand}, ${type} `;
    textarray.push(csvLine)
  }
  
  var resultCSV = textarray.join('\n');
  
  //Pass the array to PHP
  $.ajax({
    type: &quot;POST&quot;,
    url: &quot;../php/functions.php&quot;,
    data: { results: resultCSV },
    success: function(data) {
         console.log(data);
    }
  });
  </code></pre>
  <p>In PHP i would like to insert each row into mysql database.
  The values I pass to PHP look like this</p>
  <pre><code>sample text, Toyota, 30/10/2008 10:00:00 
  sample text, Nissan, 17/02/2021 14:00:00 
  </code></pre>
  <p>so each row of the text should go in as a row into the database.</p>
  <p>PHP:</p>
  <pre><code>$myArray = $_POST['results'];
  $columns = implode(&quot; &quot;,array_keys($myArray));
  $escaped_values = array_map('mysql_real_escape_string', array_values($myArray));
  $values  = implode(&quot;, &quot;, $escaped_values);
  $sql = &quot;INSERT INTO `databasetable`($columns) VALUES ($values)&quot;;
  </code></pre>