"4 arrays simultaneously write to the database how to write?" 】

Source: Internet
Author: User
$_post[' A '] value is 1,2,3,4,5
$_post[' B '] value is a,b,c,d,e
$_post[' C ' value is male, female, female, male, male
$_post[' d '] value is 25,26,27,26,26


These four arrays are also In the back. Now I'm going to write them all into the database, how should the loops be written?

The last write to the database format should be
Number name Gender age
Article I: 1 a male 25
Article two: 2 B female 26
Article three: 3 C female 27
Fourth: 4 D male 26
Fifth article: 5 e male 26


Reply to discussion (solution)

$_post[' a '] = array (1,2,3,4,5); $_post[' B '] = array (' A ', ' B ', ' C ', ' d ', ' e '); $_post[' C '] = array (' Male ', ' female ', ' female ', ' male ', ' male '); $_ post[' d '] = Array (25,26,27,26,26), foreach ($_post[' a '] as $i = = $v) {  $sql = "Inser into tbl_name values (' $v ', ' {$_po st[' B ' [$i]} ', ' {$_post[' C ' [$i]} ', ' {$_post[' d ' [$i]} ') ";  Echo $sql, Php_eol;}
Inser into tbl_name values (' 1 ', ' a ', ' Male ', ' 25 ')
Inser into tbl_name values (' 2 ', ' B ', ' Female ', ' 26 ')
Inser into tbl_name values (' 3 ', ' C ', ' Female ', ' 27 ')
Inser into tbl_name values (' 4 ', ' d ', ' Male ', ' 26 ')
Inser into tbl_name values (' 5 ', ' e ', ' Male ', ' 26 ')

$_post[' a '] = ' 1,2,3,4,5 '; $_post[' B '] = "a,b,c,d,e"; $_post[' C '] = "male, female, female, male, male"; $_post[' d '] = "25,26,27,26,26"; $a = Explode (', ', ' $_post[' a ']), $b = Explode (', ', $_post[' B ')), $c = Explode (', ', $_post[' C ')), $d = Explode (', ', $_post[' d ')); foreach ($a as $key = + $value) {    $sql = "Inser into tbl_name values (' $value ', ' {$b [$key]} ', ' {$c [$key]} ', ' {$d [$key]} ');";    mysql_query ($sql);    Echo $sql, Php_eol;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.