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 .= ','; } } Question:
The best way to insert multiple values &amp;ZeroWidthSpace;&amp;ZeroWidthSpa ...

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.'']); }

In this example I try to make a query to insert all the values of your variable $book_array

<php $q = 'INSERT INTO table (col1,col2,col3) VALUES '; $count = count($book_array); for($i = 0; $i < $count; $i++) { $q .= "( {$book_array[$i][0]}, {$book_array[1]}, {$book_array[2]} )"; // add a comma if it's not the last if( $i !== ($count-1) ) $q .= ','; } // now $q is your query.

I did not test it.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

本文标题:The best way to insert multiple values &amp;ZeroWidthSpace;&amp;ZeroWidthSpa ...

技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(19)