简体   繁体   中英

insert array value in database

I need to insert array value into table I have the forms which has array value.Now return only first character of an array 's' need to print 'sample1'

<input type="text"  placeholder="First Name" name="first_name[]"/>
<input type="text"  placeholder="Last Name" name="last_name[]">
<input type="text"  placeholder="Age" name="age[]">

codeigniter code

   $fname=$this->input->post('first_name');
   foreach($fname as $name){
       $n=$name['first_name']; //return only first character
     }
  print_r($fname) return an array
    Array ( [0] => sample1 [1] => sample2 )

Just use like this :

foreach($fname as $name){
       $n=$name;
     }

Edit :

$fname=$this->input->post('first_name');
for($i=0;$i<count($fname);$i++) {
    echo $fname[$i];
}

change foreach like this

   foreach($fname as $name){
   $n=$name; //return only first character
 }

this working fine

OR You store all data (first_name) single cell try this

example: sample1,sample2

 $first_names= implode(",",$fname); 

You can use it like this:

foreach($this->input->post("first_name") as $name){
    $n=$name;
}

No need to assign post data to a variable.

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