Save in the database the information of a variable in Codeigniter

0

I'm getting a lot of product information from the Cosmos API and storing it into variables, but how do I save the information from those 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 the $ nameprdt and $ segmentation to the database.

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);
}
    
asked by anonymous 27.06.2017 / 21:48

1 answer

1

It seems to me that the content of view is wrong. In your case, the view first should serve to tell the user "Give me a URL so I can consult!" and then " Product inserted successfully!" , < "" ""

All implementation with cURL should be in controller .

A first method in the controller server is only to call a view that contains 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 the operations with cURL, gets JSON, calls model (without the $ id parameter if it is auto increment), and based on $this->db->affected_rows() 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. You can also add the methods to the controller in one if you prefer, based on whether or not you are getting the URL variable by post .

Considerations about the implementations given in your example:

  • It is not clear, not to say incorrect, where the variable $url comes from.
  • In method uploadtest() you need to remove the unnecessary parameter $ id. If this is not self-incrementing, you will have to calculate it before inserting the product into the uploadtest() method itself, so it does not make sense to inform you.
  • In the uploadtest() method, you need to transform the parameters to query in array:

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

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

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

Echo the template call to see how many rows have been inserted:

    echo($this->Produto_model->uploadtest($nomeprdt,$segmentacao));
    
07.07.2017 / 17:02