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开发工程师 软件开发流程 软件开发工程师

代码区博客精选文章
分页:12
转载请注明
本文标题:The best way to insert multiple values &amp;ZeroWidthSpace;&amp;ZeroWidthSpa ...
本站链接:https://www.codesec.net/view/628466.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(116)