Ivan Z.
Ivan Z.

Reputation: 67

After adding forth parameter to database, error occurs while POSTing data via Postman

I am trying to POST new entry into database. When there is four parameters: id title description link code is working fine. But after I add fifth 'image' parameter and try to POST via Postman, code is returning: "Error while adding entry".

API is written in PHP with mysqli connection to database.

if(isset($_POST['title'])&&isset($_POST['description'])&&isset($_POST['link'])&&isset($_POST['image'])){
$title = $_POST['title'];
$description = $_POST['description'];
$link = $_POST['link'];
$image = $_POST['image'];

$query = "INSERT INTO votes( title, description, link, image) VALUES (?,?,?,?)";
if($stmt = $con->prepare($query)){
    mysqli_stmt_bind_param($stmt,"sss",$title,$description,$link,$image);
    $stmt->execute();
    if($stmt->affected_rows == 1){
        $response["success"] = 1;           
        $response["message"] = "News Successfully Added";           

    }else{
        //Some error while inserting
        $response["success"] = 0;
        $response["message"] = "Error while adding entry";
    }                   
}else{
    $response["success"] = 0;
    $response["message"] = mysqli_error($con);
}}else{
$response["success"] = 0;
$response["message"] = "missing mandatory parameters";}echo json_encode($response);?>

I can't figure out why is not working after I add one column in database, but its working when there is no 'image' column.

Upvotes: 0

Views: 38

Answers (1)

Raahul
Raahul

Reputation: 397

Arguments need to be correct

i - integer d - double s - string b - BLOB

if(isset($_POST['title'])&&isset($_POST['description'])&&isset($_POST['link'])&&isset($_POST['image'])){
$title = $_POST['title'];
$description = $_POST['description'];
$link = $_POST['link'];
$image = $_POST['image'];

$query = "INSERT INTO votes( title, description, link, image) VALUES (?,?,?,?)";
if($stmt = $con->prepare($query)){
    mysqli_stmt_bind_param($stmt,"ssss",$title,$description,$link,$image);
    $stmt->execute();
    if($stmt->affected_rows == 1){
        $response["success"] = 1;           
        $response["message"] = "News Successfully Added";           

    }else{
        //Some error while inserting
        $response["success"] = 0;
        $response["message"] = "Error while adding entry";
    }                   
}else{
    $response["success"] = 0;
    $response["message"] = mysqli_error($con);
}}else{
$response["success"] = 0;
$response["message"] = "missing mandatory parameters";}echo json_encode($response);?>

Upvotes: 1

Related Questions