php - 如何在php中从数据库中的数组中添加多行

标签 php mysql arrays database string

我有四个数组 $V1、$V2、$V3、$V4,为此我执行了以下代码,但未插入数据:

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

}

最佳答案

您好,如果您想插入批量数据,请使用 PDO 使用以下代码:

$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();
            }

关于php - 如何在php中从数据库中的数组中添加多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228966/

相关文章:

PHP 登录表单 mysql 和 bootstrap 风格给出错误

php - 我应该使用 .或者,在 PHP 短标签 echo 中分隔变量?

php - OpenCart 的新页面

mysql - 使用MYSQL删除字符串中不需要的字符

php - 在 MySql 中增加 int 值

javascript - 验证数组中是否存在元素

arrays - Delphi - TStack 容量困惑

php - 将文件夹路径替换为html内容

javascript - 页面加载时选中的复选框

java - 如何正确合并两个已排序的数组?