pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<small id='zTqMH'></small><noframes id='zTqMH'>

    <legend id='zTqMH'><style id='zTqMH'><dir id='zTqMH'><q id='zTqMH'></q></dir></style></legend>

      <bdo id='zTqMH'></bdo><ul id='zTqMH'></ul>

    1. <i id='zTqMH'><tr id='zTqMH'><dt id='zTqMH'><q id='zTqMH'><span id='zTqMH'><b id='zTqMH'><form id='zTqMH'><ins id='zTqMH'></ins><ul id='zTqMH'></ul><sub id='zTqMH'></sub></form><legend id='zTqMH'></legend><bdo id='zTqMH'><pre id='zTqMH'><center id='zTqMH'></center></pre></bdo></b><th id='zTqMH'></th></span></q></dt></tr></i><div class="9dblbpz" id='zTqMH'><tfoot id='zTqMH'></tfoot><dl id='zTqMH'><fieldset id='zTqMH'></fieldset></dl></div>
      <tfoot id='zTqMH'></tfoot>

        如何將多個(gè)數(shù)組插入到數(shù)據(jù)庫中?

        How to insert multiple arrays into a database?(如何將多個(gè)數(shù)組插入到數(shù)據(jù)庫中?)

        <small id='V4LmW'></small><noframes id='V4LmW'>

            <bdo id='V4LmW'></bdo><ul id='V4LmW'></ul>

              <legend id='V4LmW'><style id='V4LmW'><dir id='V4LmW'><q id='V4LmW'></q></dir></style></legend>

              1. <tfoot id='V4LmW'></tfoot>
                <i id='V4LmW'><tr id='V4LmW'><dt id='V4LmW'><q id='V4LmW'><span id='V4LmW'><b id='V4LmW'><form id='V4LmW'><ins id='V4LmW'></ins><ul id='V4LmW'></ul><sub id='V4LmW'></sub></form><legend id='V4LmW'></legend><bdo id='V4LmW'><pre id='V4LmW'><center id='V4LmW'></center></pre></bdo></b><th id='V4LmW'></th></span></q></dt></tr></i><div class="9rpr7h3" id='V4LmW'><tfoot id='V4LmW'></tfoot><dl id='V4LmW'><fieldset id='V4LmW'></fieldset></dl></div>
                    <tbody id='V4LmW'></tbody>
                  本文介紹了如何將多個(gè)數(shù)組插入到數(shù)據(jù)庫中?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我從表單中獲取一些數(shù)據(jù)作為數(shù)組.每個(gè) $_POST 值本身就是一個(gè)數(shù)組:

                  I get some data from a form as arrays. Each $_POST value is an array itself:

                  //Example snippet from my code; I have some more data/arrays
                  $department_name = ($_POST[department_name]);
                  $participant_name = ($_POST[participant_name]);
                  $activity = ($_POST[activity]);
                  $location = ($_POST[location]);
                  

                  現(xiàn)在我知道我可以使用 foreach 循環(huán)來遍歷這些數(shù)組中的每一個(gè)并將值一個(gè)一個(gè)地插入到我的數(shù)據(jù)庫中:

                  Now I know that I could use a foreach loop to loop over each of these arrays and insert the values one by one into my database:

                  foreach($department_name as $department) {
                      $query  = "INSERT INTO location_info (`department`) VALUES ('{$department}')";
                      $result = mysqli_query($connection, $query);
                  }
                  

                  這似乎是我所有 35 個(gè) POST 變量的大量代碼以及服務(wù)器的大量工作.更重要的是,我將如何對(duì)齊"每條數(shù)據(jù)?由于循環(huán),它會(huì)在每次迭代時(shí)在數(shù)據(jù)庫內(nèi)創(chuàng)建一個(gè)新行,并為所有其他列保留空白字段.

                  This seems like a lot of code for all my 35 POST variables as well as a lot of work for the server. More importantly how would I go about to "align" each piece of data? Because of the loop it would create a new row inside the database each iteration and leave blank fields for all other columns.

                  所以我搜索了如何一次遍歷多個(gè)數(shù)組并找到了這個(gè)解決方案:

                  So I searched how I could loop through multiple arrays at once and found this solution:

                  <?php
                  
                      $ZZ = array('a', 'b', 'c', 'd');
                      $KK = array('1', '2', '3', '4');
                  
                      foreach($ZZ as $index => $value) {
                          echo $ZZ[$index] . $KK[$index];
                          echo "<br/>";
                      }
                  
                  ?>
                  

                  但我真的不明白這是如何工作的以及如何將其應(yīng)用于我的代碼?

                  But I don't really understand how this works and how I can apply this to my code?

                  基本上作為一個(gè)例子,我有多個(gè)數(shù)組,如:

                  Basically as an example I have multiple arrays like:

                  $department_name = array("A", "B", "C");
                  $participant_name = array(1, 2, 3);
                  

                  我需要像這樣將它們插入到我的數(shù)據(jù)庫中:

                  And I need to insert them into my database like this:

                  INSERT INTO location_info (`department`, `participant`) VALUES ('A', 1);
                  INSERT INTO location_info (`department`, `participant`) VALUES ('B', 2);
                  INSERT INTO location_info (`department`, `participant`) VALUES ('C', 3);
                  

                  所以我想我必須使用 foreach 循環(huán)并一次循環(huán)遍歷所有數(shù)組以逐行插入數(shù)據(jù),但我看不到如何將上面找到的代碼應(yīng)用于我的代碼?

                  So I think I have to use a foreach loop and loop over all arrays at once to insert the data row by row, but I fail to see how I can apply the code I found above to my code?

                  經(jīng)過一些有用的評(píng)論后,我切換到 PDO 并取得了一些進(jìn)展.

                  After some helpful comments I switched to PDO and made some progress.

                  我當(dāng)前的代碼如下所示.

                  My current code looks like the following.

                  連接:

                  <?php 
                  
                      try {
                          $dsn = 'mysql:host=localhost;dbname=assessment';
                          $db = new PDO($dsn,  'xxx', 'xxx');
                      } catch (Exception $e) {
                          $error = $e->getMessage();
                      }
                  
                  ?>
                  

                  再往下看:

                  try {
                  
                      $sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries`, `non_rec_injuries_timeframe`, `competitor`, `cost_per_pair`, `usage_rate`, `leakage`, `cost_of_productivity`, `non_rec_impact`, `non_rec_sprain`, `non_rec_puncture`, `non_rec_dermatitis`, `non_rec_infection`, `non_rec_burns`, `non_rec_cuts`, `rec_impact`, `rec_sprain`, `rec_puncture`, `rec_dermatitis`, `rec_infection`, `rec_burns`, `rec_cuts`, `condition`, `general_id`)
                          VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries_timeframe, :competitor, :cost_per_pair, :usage_rate, :leakage, :cost_of_productivity,:non_rec_impact, :non_rec_sprain, :non_rec_puncture, :non_rec_dermatitis, :non_rec_infection, :non_rec_burns, :non_rec_cuts, :rec_impact, :rec_sprain, :rec_puncture, :rec_dermatitis, :rec_infection, :rec_burns, :rec_cuts, :condition, :general_id)";
                  
                      $stmt = $db->prepare($sql);
                  
                  for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) {
                  
                    $loc_info = array(':department' => $_POST["department_name"][$i],
                                      ':participant' => $_POST["participant_name"][$i],
                                      ':activity' => $_POST["activity"][$i],
                                      ':location' => $_POST["location"][$i],
                                      ':rec_injuries' => $_POST["injuries"][$i],
                                      ':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],
                                      ':non_rec_injuries' => $_POST["non_rec_injuries"][$i],
                                      ':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],
                                      ':competitor' => $_POST["competitor"][$i],
                                      ':cost_per_pair' => $_POST["cost_per_pair"][$i],
                                      ':usage_rate' => $_POST["usage_rate"][$i],
                                      ':leakage' => $_POST["leakage"][$i],
                                      ':cost_of_productivity' => $_POST["cost_of_productivity"][$i],
                                      ':non_rec_impact' => $_POST["non_rec_impact"][$i],
                                      ':non_rec_sprain' => $_POST["non_rec_sprain"][$i],
                                      ':non_rec_puncture' => $_POST["non_rec_puncture"][$i],
                                      ':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],
                                      ':non_rec_infection' => $_POST["non_rec_infection"][$i],
                                      ':non_rec_burns' => $_POST["non_rec_burns"][$i],
                                      ':non_rec_cuts' => $_POST["non_rec_cuts"][$i],
                                      ':rec_impact' => $_POST["impact"][$i],
                                      ':rec_sprain' => $_POST["sprain"][$i],
                                      ':rec_puncture' => $_POST["puncture"][$i],
                                      ':rec_dermatitis' => $_POST["dermatitis"][$i],
                                      ':rec_infection' => $_POST["infection"][$i],
                                      ':rec_burns' => $_POST["burns"][$i],
                                      ':rec_cuts' => $_POST["cuts"][$i],
                                      ':condition' => $_POST["condition"][$i],
                                      ':general_id' => $_POST["id"][$i]
                              );
                  
                      $stmt->execute($loc_info);      
                  }
                  
                  } catch (Exception $e) {
                      $error = $e->getMessage();
                  }
                  

                  但這仍然不起作用.

                  想法?數(shù)組里面不能放數(shù)組嗎?

                  Thoughts? Can I not put an array inside an array?

                  推薦答案

                  經(jīng)過幾次嘗試和錯(cuò)誤以及@Rizier123 的一些幫助后,好的,這里是答案:

                  Ok after a few trial and errors and some help from @Rizier123, here is the answer:

                  表單頁面上的html

                  為了清楚起見,我試圖弄清楚如何將幾個(gè)數(shù)據(jù)數(shù)組添加到我的數(shù)據(jù)庫中.所以在動(dòng)態(tài)表單頁面上,我的輸入類似于:

                  For clarity sake, I was trying to figure out how add several arrays of data to my db. So on dynamic form page, I have inputs similar to:

                  <p>Location: <input type='text' name='location[]'  > </p>
                  

                  處理表單并將數(shù)據(jù)輸入數(shù)據(jù)庫

                  首先,我從 mysqli 切換到 PDO,然后我用以下代碼運(yùn)行:

                  First, I switched from mysqli to PDO, then I ran with the following code:

                  try {
                          $sql = "INSERT INTO location_info (`department`, `participant`, `activity`, `location`, `rec_injuries`, `rec_injuries_timeframe`, `non_rec_injuries`, `non_rec_injuries_timeframe`, `competitor`, `cost_per_pair`, `usage_rate`, `leakage`, `cost_of_productivity`, `non_rec_impact`, `non_rec_sprain`, `non_rec_puncture`, `non_rec_dermatitis`, `non_rec_infection`, `non_rec_burns`, `non_rec_cuts`, `rec_impact`, `rec_sprain`, `rec_puncture`, `rec_dermatitis`, `rec_infection`, `rec_burns`, `rec_cuts`, `condition`, `general_id`)
                          VALUES (:department, :participant, :activity, :location, :rec_injuries, :rec_injuries_timeframe, :non_rec_injuries, :non_rec_injuries_timeframe, :competitor, :cost_per_pair, :usage_rate, :leakage, :cost_of_productivity,:non_rec_impact, :non_rec_sprain, :non_rec_puncture, :non_rec_dermatitis, :non_rec_infection, :non_rec_burns, :non_rec_cuts, :rec_impact, :rec_sprain, :rec_puncture, :rec_dermatitis, :rec_infection, :rec_burns, :rec_cuts, :condition, '{$id}')";
                  
                          $stmt = $db->prepare($sql);
                  
                  for($i = 0, $l = count($_POST["department_name"]); $i < $l; $i++) { 
                  
                      $loc_info = array(':department' => $_POST["department_name"][$i],
                                          ':participant' => $_POST["participant_name"][$i],
                                          ':activity' => $_POST["activity"][$i],
                                          ':location' => $_POST["location"][$i],
                                          ':rec_injuries' => $_POST["injuries"][$i],
                                          ':rec_injuries_timeframe' => $_POST["injury_time_frame"][$i],
                                          ':non_rec_injuries' => $_POST["non_rec_injuries"][$i],
                                          ':non_rec_injuries_timeframe' => $_POST["non_rec_injury_timeframe"][$i],
                                          ':competitor' => $_POST["competitor"][$i],
                                          ':cost_per_pair' => $_POST["cost_per_pair"][$i],
                                          ':usage_rate' => $_POST["usage_rate"][$i],
                                          ':leakage' => $_POST["leakage"][$i],
                                          ':cost_of_productivity' => $_POST["cost_of_productivity"][$i],
                                          ':non_rec_impact' => $_POST["non_rec_impact"][$i],
                                          ':non_rec_sprain' => $_POST["non_rec_sprain"][$i],
                                          ':non_rec_puncture' => $_POST["non_rec_puncture"][$i],
                                          ':non_rec_dermatitis' => $_POST["non_rec_dermatitis"][$i],
                                          ':non_rec_infection' => $_POST["non_rec_infection"][$i],
                                          ':non_rec_burns' => $_POST["non_rec_burns"][$i],
                                          ':non_rec_cuts' => $_POST["non_rec_cuts"][$i],
                                          ':rec_impact' => $_POST["impact"][$i],
                                          ':rec_sprain' => $_POST["sprain"][$i],
                                          ':rec_puncture' => $_POST["puncture"][$i],
                                          ':rec_dermatitis' => $_POST["dermatitis"][$i],
                                          ':rec_infection' => $_POST["infection"][$i],
                                          ':rec_burns' => $_POST["burns"][$i],
                                          ':rec_cuts' => $_POST["cuts"][$i],
                                          ':condition' => $_POST["condition"][$i] );
                  
                  $stmt->execute($loc_info);
                  }
                  

                  這篇關(guān)于如何將多個(gè)數(shù)組插入到數(shù)據(jù)庫中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                  <i id='hlX9P'><tr id='hlX9P'><dt id='hlX9P'><q id='hlX9P'><span id='hlX9P'><b id='hlX9P'><form id='hlX9P'><ins id='hlX9P'></ins><ul id='hlX9P'></ul><sub id='hlX9P'></sub></form><legend id='hlX9P'></legend><bdo id='hlX9P'><pre id='hlX9P'><center id='hlX9P'></center></pre></bdo></b><th id='hlX9P'></th></span></q></dt></tr></i><div class="njflzf3" id='hlX9P'><tfoot id='hlX9P'></tfoot><dl id='hlX9P'><fieldset id='hlX9P'></fieldset></dl></div>
                    <tbody id='hlX9P'></tbody>

                    <tfoot id='hlX9P'></tfoot>
                      <bdo id='hlX9P'></bdo><ul id='hlX9P'></ul>
                    • <legend id='hlX9P'><style id='hlX9P'><dir id='hlX9P'><q id='hlX9P'></q></dir></style></legend>

                        1. <small id='hlX9P'></small><noframes id='hlX9P'>

                            主站蜘蛛池模板: 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 挤出熔体泵_高温熔体泵_熔体出料泵_郑州海科熔体泵有限公司 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 深圳南财多媒体有限公司介绍| 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 电梯乘运质量测试仪_电梯安全评估测试仪-武汉懿之刻 | 模温机-油温机-电加热导热油炉-工业冷水机「欧诺智能」 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 培训中心-翰香原香酥板栗饼加盟店总部-正宗板栗酥饼技术 | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 上海小程序开发-小程序制作-上海小程序定制开发公司-微信商城小程序-上海咏熠 | 精雕机-火花机-精雕机 cnc-高速精雕机-电火花机-广东鼎拓机械科技有限公司 | 河南包装袋厂家_河南真空袋批发价格_河南服装袋定制-恒源达包装制品 | 传动滚筒_厂家-淄博海恒机械制造厂 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | 据信,上课带着跳 D 体验-别样的课堂刺激感受引发网友热议 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 |