【问题标题】:Codeigniter post data from declared variableCodeigniter 从声明的变量发布数据
【发布时间】:2020-03-18 18:16:27
【问题描述】:
function index_post() {
        $sql = "SELECT id_so FROM so_detail ORDER BY id_so DESC LIMIT 1";
        $last_id2 = $this->db->query($sql)->result();
         foreach ($last_id2 as $row) {
             $last_id = $row->id_so;
         }
         //echo $last_id;
         $data = array(
                'id_so'      => $this->post($last_id),
                'id_product' => $this->post('id_product'),
                'harga'      => $this->post('harga'),
                'harga_dasar'=> $this->post('harga'),
                'modal'      => $this->post('modal'),
                'pajak'      => $this->post('pajak'),
                'qty'        => $this->post('qty'),
                'keterangan' => $this->post('keterangan'),
                'create_user'=> $this->post('create_user'),
                'create_time'=> $this->post('create_time'),
                'update_user'=> $this->post('create_user'),
                'update_time'=> $this->post('create_time'));
         $insert = $this->db->insert('so_detail', $data);

        if ($insert) {
            $this->response($data, 200);
        } else {
            $this->response(array('status' => 'fail', 502));
        }

    }

我遇到了一个问题,即发布的 id_so 为“null”。当我回显 $last_id 时,它显示正确的 id ex:120。但是当我将它调用到 $this->post($last_id) 时,它只是为空..

如何将 id_so 与之前已声明的字符串 ($last_id) 一起发布??

【问题讨论】:

  • 我不明白你在做什么。你为什么使用 $this->post()?

标签: sql json database api codeigniter


【解决方案1】:

$last_id2 变量中有很多数组。数据会根据你的 last_id2 插入多次。试试这个:

foreach ($last_id2 as $row) {
 $data = array(
                'id_so'      =>$row->id_so,
                'id_product' => $this->post('id_product'),
                'harga'      => $this->post('harga'),
                'harga_dasar'=> $this->post('harga'),
                'modal'      => $this->post('modal'),
                'pajak'      => $this->post('pajak'),
                'qty'        => $this->post('qty'),
                'keterangan' => $this->post('keterangan'),
                'create_user'=> $this->post('create_user'),
                'create_time'=> $this->post('create_time'),
                'update_user'=> $this->post('create_user'),
                'update_time'=> $this->post('create_time'));
         }
 $this->db->insert_batch('so_detail',$data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多