如何插入值DB没有强迫用户填写的所有表格没有得到undefind指数的任何错误

问题描述 投票:0回答:2

我有几个输入的形式,选择和选项,当用户选择的选择和选项的值(是或否),使用jQuery打开是需要填写,所以我们得到的情况是,用户不满山遍野所有的表单只是他需要,而一些投入一直为空,但我得到上留下空白领域未定义指数错误。

现在的问题是如何避免这个错误。

我不知道我应该尝试

if (isset($_POST['submit'])) {

    $platform =  json_encode($_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());
        }
    }
}
php html pdo
2个回答
1
投票

你必须检查每个输入已经设置,如果没有设置默认的是套房,表中的列类型。

因此,对于每一个,这里只是一个字段的示例

$advertising_history = isset($_POST['advertising_history']) 
                            ? $_POST['advertising_history'] 
                            : '<some_default>';

1
投票

使用isset()检查$_POST的元素存在。

您可以使用三元表达式为丢失$_POST指定一个默认值

在例如:

$myVar = ((isset($_POST['someFormInput'])) ? ($_POST['someFormInput']) : ("default value")); //default value can be null or whatever

这将做的比相同:

if (isset($_POST['someFormInput']))
{
    $myVar = $_POST['someFormInput'];
}
else
{
    $myVar = "default value";
}
© www.soinside.com 2019 - 2024. All rights reserved.