CodeIgniter Forums

Full Version: Insert Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]kre8ivdesigns[/eluser]
I have a two column table called features

Code:
CREATE TABLE `features` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `feature` varchar(255),
                PRIMARY KEY(`id`)
            ) ENGINE=InnoDB default charset=UTF8

And during install I want to add the features automcatically.

Code:
INSERT INTO (`features`) VALUES ('Air Conditioning','Alarm','Balcony','Cable/Satellite','Carpeted Floors', 'Disposal','Dishwasher','Fireplace','Gas Range','Hardwood Floors','Microwave','Patio/Deck','Tiled Shower','Washer/Dryer','Waterfront','Wheelchair Access

Whats the easiest way with Codeigniter Active Record. Thanks.

El Forum

[eluser]toopay[/eluser]
Code:
// In controller
$this->model_name->set_initial_data();

// in model
function set_initial_data()
{
   $initial_data = array(
      'Air Conditioning',
      'Alarm',
      'Balcony',
      'Cable/Satellite',
      'Carpeted Floors',
      'Disposal',
      'Dishwasher',
      'Fireplace',
      'Gas Range',
      'Hardwood Floors',
      'Microwave',
      'Patio/Deck',
      'Tiled Shower',
      'Washer/Dryer',
      'Waterfront',
      'Wheelchair Access'
   );

   $this->db->truncate('features');
   foreach($initial_data as $single_data) $this->db->insert('features', array('feature' => $single_data));
}

El Forum

[eluser]Madoc[/eluser]
Your insert synthax is wrong. I believe that that correct synthax for quick insert is:
Code:
INSERT INTO (`features`) VALUES ('Air Conditioning'),('Alarm'),('Balcony'),('Cable/Satellite'),('Carpeted Floors');

I do not think using Active records is that relevant in this case (hard coded inserts).

reference to mysql insert:
http://dev.mysql.com/doc/refman/5.5/en/insert.html

El Forum

[eluser]toopay[/eluser]
[quote author="Madoc" date="1303572429"]Your insert synthax is wrong. I believe that that correct synthax for quick insert is:
Code:
INSERT INTO (`features`) VALUES ('Air Conditioning'),('Alarm'),('Balcony'),('Cable/Satellite'),('Carpeted Floors');
[/quote]
What you mean wrong? Its a valid synthax. And if you want to use general SQL statement, it should be
Code:
INSERT INTO features VALUES (NULL,'values_1'),(NULL,'values_2'),(NULL,'values_3');
or, like my above statement
Code:
INSERT INTO features (feature) VALUES ('values_1'),('values_2'),('values_3');
Your code will generates error, since it not specify the column name or the 'id' column value!
[quote author="Madoc" date="1303572429"]
I do not think using Active records is that relevant in this case (hard coded inserts).[/quote]
Why not relevant? AR, in codeigniter, is the safest way to insert sql statement and makes your code cleaner!

El Forum

[eluser]kre8ivdesigns[/eluser]
Thanks toopay it worked great!!