0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, returned: Array ( [0] => [1] => [2] => ) null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.