简体   繁体   中英

Insert multiple values in one column of mysql database

I am trying to insert multiple values with one html input, HTML input will have text with seperated by semicolon, will use explode function to create array and then want to pass this array to mysql

I have created below code,

if($this->input->post('create_category'))
    {
        $category_chunk = $this->input->post('category_name');
        $category_list = explode(";", $category_chunk);

        $category_array = array(
                    'category_name' =>$category_list );

        $result = $this->model_admin->create_expense_category($category_array);}

When i click Create category, it throw error " Message: Array to string conversion" and "INSERT INTO ec_ex_category ( category_name ) VALUES (Array)"

I know this is created due to 'category_name' =>$category_list,

I dont know how to pass array in 'category_name' column, I am using codeigniter.

Any help appreciated,

Thanks,

$category_list is array so you have to get category_name from it first and then to insert it.

Example:

$category_name = $category_list[0] // Whatever index is name in array

then insert $category_name.

You are trying to send an array within an array to your model. To insert multiple lines in one line you need to use insert_batch which require an array of arrays to work :

Controller :

$category_list = explode(";", $category_chunk);

$category_array = array();
foreach($category_list as $c)
{
    $category_array[] = array('category_name' =>$c)
}

Then in your model :

$this->db->insert_batch('ec_ex_category',$category_array); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM