Problem solved. According to the answer of Zerquix18, I made small changes and it worked like a charm.
$count = count($book_array);
$query = 'INSERT INTO book_exchange (class, book_title, isbn) VALUES ';
for ($i=0; $i<$count; $i++){
$query .= '( \''.$book_array[$i][0].'\',\''.$book_array[$i][1].'\',\''.$book_array[$i][2].'\' )';
if ($i != ($count-1)) {
$query .= ',';
}
}
I have a book table and and let users put information in the fields below. I want to insert the values into my book table. I use "for" loop to get the values each row and add it into an array. I'm gonna use "for" again to add these array into database.
For example if use fill value into the fields. I have 3 array:
book_array[0]
, book_array[1]
, book_array[2]
. Each of them contains 3 elements. I'm gonna write a for loop to insert 3 arrays into the table.
On the other hand, I think I can add all element with the same type into an array. For example I have an array for class, an array for book title and an array for isbn. then I add these array into each column in database. I think this way is faster. But I'm not sure it works. Please suggest me the best way to insert data into DB in this case. ALso please give me an example. Thanks.
for ($i=0; $i<$n; $i++) {
$book_array[$i] = array($_POST['field_class_'.$i.''],$_POST['field_book_title_'.$i.''],$_POST['field_isbn_'.$i.'']);
}