4

I have four arrays $V1, $V2, $V3, $V4 and for this I did the following code but the data are not inserted:

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

}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Tarun modi
  • 1,050
  • 1
  • 13
  • 30
  • Thats probably because you dont actually sent the query you built to the server for execution! What database extension are you using? `mysqli_` or `PDO` – RiggsFolly Apr 05 '17 at 10:52
  • Strings containing a SQL Query do not get magically processed – RiggsFolly Apr 05 '17 at 10:54
  • mysql_query($qry) – Tarun modi Apr 05 '17 at 10:54
  • This is also not working – Tarun modi Apr 05 '17 at 10:54
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 05 '17 at 10:55
  • @Tarunmodi if its not working then what error are you getting? – Saili Jaguste Apr 05 '17 at 10:56
  • The result print success but data was not inserted – Tarun modi Apr 05 '17 at 10:57
  • Please show us all the relevant code and all the relevant error messages. We are not clairvoyant, we cannot help with code we cannot see – RiggsFolly Apr 05 '17 at 10:57
  • Ok I will send full code – Tarun modi Apr 05 '17 at 10:59
  • 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 ('14','4','2','2','1','dqdqd','qddq','ddqddqdqd')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 ('14','','4','4','2','dqdqd','qddq','ddqddqdqd') Success But the data not inserted. – Tarun modi Apr 05 '17 at 11:06

1 Answers1

3

Hi please use following code using PDO if you want to INSERT bulk data:

$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();
            }
Tushar
  • 46
  • 5