Почему моя кнопка «Добавить» добавляет также пустой объект - PullRequest
0 голосов
/ 10 октября 2018

Так что моя кнопка добавления работает, но время от времени она также добавляет пустой объект, хотя и не всегда.Итак, вот мой код в API:

function create(){

    // query to insert record
    $query = "INSERT INTO
                " . $this->table_name . "
            SET
                name=:name,location=:location";

                echo $query;

    // prepare query
    $stmt = $this->conn->prepare($query);
    if (!$stmt)
      var_dump($this->conn->errorInfo());

    // sanitize
    $this->name=htmlspecialchars(strip_tags($this->name));
    $this->location=htmlspecialchars(strip_tags($this->location));



    // bind values
    $stmt->bindParam(":name", $this->name);
    $stmt->bindParam(":location", $this->location);



    // execute query
    if($stmt->execute()){
        return true;
    }
var_dump($this->conn->errorInfo());
    return false;

}

А вот тот, который находится в отделе Service:

  addDepartment (name:string, location:string):Observable<any> {
      return this.http.post(this.depCreate,{
            "name": name,
            "location": location},
            httpOptions);
    }

Ответы [ 2 ]

0 голосов
/ 10 октября 2018

Измените это

 return this.http.post(this.depCreate,{
        "name": name,
        "location": location},
        httpOptions);
}

на

 return this.http.post(this.depCreate,{
        name,
        location},
        httpOptions);
}
0 голосов
/ 10 октября 2018

вы не добавили проверку пустых значений

if(!empty($this->name) && !empty($this->name))
{
// your code
}
...