Abd Domingos
Abd Domingos

Reputation: 55

Post data using php

I made a API to fetch and store data, I can fetch data without problem, but everything changes when I try to store data, when I send the data it returns [{"success":"0"}]. In the beginning it worked well, used accepted to store data, but suddenly everything changed. I've tried everything, compare the new with the old, changed the code, tables, but even so it always returns [{"success":"0"}] after comparing both (old and new) code I couldn't see much difference. What could I be doing wrong?

Insert data:

function insertloin()
{
    if(isset($_POST["loincode"]))
    {
        $form_data = array(
            ':loincode'     =>  $_POST["loincode"],
            ':code'     =>  $_POST["code"],
            ':specie'       =>  $_POST["specie"],
            ':grade'        =>  $_POST["grade"],
            ':vesselname'       =>  $_POST["vesselname"],
            ':type'     =>  $_POST["type"],
            ':productformat'        =>  $_POST["productformat"],
            ':dateprocessed'        =>  $_POST["dateprocessed"],
            ':datebb'       =>  $_POST["datebb"],
            ':projectcode'      =>  $_POST["projectcode"],
            ':netweight'        =>  $_POST["netweight"],
            ':producttype'      =>  $_POST["producttype"],
            ':oldoc'        =>  $_POST["oldoc"]
            
        );
        $query = "
        INSERT INTO loins 
        (loincode, code, specie, grade, vesselname, type, productformat, dateprocessed, datebb, projectcode, netweight, producttype, oldoc) VALUES 
        (:loincode, :code, :specie, :grade, :vesselname, :type, :productformat, :dateprocessed, :datebb, :projectcode, :netweight, :producttype, :oldoc)
        ";
        $statement = $this->connect->prepare($query);
        if($statement->execute($form_data))
        {
            $data[] = array(
                'success'   =>  '1'
            );
        }
        else
        {
            $data[] = array(
                'success'   =>  '0'
            );
        }
    }
    else
    {
        $data[] = array(
            'success'   =>  '0'
        );
    }
    return $data;
}

Test:

    if($_GET["action"] == 'insertloin')
{
    $data = $api_object->insertloin();
}

Action:

    if(isset($_POST["action"]))
{
    if($_POST["action"] == 'insertloin')
    {
        $form_data = array(
                'loincode'      =>  $_POST["loincode"],
                'code'      =>  $_POST["code"],
                'specie'        =>  $_POST["specie"],
                'grade'     =>  $_POST["grade"],
                'vesselname'        =>  $_POST["vesselname"],
                'type'      =>  $_POST["type"],
                'productformat'     =>  $_POST["productformat"],
                'dateprocessed'     =>  $_POST["dateprocessed"],
                'datebb'        =>  $_POST["datebb"],
                'projectcode'       =>  $_POST["projectcode"],
                'netweight'     =>  $_POST["netweight"],
                'producttype'       =>  $_POST["producttype"],
                'oldoc'     =>  $_POST["oldoc"]
        );
        $api_url = "http://192.168.85.160/API/v2/api/test_api.php?action=insertloin";
        
        $client = curl_init($api_url);
        curl_setopt($client, CURLOPT_POST, true);
        curl_setopt($client, CURLOPT_POSTFIELDS, $form_data);
        curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($client);
        curl_close($client);
        $result = json_decode($response, true);
        foreach($result as $keys => $values)
        {
            if($result[$keys]['success'] == '1')
            {
                echo 'insert';
            }
            else
            {
                echo 'error';
            }
        }
    }

Please, help me find the bug. Kind regards, Abd

Upvotes: 0

Views: 118

Answers (1)

O. Jones
O. Jones

Reputation: 108651

When $statement->execute() returns a false value it means your SQL had some sort of error. But you ignore that false value and instead return the same success status in both your if and your else clause.

To see your error codes try something like this:

      if($statement->execute($form_data))
        {
            $data[] = array(
                'success'   =>  '1'
            );
        }
        else
        {
          echo $statement->errorInfo();
          print_r ( $statement->errorInfo() );
          die(1);
        }

You'll need to look at the error codes you get to see what's wrong and then do something smarter than echo / print_r / die.

Upvotes: 2

Related Questions