Codeigniter PHP 在数据库中保存数据

Posted

技术标签:

【中文标题】Codeigniter PHP 在数据库中保存数据【英文标题】:Codeigniter PHP Saving data in database 【发布时间】:2019-01-15 09:04:12 【问题描述】:

我在保存数据时遇到问题。我的 2 个表单中的所有数据都没有保存在数据库中。它保存为空。我确信我已经从 html 表单获取数据到控制器。我检查了网络>标题

这是我的代码

控制器

public function add_prop()

    ini_set('max_execution_time', 300);
    // property details form


    $account_id = $this->session->userdata('login_id');
    $td_arp_no = sanitize($this->input->post('td_arp_no'));
    $owner = sanitize($this->input->post('owner'));
    $address = sanitize($this->input->post('address'));
    $administrator_user = sanitize($this->input->post('administrator_user'));
    $address_2 = sanitize($this->input->post('address_2'));
    $pin = sanitize($this->input->post('pin'));
    $tin = sanitize($this->input->post('tin'));
    $tel_cell_no = sanitize($this->input->post('tel_cell_no'));
    $tin_2 = sanitize($this->input->post('tin_2'));
    $tel_cell_no_2 = sanitize($this->input->post('td_arp_no'));



    // property location form


    $no_street = sanitize($this->input->post('no_street'));
    $municipality = sanitize($this->input->post('municipality'));
    $land_owner = sanitize($this->input->post('land_owner'));
    $building_owner = sanitize($this->input->post('building_owner'));
    $longitude = sanitize($this->input->post('longitude'));
    $brgy = sanitize($this->input->post('brgy'));
    $province_city = sanitize($this->input->post('province_city'));
    $parcel_pin = sanitize($this->input->post('parcel_pin'));
    $bldg_pin = sanitize($this->input->post('bldg_pin'));
    $latitude = sanitize($this->input->post('td_arp_no'));


    $prop_id = $this->LandClientModel->add_prop($account_id,$td_arp_no,$pin,$owner,$tin,$address,$tel_cell_no,$administrator_user,$tin_2,$address_2,$tel_cell_no_2,$no_street,$brgy,$municipality,$province_city,$land_owner,$parcel_pin,$building_owner,$bldg_pin,$longitude,$latitude);

    $data = [
        "success" => 1,
        "prop_id" => $prop_id
    ];
    generate_json($data);


型号

public function add_prop($account_id,$td_arp_no,$pin,$owner,$tin,$address,$tel_cell_no,$administrator_user,$tin_2,$address_2,$tel_cell_no_2,$no_street,$brgy,$municipality,$province_city,$land_owner,$parcel_pin,$building_owner,$bldg_pin,$longitude,$latitude)

 $sql = "INSERT INTO land_property_details (
        account_id,
        td_arp_no,
        pin,
        owner,
        tin,
        address,
        tel_cell_no,
        administrator_user,
        tin_2,
        address_2,
        tel_cell_no_2,
        status


    ) VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ? 
    )";
    $data = array(
        $account_id,
        $td_arp_no,
        $pin,
        $owner,
        $tin,
        $address,
        $tel_cell_no,
        $administrator_user,
        $tin_2,
        $address_2,
        $tel_cell_no_2,
        1
    );

    $return['property_details'] = $this->db->query($sql,$data);

    $property_id = $this->db->insert_id();

  $sql2 = "INSERT INTO land_property_location (
        prop_id,
        account_id,
        no_street,
        brgy,
        municipality,
        province_city,
        land_owner,
        parcel_pin,
        building_owner,
        bldg_pin,
        longitude,
        latitude 
    ) VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?  
    )";
    $data2 = array(
        $property_id,
        $account_id,
        $no_street,
        $brgy,
        $municipality,
        $province_city,
        $land_owner,
        $parcel_pin,
        $building_owner,
        $bldg_pin,
        $longitude,
        $latitude

    );

    $return['property_location'] =  $this->db->query($sql2,$data2);




    return $return;

我一直在这样做,但这是我第一次遇到这个问题。 我该怎么办?似乎是什么问题?

【问题讨论】:

你在这里过于复杂了 - 你应该正确组织你的数据 【参考方案1】:

正如我在评论部分已经说过的那样,您在这里过于复杂了-您必须正确组织数据-在您的情况下,最好在控制器中执行此操作

试试下面的

你的控制器

