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?

1 Answer 1

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. "')";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.