データベースに配列を挿入します

空中ブランコ

データベースに配列を挿入する必要があります。私は次のような形をしています

<form action="" method="post">
<p>servicename</p>
<input type="text" name="service_name"><br>

<p>bgname</p>
<input type='text' name="background[]">
<input type='text' name="background[]">
<input type='text' name="background[]">

<p>bg price:</p>
<input type='text' name="background_price[]">
<input type='text' name="background_price[]">
<input type='text' name="background_price[]">

<p>resolution:</p>
<input type='text' name="resolution[]">
<input type='text' name="resolution[]">

<p>resolution price</p>
<input type='text' name="resolution_price[]">
<input type='text' name="resolution_price[]">

<p>count</p>
<input type='text' name="count[]">
<input type='text' name="count[]">
<input type='text' name="count[]">
<input type='text' name="count[]">
<input type='text' name="count[]">

<p>count price</p>
<input type='text' name="count_price[]">
<input type='text' name="count_price[]">
<input type='text' name="count_price[]">
<input type='text' name="count_price[]">
<input type='text' name="count_price[]">

<p>Type:</p>
<input type='text' name="type[]">
<input type='text' name="type[]">
<input type='text' name="type[]">

<p>Type price</p>
<input type='text' name="type_price[]">
<input type='text' name="type_price[]">
<input type='text' name="type_price[]">

<p>how</p>
<input type='text' name="how[]">
<input type='text' name="how[]">

<p>how price</p>
<input type='text' name="how_price[]">
<input type='text' name="how_price[]">
<input type="submit" name="submit">
</form>

また、動作するスクリプトを作成しましたが、不要なテーブルが多すぎて、1つのテーブルに最適化したいと思います

