Slim 3发布数据

时间:2017-10-25 04:02:49

标签: php pdo slim-3

我一直收到这些错误:

  

未定义的索引:第38行的C:\ wamp \ www \ legitapi \ src \ public \ index.php中的fname

  

警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未在第5行的C:\ wamp \ www \ legitapi \ src \ public \ index.php中定义

$app->post('/users/new', function ($request, $response, $args) {
    require_once '../config/db.php';

    $requestObj = $request->getParsedBody();

    if(isset($requestObj)){
        $firstName = $requestObj['fname'];
        $lastName = $requestObj['lname'];
        $email = $requestObj['email'];
        $phone = $requestObj['phone'];
        $country = $requestObj['country'];
        $zipcode = $requestObj['zipcode'];

        try {
            $conn = $pdo;
            $sql = 'INSERT INTO CUSTOMERS(fname, lname, email, phone, country, zipcode) VALUES (:first_name, :last_name, :email, :phone,  :country, :zipcode)';

            $stmt = $conn->prepare($sql);
            $stmt->bindValue(':fname', $firstName);
            $stmt->bindValue(':lname', $lastName);
            $stmt->bindValue(':email', $email);
            $stmt->bindValue(':phone', $phone);
            $stmt->bindValue(':country', $country);
            $stmt->bindValue(':zipcode', $zipcode);
            echo json_encode($stmt->execute());
        } catch (PDOException $e) {
            echo json_encode($e);
        }
    }
});

1 个答案:

答案 0 :(得分:0)

来自前端的帖子请求正文参数与后端的匹配不匹配。错误发生在客户端的html表单代码中。你的PHP代码非常完美。