Save the information of a variable in Codeigniter to the database

Asked

Viewed 395 times

0

I’m getting a lot of product information from the Cosmos API and storing it in variables, but how do I save the information from these variables in the database?

VIEW

 $curl = curl_init($url);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_FAILONERROR, true);
 $data = curl_exec($curl);

 if ($data === false || $data == NULL) {
     var_dump(curl_error($curl));
 } else {
     $array = json_decode($data);
     $linkimg = $array->thumbnail;
     $nomeprdt = $array->description;
     $segmentacao = $array->ncm->description;

I wanted to pass to the database the $nomeprdt and $segmentation.

MODEL

    public function uploadtest($id,$nomeprdt,$segmentacao)
    {
      if($id > 0){
        $this->db->query("INSERT INTO produto(nomeprdt, segprdt) VALUES(? , ?)",$nomeprdt,$segmentacao);
      }else
      {
        return false;
      }
    }

CONTROLLER

public function upload_dcr(){
  $this->Produto_model->uploadtest($id,$nomeprdt,$segmentacao);
}
  • Have you tried using data of type JSON in creating the table produto? Or alternatively, why not use serialize of PHP.

  • Which error is shown?

1 answer

1


It seems to me that the contents of view is mistaken. In your case, the view should first serve to tell the user "Give me a URL to consult!" and then "Product successfully inserted!", "Product already exists in the register!", "Such error!" etc..

All Curl implementation should be on controller.

A first method in the serveria controller only to call one view containing a form method post asking the user to enter a URL with <input type="url" name="url" required autofocus>.

Then another method receives this URL, validates this URL, processes operations with Curl, gets JSON, calls model (without the $id parameter if it is auto-increment) and, based on $this->db->affected_rows() it is discovered whether or not the product has been inserted. In a very rough way it is possible to echo the affected_rows() without having view for this processing. It is also possible to merge the methods in the controller into one, if you prefer, based on whether or not you are receiving the URL variable by post.

Considerations on the implementations given in your example:

  • It is not clear, not to say incorrect, where the variable comes from $url.
  • In the method uploadtest() you need to remove the $id unnecessary parameter. If this is not auto-increment, you will have to calculate it before inserting the product, within the method itself uploadtest(), then it makes no sense to ever inform you.
  • In the method uploadtest() missing to transform the parameters for query into array:

    $this->db->query("INSERT INTO produto(nomeprdt, segprdt) SELECT ?, ?", array($nomeprdt, $segmentacao));
    

And in the method uploadtest() it is good to give a useful feedback, which one can display on the screen for you to understand what is happening:

    return $this->db->affected_rows();

Echo the model call to see how many lines were inserted:

    echo($this->Produto_model->uploadtest($nomeprdt,$segmentacao));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.