如何在不强制用户填写所有表单的情况下将值插入DB,而不会出现未定义索引的任何错误

时间:2019-02-07 12:03:22

标签: php html pdo

我有一个包含多个输入和select和option的表单,当用户在select和option中选择一个值(是或否)时,使用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());
        }
    }
}

2 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分:1)

您必须检查是否已设置每个输入,如果未设置默认值,则该默认值适合表中的列类型。

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

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