1

I have an array which I need to insert into multiple rows of database. The structure of array is like:

$var = "Name1,Age1,DOB1,Relation1.Name2,Age2,Dob2,Relation2.";//And so on, depending on users input

(Dot indicates new line whereas Comma indicates new column) I need to insert it into database like this:

I first stored all rows in an array like:

$rowsToInsert = explode (".",$var);

I have now:

$rowsToInsert[0] = Name1,Age1,DOB1,Relation1;
$rowsToInsert[1] = Name2,Age2,DOB2,Relation2; 
...And So on...

Problem:

What is the fastest way to store these array elements into database having Name, Age, DOB, Relation columns?

Ingila Ejaz
  • 233
  • 3
  • 6
  • 18

1 Answers1

1

May be this will work

$rows = explode (".",$var);
$addslash = addslashes($rows);

foreach($addslash as $val) {
    $val_str = str_replace("," ,"','", $val);
    $sql = "INSERT INTO tablename (Name, Age, DOB, Relation) VALUES ('" .$val_str. "')";
}
Darren
  • 13,050
  • 4
  • 41
  • 79
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59