public function add_prop()

    $arrPropertyDetails = [
        'account_id' => $this->session->userdata('login_id'),
        'td_arp_no' => sanitize($this->input->post('td_arp_no')),
        'pin' => sanitize($this->input->post('pin')),
        'owner' => sanitize($this->input->post('owner')),
        'tin' => sanitize($this->input->post('tin')),
        'address' => sanitize($this->input->post('address')),
        'tel_cell_no' => sanitize($this->input->post('tel_cell_no')),
        'administrator_user' => sanitize($this->input->post('administrator_user')),
        'tin_2' => sanitize($this->input->post('tin_2')),
        'address_2' => sanitize($this->input->post('address_2')),
        'tel_cell_no_2' => sanitize($this->input->post('tel_cell_no_2')),
        'status' => 1
    ];

    $arrPropertyLocation = [
        'prop_id' => '',
        'account_id' => $this->session->userdata('login_id'),
        'no_street' => sanitize($this->input->post('no_street')),
        'brgy' =>  sanitize($this->input->post('brgy')),
        'municipality' => sanitize($this->input->post('municipality')),
        'province_city' => sanitize($this->input->post('province_city')),
        'land_owner' => sanitize($this->input->post('land_owner')),
        'parcel_pin' => sanitize($this->input->post('parcel_pin')),
        'building_owner' => sanitize($this->input->post('building_owner')),
        'bldg_pin' => sanitize($this->input->post('bldg_pin')),
        'longitude' => sanitize($this->input->post('longitude')),
        'latitude' => sanitize($this->input->post('latitude'))
    ];

    $propertyId = $this->LandClientModel->add_prop($arrPropertyDetails, $arrPropertyLocation);

    $data = [
        "success" => 1,
        "prop_id" => $propertyId
    ];
    generate_json($data);

你的模型

public function add_prop($arrPropertyDetails, $arrPropertyLocation)

    $this->db->insert('land_property_details', $arrPropertyDetails);
    $id = $this->db->insert_id();
    $arrPropertyLocation['prop_id'] = $id;
    $this->db->insert('land_property_location', $arrPropertyLocation);
    return $id;

如果您的数据有问题 - 您只需在控制器中打印出数组即可。

【讨论】:

【参考方案2】:

在控制器中使用数组

public function add_prop()

$data=array();
$insertArray=array(
     $account_id = $this->session->userdata('login_id'),
    $td_arp_no = sanitize($this->input->post('td_arp_no')),
    $owner = sanitize($this->input->post('owner')),
    $address = sanitize($this->input->post('address')),
    $administrator_user = sanitize($this->input->post('administrator_user')),
    $address_2 = sanitize($this->input->post('address_2')),
    $pin = sanitize($this->input->post('pin')),
    $tin = sanitize($this->input->post('tin')),
    $tel_cell_no = sanitize($this->input->post('tel_cell_no')),
    $tin_2 = sanitize($this->input->post('tin_2')),
    $tel_cell_no_2 = sanitize($this->input->post('td_arp_no')));
if($this->model_name->add_prop($insertArray))
 // property location form
$insertArray2=array(
 $no_street = sanitize($this->input->post('no_street')),
    $municipality = sanitize($this->input->post('municipality')),
    $land_owner = sanitize($this->input->post('land_owner')),
    $building_owner = sanitize($this->input->post('building_owner')),
    $longitude = sanitize($this->input->post('longitude')),
    $brgy = sanitize($this->input->post('brgy')),
    $province_city = sanitize($this->input->post('province_city')),
    $parcel_pin = sanitize($this->input->post('parcel_pin')),
    $bldg_pin = sanitize($this->input->post('bldg_pin')),
    $latitude = sanitize($this->input->post('td_arp_no')));
if($this->model_name->methodname($insertArray2))
//sccess message

    

//模型

function add_prop($insertArray) 
        if ($this->db->insert('land_property_details', $insertArray) > 0) 
            return $this->db->insert_id();
        
        return 0;
    

function methodname($insertArray2) 
        if ($this->db->insert('land_property_location', $insertArray2) > 0) 
            return $this->db->insert_id();
        
        return 0;
    

【讨论】:

以上是关于Codeigniter PHP 在数据库中保存数据的主要内容,如果未能解决你的问题,请参考以下文章

如何保存在PHP CodeIgniter中的文本框中输入的数据

在 php codeigniter 中包含另一个页面时,父页面未加载

Codeigniter php - 更新数据库多行

CodeIgniter 如何知道 cookie 保存了有效的会话数据?

如何在 CodeIgniter 4 中保存到数据库后获取插入 ID

在会话中保存下拉菜单中的数据并显示在另一个页面 Codeigniter