<?php
class Service extends Connection {
public function addService($service_name, $background, $background_price, $resolution, $resolution_price, $count, $count_price, $type, $type_price, $how, $how_price) {
        try {
            $sth = $this->dbh->prepare("INSERT IGNORE INTO services (id, name) VALUES ('', ?)");
            $sth->bindParam(1, $service_name, PDO::PARAM_STR);
            $sth->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        foreach($background as $index1 => $backgroundItem) {
            try {
                $sth = $this->dbh->prepare("INSERT IGNORE INTO background (id, service_name, background, background_price) VALUES ('', ?, ?, ?)");
                $sth->bindParam(1, $service_name, PDO::PARAM_STR);
                $sth->bindParam(2, $backgroundItem, PDO::PARAM_STR);
                $sth->bindParam(3, $background_price[$index1], PDO::PARAM_INT);
                $sth->execute();
            } catch (PDOException $e) {
                echo $e->getMessage;
            }
        }

        foreach($resolution as $index2=> $resolutionItem) {
            try {
                $sth = $this->dbh->prepare("INSERT IGNORE INTO resolution (id, service_name, resolution, resolution_price) VALUES ('', ?, ?, ?)");
                $sth->bindParam(1, $service_name, PDO::PARAM_STR);
                $sth->bindParam(2, $resolutionItem, PDO::PARAM_STR);
                $sth->bindParam(3, $resolution_price[$index2], PDO::PARAM_INT);
                $sth->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }

        foreach($count as $index3=> $countItem) {
            try {
                $sth = $this->dbh->prepare("INSERT IGNORE INTO counts (id, service_name, count, count_price) VALUES ('', ?, ?, ?)");
                $sth->bindParam(1, $service_name, PDO::PARAM_STR);
                $sth->bindParam(2, $countItem, PDO::PARAM_STR);
                $sth->bindParam(3, $count_price[$index3], PDO::PARAM_INT);
                $sth->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }

        foreach($type as $index4=> $typeItem) {
            try {
                $sth = $this->dbh->prepare("INSERT IGNORE INTO type (id, service_name, type, type_price) VALUES ('', ?, ?, ?)");
                $sth->bindParam(1, $service_name, PDO::PARAM_STR);
                $sth->bindParam(2, $typeItem, PDO::PARAM_STR);
                $sth->bindParam(3, $type_price[$index4], PDO::PARAM_INT);
                $sth->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }

        foreach($how as $index5=> $howItem) {
            try {
                $sth = $this->dbh->prepare("INSERT IGNORE INTO how (id, service_name, how, how_price) VALUES ('', ?, ?, ?)");
                $sth->bindParam(1, $service_name, PDO::PARAM_STR);
                $sth->bindParam(2, $howItem, PDO::PARAM_STR);
                $sth->bindParam(3, $how_price[$index5], PDO::PARAM_INT);
                $sth->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
      }

    }
}
?>

配列をテーブル「サービス」に保存するにはどうすればよいですか?
id | service_name | background | background_price | count | count_price | type | type_price | how | how_price | resolution | resolution_price
スタックから多くのソリューションを試したよう見えますが、何も役に立ちませんでした。エクスポートされたスキーマテーブル「サービス」に関して

クラシペンコフ

私があなたの質問とあなたが達成したいことを理解しているなら、これはあなたの問題に対する一つのアプローチです:

class Service extends Connection {
    public function addService($service_name, $background, $background_price, $resolution, $resolution_price, $count, $count_price, $type, $type_price, $how, $how_price) {
        $max = max(count($background), count($background_price), count($resolution), count($resolution_price), count($count), count($count_price), count($type), count($type_price), count($how), count($how_price));

        try {
            $query = "INSERT INTO services (`service_name`, `background`, `background_price`, `count`, `count_price`, `type`, `type_price`, `how`, `how_price`, `resolution`, `resolution_price`) VALUES ";
            $data = [];
            for ($i = 0; $i < $max; $i++) {
                $query .= "(?,?,?,?,?,?,?,?,?,?,?),";
                $data[] = isset($service_name) ? $service_name : '';
                $data[] = isset($background[$i]) ? $background[$i] : '';
                $data[] = isset($background_price[$i]) ? $background_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($count[$i]) ? $count[$i] : '';
                $data[] = isset($count_price[$i]) ? $count_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($type[$i]) ? $type[$i] : '';
                $data[] = isset($type_price[$i]) ? $type_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($how[$i]) ? $how[$i] : '';
                $data[] = isset($how_price[$i]) ? $how_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($resolution[$i]) ? $resolution[$i] : '';
                $data[] = isset($resolution_price[$i]) ? $resolution_price[$i] : 0; // OR '' depending on the column type
            }
            $query = rtrim($query, ',');
            $sth = $this->dbh->prepare($query);
            $sth->execute($data);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
}

これは、挿入ごとに1つのクエリを使用する2番目のアプローチです。

class Service extends Connection {
    public function addService($service_name, $background, $background_price, $resolution, $resolution_price, $count, $count_price, $type, $type_price, $how, $how_price) {
        $max = max(count($background), count($background_price), count($resolution), count($resolution_price), count($count), count($count_price), count($type), count($type_price), count($how), count($how_price));

        try {
            $query = "INSERT INTO services (`service_name`, `background`, `background_price`, `count`, `count_price`, `type`, `type_price`, `how`, `how_price`, `resolution`, `resolution_price`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";

            for ($i = 0; $i < $max; $i++) {
                $data = [];
                $data[] = isset($service_name) ? $service_name : '';
                $data[] = isset($background[$i]) ? $background[$i] : '';
                $data[] = isset($background_price[$i]) ? $background_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($count[$i]) ? $count[$i] : '';
                $data[] = isset($count_price[$i]) ? $count_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($type[$i]) ? $type[$i] : '';
                $data[] = isset($type_price[$i]) ? $type_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($how[$i]) ? $how[$i] : '';
                $data[] = isset($how_price[$i]) ? $how_price[$i] : 0; // OR '' depending on the column type
                $data[] = isset($resolution[$i]) ? $resolution[$i] : '';
                $data[] = isset($resolution_price[$i]) ? $resolution_price[$i] : 0; // OR '' depending on the column type

                $sth = $this->dbh->prepare($query);
                $sth->execute($data);
            }

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
}

そして、重複する行が見つかった場合のUPDATE代わりにINSERT

class Service extends Connection {
    public function addService($service_name, $background, $background_price, $resolution, $resolution_price, $count, $count_price, $type, $type_price, $how, $how_price) {
        $max = max(count($background), count($background_price), count($resolution), count($resolution_price), count($count), count($count_price), count($type), count($type_price), count($how), count($how_price));

        try {
            $query = "INSERT INTO services (`service_name`, `background`, `background_price`, `count`, `count_price`, `type`, `type_price`, `how`, `how_price`, `resolution`, `resolution_price`) VALUES
                        (:service_name, :background,:background_price,:count,:count_price,:type,:type_price,:how,:how_price,:resolution,:resolution_price)
                      ON DUPLICATE KEY UPDATE
                        `background`=:background2, `background_price`=:background_price2, `count`=:count2, `count_price`=:count_price2,
                        `type`=:type2, `type_price`=:type_price2, `how`=:how2, `how_price`=:how_price2, `resolution`=:resolution2, `resolution_price`=:resolution_price2";

            for ($i = 0; $i < $max; $i++) {
                $data = [];
                $data[':service_name'] = isset($service_name) ? $service_name : '';
                $data[':background'] = isset($background[$i]) ? $background[$i] : '';
                $data[':background2'] = isset($background[$i]) ? $background[$i] : '';
                $data[':background_price'] = isset($background_price[$i]) ? $background_price[$i] : 0; // OR '' depending on the column type
                $data[':background_price2'] = isset($background_price[$i]) ? $background_price[$i] : 0; // OR '' depending on the column type
                $data[':count'] = isset($count[$i]) ? $count[$i] : '';
                $data[':count2'] = isset($count[$i]) ? $count[$i] : '';
                $data[':count_price'] = isset($count_price[$i]) ? $count_price[$i] : 0; // OR '' depending on the column type
                $data[':count_price2'] = isset($count_price[$i]) ? $count_price[$i] : 0; // OR '' depending on the column type
                $data[':type'] = isset($type[$i]) ? $type[$i] : '';
                $data[':type2'] = isset($type[$i]) ? $type[$i] : '';
                $data[':type_price'] = isset($type_price[$i]) ? $type_price[$i] : 0; // OR '' depending on the column type
                $data[':type_price2'] = isset($type_price[$i]) ? $type_price[$i] : 0; // OR '' depending on the column type
                $data[':how'] = isset($how[$i]) ? $how[$i] : '';
                $data[':how2'] = isset($how[$i]) ? $how[$i] : '';
                $data[':how_price'] = isset($how_price[$i]) ? $how_price[$i] : 0; // OR '' depending on the column type
                $data[':how_price2'] = isset($how_price[$i]) ? $how_price[$i] : 0; // OR '' depending on the column type
                $data[':resolution'] = isset($resolution[$i]) ? $resolution[$i] : '';
                $data[':resolution2'] = isset($resolution[$i]) ? $resolution[$i] : '';
                $data[':resolution_price'] = isset($resolution_price[$i]) ? $resolution_price[$i] : 0; // OR '' depending on the column type
                $data[':resolution_price2'] = isset($resolution_price[$i]) ? $resolution_price[$i] : 0; // OR '' depending on the column type

                $sth = $this->dbh->prepare($query);
                $sth->execute($data);
            }

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

配列値をデータベースに挿入します

分類Dev

PHPを使用してデータベースに配列を挿入します

分類Dev

データベーステーブルに配列を挿入します

分類Dev

配列を分割し、laravelのデータベースに挿入します

分類Dev

Laravelのデータベースに配列値を挿入します

分類Dev

PythonNumPy配列をPostgreSQLデータベースに挿入します

分類Dev

データベースに複数の配列を挿入します

分類Dev

配列の1つの値をデータベースに挿入します

分類Dev

データベースに2つの配列を挿入します

分類Dev

配列値をmysqlデータベースに挿入します

分類Dev

文字列をJDBCSQLiteデータベースに挿入します

分類Dev

array_intersect_key配列データをデータベーステーブルに挿入します

分類Dev

配列リストjavaにデータを挿入します

分類Dev

配列をPostgresqlデータベースに挿入する

分類Dev

データベースに配列を挿入する方法

分類Dev

PHPを使用してデータベースに配列を挿入する

分類Dev

ノードを使用して辞書の配列をMySQLデータベースに挿入します

分類Dev

json配列を取得してデータベースに挿入する方法。php

分類Dev

foreachループに接続された配列をデータベースに挿入します

分類Dev

PHPを使用してMySQLデータベースに配列を挿入しますか?

分類Dev

MYSQLデータベースの単一のフィールドに配列を挿入します

分類Dev

MySQL / PHP:フォーム配列をデータベースに挿入しますか?

分類Dev

ループ内のデータベースにマルチ配列を挿入します

分類Dev

データベース挿入のためにPOST配列をループします

分類Dev

2D配列の最初の3つの要素をMySqlデータベースに挿入します

分類Dev

Rubyオブジェクトの配列をMongoデータベースに挿入します

分類Dev

2つの不均一な配列を組み合わせて、データベースに挿入します

分類Dev

Laravel - JSON 配列データをデータベースに挿入する

分類Dev

配列からデータをフェッチしてデータベースに挿入する方法

Related 関連記事

  1. 1

    配列値をデータベースに挿入します

  2. 2

    PHPを使用してデータベースに配列を挿入します

  3. 3

    データベーステーブルに配列を挿入します

  4. 4

    配列を分割し、laravelのデータベースに挿入します

  5. 5

    Laravelのデータベースに配列値を挿入します

  6. 6

    PythonNumPy配列をPostgreSQLデータベースに挿入します

  7. 7

    データベースに複数の配列を挿入します

  8. 8

    配列の1つの値をデータベースに挿入します

  9. 9

    データベースに2つの配列を挿入します

  10. 10

    配列値をmysqlデータベースに挿入します

  11. 11

    文字列をJDBCSQLiteデータベースに挿入します

  12. 12

    array_intersect_key配列データをデータベーステーブルに挿入します

  13. 13

    配列リストjavaにデータを挿入します

  14. 14

    配列をPostgresqlデータベースに挿入する

  15. 15

    データベースに配列を挿入する方法

  16. 16

    PHPを使用してデータベースに配列を挿入する

  17. 17

    ノードを使用して辞書の配列をMySQLデータベースに挿入します

  18. 18

    json配列を取得してデータベースに挿入する方法。php

  19. 19

    foreachループに接続された配列をデータベースに挿入します

  20. 20

    PHPを使用してMySQLデータベースに配列を挿入しますか?

  21. 21

    MYSQLデータベースの単一のフィールドに配列を挿入します

  22. 22

    MySQL / PHP:フォーム配列をデータベースに挿入しますか?

  23. 23

    ループ内のデータベースにマルチ配列を挿入します

  24. 24

    データベース挿入のためにPOST配列をループします

  25. 25

    2D配列の最初の3つの要素をMySqlデータベースに挿入します

  26. 26

    Rubyオブジェクトの配列をMongoデータベースに挿入します

  27. 27

    2つの不均一な配列を組み合わせて、データベースに挿入します

  28. 28

    Laravel - JSON 配列データをデータベースに挿入する

  29. 29

    配列からデータをフェッチしてデータベースに挿入する方法

ホットタグ

アーカイブ