0

I keep getting these errors:

Undefined index: fname in C:\wamp\www\legitapi\src\public\index.php on line 38

and

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\wamp\www\legitapi\src\public\index.php on line 5

$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);
        }
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

0

Your post request body params from the frontend are not matching up with those of the backend. The error is in your html form code on the client side. Your PHP code is perfectly sound.

Scriptonomy
  • 3,975
  • 1
  • 15
  • 23