2017-04-05 62 views
4

添加多個行數據庫中,我有四個陣列V1,$ V2,$ V3,$ V4,爲此我做了下面的代碼,但該數據未插入:如何從數組在PHP

$max_count = max(array(count($_POST['kw_coukw']), count($_POST['combin_kws']), count($_POST['combined_kws']), count($_POST['combine_kws_city']))); 

for($cat1 = 0; $cat1 < $max_count; $cat1++) { 

    if($cat1 <= count($_POST['kw_coukw'])){ 
     $V1 = $_POST['kw_coukw'][$cat1]; 
    }else{ 
     $V1 = 0; 
    } 

    if($cat1 <= count($_POST['combin_kws'])){ 
     $V2 = $_POST['combin_kws'][$cat1]; 
    }else{ 
     $V2 = 0; 
    } 

    if($cat1 <= count($_POST['combined_kws'])){ 
     $V3 = $_POST['combined_kws'][$cat1]; 
    }else{ 
     $V3 = 0; 
    } 

    if($cat1 <= count($_POST['combine_kws_city'])){ 
     $V4 = $_POST['combine_kws_city'][$cat1]; 
    }else{ 
     $V4 = 0; 
    } 

    $qry = "INSERT INTO seo_sentence_rl_pg_create_kw (seo_sent_pg_cre_id,kw_1_id,kw_124_id,kw_1234_id,kw_1234_city_id,customized_keyword,customized_title,description) VALUES ('".$_POST['pagenm']."','".$V1."','".$V2."','".$V3."','".$V4."','".$_POST['cuskeyword']."','".$_POST['custitle']."','".$_POST['description']."')"; 

} 
+0

那可能是因爲你沒有實際發送你建立到服務器執行的查詢!你使用什麼數據庫擴展? 'mysqli_'' or'PDO' – RiggsFolly

+0

包含SQL查詢的字符串不會被神奇地處理 – RiggsFolly

+0

mysql_query($ qry) –

回答

3

使用PDO如果要插入大容量數據Hi請使用如下代碼:

$sql_insert = "INSERT INTO seo_sentence_rl_pg_create_kw (seo_sent_pg_cre_id,kw_1_id,kw_124_id,kw_1234_id,kw_1234_city_id,customized_keyword,customized_title,description) VALUES (:seo_sent_pg_cre_id, :kw_1_id, :kw_124_id, :kw_1234_id, :kw_1234_city_id, :customized_keyword, :customized_title, :description)"; 


      //Prepare our statement using the SQL query. 
      $statement = $pdo->prepare($sql_insert); 


      //Bind our values to our parameters 

      for($cat1 = 0; $cat1 < $max_count; $cat1++) { 

      if($cat1 < count($_POST['kw_coukw'])){ 
       $V1 = $_POST['kw_coukw'][$cat1]; 

       }else{ $V1 = 0; } 

       if($cat1 < count($_POST['combin_kws'])){ 
       $V2 = $_POST['combin_kws'][$cat1]; 
       }else{ $V2 = 0; } 

       if($cat1 < count($_POST['combined_kws'])){ 
       $V3 = $_POST['combined_kws'][$cat1]; 
       }else{ $V3 = 0; } 

       if($cat1 < count($_POST['combine_kws_city'])){ 
       $V4 = $_POST['combine_kws_city'][$cat1]; 
       }else{ $V4 = 0; } 

      $statement->bindValue(':seo_sent_pg_cre_id', $_POST['pagenm']); 
      $statement->bindValue(':kw_1_id', $V2); 
      $statement->bindValue(':kw_124_id', $V3); 
      $statement->bindValue(':kw_1234_id', $V4); 
      $statement->bindValue(':kw_1234_city_id', $V5); 
      $statement->bindValue(':customized_keyword', $_POST['cuskeyword']); 
      $statement->bindValue(':customized_title', $_POST['custitle']); 
      $statement->bindValue(':description', $_POST['description']); 
      $inserted = $statement->execute(); 
      } 
+0

在mysql中不可能? –

+0

如果你想使用批量插入你想使用mysqli或pdo。 – Tushar