如何使用PDO将多个选择值插入数据库?

时间:2019-02-07 10:11:50

标签: php html pdo

我有一个具有多个选择选项的表单,当我运行插入查询时,我得到一个Array to string conversion error

我尝试了 foreach ,但是它仍然没有插入数据库,并且我得到一个空白页,没有错误或foreach错误。

HTML

<select multiple name="platform[]" id="platform">
    <option value="" disabled>בחר כמה מהאפשרויות</option>
    <option value="facebook">facebook</option>
    <option value="instagram">instagram</option>
    <option value="google">google</option>
    <option value="youtube">youtube</option>
    <option value="linkedin">linkedin</option>
</select>

PHP

if (isset($_POST['submit'])) {
    $platform = $_POST['platform'];
    $advertising_history = $_POST['advertising_history'];
    $advertising_external = $_POST['advertising_external'];
    $period_of_time_office = $_POST['period_of_time_office'];
    $price_satisfaction_office = $_POST['price_satisfaction_office'];
    $service_satisfaction_office = $_POST['service_satisfaction_office'];
    $effectiveness_advertising_office = 
    $_POST['effectiveness_advertising_office'];
    $advertising_price_range = $_POST['advertising_price_range'];
    $effectiveness_advertising = $_POST['effectiveness_advertising'];
    $outweb_advertising = $_POST['outweb_advertising'];
    $outweb_location = $_POST['outweb_location'];
    $outweb_effectiveness = $_POST['outweb_effectiveness'];

    $valid = true;

    if ($valid) {
        try {
            $pdo = DB();
            $stmt = $pdo->prepare("
                INSERT INTO client_form_5(
                    client_id,
                    advertising_history,
                    advertising_external,
                    period_of_time_office,
                    price_satisfaction_office,
                    service_satisfaction_office,
                    effectiveness_advertising_office,
                    platform,
                    advertising_price_range,
                    effectiveness_advertising,
                    outweb_advertising,
                    outweb_location,
                    outweb_effectiveness
                )
                VALUES (
                    :client_id,
                    :advertising_history,
                    :advertising_external,
                    :period_of_time_office,
                    :price_satisfaction_office,
                    :service_satisfaction_office,
                    :effectiveness_advertising_office,
                    :platform,
                    :advertising_price_range,
                    :effectiveness_advertising,
                    :outweb_advertising,
                    :outweb_location,
                    :outweb_effectiveness
                )
            ");

            $stmt->bindParam("client_id", $user_id, PDO::PARAM_INT);
            $stmt->bindParam("advertising_history", $advertising_history, PDO::PARAM_STR);
            $stmt->bindParam("advertising_external", $advertising_external, PDO::PARAM_STR);
            $stmt->bindParam("period_of_time_office", $period_of_time_office, PDO::PARAM_STR);
            $stmt->bindParam("price_satisfaction_office", $price_satisfaction_office, PDO::PARAM_STR);
            $stmt->bindParam("service_satisfaction_office", $service_satisfaction_office, PDO::PARAM_STR);
            $stmt->bindParam("effectiveness_advertising_office", $effectiveness_advertising_office, PDO::PARAM_STR);
            $stmt->bindParam("platform", $platform, PDO::PARAM_STR);
            $stmt->bindParam("advertising_price_range", $advertising_price_range, PDO::PARAM_STR);
            $stmt->bindParam("effectiveness_advertising", $effectiveness_advertising, PDO::PARAM_STR);
            $stmt->bindParam("outweb_advertising", $outweb_advertising, PDO::PARAM_STR);
            $stmt->bindParam("outweb_location", $outweb_location, PDO::PARAM_STR);
            $stmt->bindParam("outweb_effectiveness", $outweb_effectiveness, PDO::PARAM_STR);
            $stmt->execute();

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

我希望在数据库中获取所有值选项,但我只会得到单词Array。

0 个答案:

没有答案