【问题标题】:Pass last_inserted id from one function to another function in the same class in codeigniter将 last_inserted id 从一个函数传递给 codeigniter 中同一类中的另一个函数
【发布时间】:2019-06-27 20:33:41
【问题描述】:

我想将我的变量 $lastid 传递给同一类中的另一个函数。我该怎么做??..我尝试了一些东西,但它不起作用?

class ProductmModel extends CI_Model
{

  var $PRODUCTNAME='';
  var $DESCRIPTION='';
  var $CATEGORY='';
  var $SUBCAT='';
  var $IMAGE='';


public function addproductm()
{
$this->load->database();
$data = array(
"p_name" => $this->PRODUCTNAME,
"p_des" => $this->DESCRIPTION,
"cat" => $this->CATEGORY,
"subcat" => $this->SUBCAT

);
$this->db->insert('product_multi', $data);
$lastid = $this->db->insert_id();

}

这是下一个函数

public function addimage()
{
$last= $this->addproductm();
$this->load->database();

$data = array(
"pid" => $lastid,
"images" => $this->IMAGE

);
$this->db->insert('image', $data);
}
}

【问题讨论】:

  • 尝试将 id 传递给函数。 public function addimage($product_id) 并在你想传递 id 的地方调用这个函数,这里在 addproductm() 函数为 $this->addimage($lastid)
  • 为什么不在全局范围内扩展$lastid 的声明? php.net/manual/en/language.variables.scope.php

标签: php function codeigniter class mysqli


【解决方案1】:

这是一种方法。假设你有一个类似

的函数
function getCirclePoints($center,$radius){

}

在同一个类中调用函数时

$this->getCirclePoints($latlng,radius));

【讨论】:

    【解决方案2】:

    你可以在你的第一个函数中调用 addimage 函数。试试这样。我希望它会有所帮助。

    public function addproductm()
    {
    $this->load->database();
    $data = array(
    "p_name" => $this->PRODUCTNAME,
    "p_des" => $this->DESCRIPTION,
    "cat" => $this->CATEGORY,
    "subcat" => $this->SUBCAT
    );
    $this->db->insert('product_multi', $data);
    $lastid = $this->db->insert_id();
    $id=$this->addimage($lastid);
    }
    
    public function addimage($id)
    {
    $last= $this->addproductm();
    $this->load->database();
    
    $data = array(
    "pid" => $id,
    "images" => $this->IMAGE
    
    );
    $this->db->insert('image', $data);
    }
    }
    

    【讨论】:

    • 嘿,如果你在那里,你能帮我吗.. 当我使用此代码时...在实际行之前将空白行添加到数据库中...并且空白行数取决于图像数我正在上传..你能帮忙吗?
    • 但如果我不使用 last_id 的东西它可以完美地工作
    【解决方案3】:

    首先,确保您的表格中有一个自动递增的 id

    其次,插入后从模型中返回$this->db->insert_id()

    public function addproductm()
    {
        $this->load->database();
        $data = array(
           "p_name" => $this->PRODUCTNAME,
           "p_des" => $this->DESCRIPTION,
           "cat" => $this->CATEGORY,
           "subcat" => $this->SUBCAT
        );
        $this->db->insert('product_multi', $data);
        return $this->db->insert_id();
    }
    

    在这里你得到了模型中最后插入的 id,例如 $last_id = $this->db->insert('image', $data)

    $last_id 传递给函数

    public function addimage()
    {
       $last= $this->addproductm();
       $this->load->database();
    
       $data = array(
          "pid" => $lastid,
          "images" => $this->IMAGE
       );
       $last_id = $this->db->insert('image', $data);
       $this->function_name($last_id);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2019-07-25
      相关资源
      最近更新 更多