How to solve save data problem into table with cakephp?

Answer: Here i have given example to save data into table
Step 1:
Create table
CREATE TABLE IF NOT EXISTS `titles` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT ‘0’,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)

Step 2:
Controller file controllers/titles_controller.php

class TitlesController extends AppController
{
	var $helpers = array ('Html','Form');
	var $name = 'Titles';
	function index()
	{
		$this-> Title ->recursive = 0;
		$this->set('title', $this->paginate());
	}
	function add()
	{
		if (!empty($this->data))
		{
			if ($this->Title->save($this->data))
			{
				$this->Session->setFlash('Your title has been saved.');
			}
		}
	}
}

Step 3:
Model file title.php
NOTE: You have to check model file when your data is not saved if there is any field required true in the validation component and that field not exist into your form then data is not save into your table problem occurs

class Title extends AppModel {
	var $name = 'Title';
  var $validate = array('title'=>array('rule'=>'notEmpty','message'=>'Please Enter Title','allowEmpty'=>false,'required'=>false));
}

step 4:
View file save into app/views/titles/ folder add.ctp file

 echo $this->Form->create('Title'); 

<fieldset>
<legend>

  __('Add Title');  

</legend>

 echo $this->Session->flash();  
		echo $this->Form->input('title');
	

</fieldset>

 echo $this->Form->end(__('Submit', true)); 

Now successfully your data saved

Advertisements

Installation step for cakephp

Cakephp 1.3.6 Installation step

1.       Download cakephp 1.3.6 version(latest)

2.       Extract folder into your root directory

3.       Change app->config/core.php file line no. 204

              Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi'); 

 

change 1 character in the salt and you’ll be all set!

4.        Change app->config/core.php file line no. 209

Configure::write('Security.cipherSeed', '76859309657453542496749683645'); 

 

change the number in the cipherSeed and you’ll be all set!

5.  App/config directory filename database.php.default Rename to database.php

create databse and change into  database.php file hostname,login username,password and database name and prefix if you want

     var $default = array('driver' => 'mysql',

'connect' => 'mysql_connect',

'host' => 'localhost',

'login' => 'username',

'password' => 'password',

'database' => 'cakephp',

'prefix' => '');

  1. Editing this Page

To change the content of this page, create: APP/views/pages/home.ctp.
To change its layout, create: APP/views/layouts/default.ctp.
You can also add some CSS styles for your pages at: APP/webroot/css.

7.      Your index.php page as default.ctp is running now