Codeigniter PHP在数据库中保存数据

时间:2019-01-15 09:04:13

标签: php sql codeigniter

我在保存数据时遇到问题。我的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;

}

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

2 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分:0)

在控制器中使用数组

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;
    }