insert query is not working in cake php - php

this is action url: http://localhost/carsdirectory/users/dashboard.
dashboad.ctp (i have select filed and in this select field i m fetching data from that filed car_type and table name car_types)
<?php echo $this->Form->create('User', array('type' => 'file', 'action' => 'dashboard')); ?>
<label class="ls-details-label">Type</label>
<div class="ls-details-box">
<?php
foreach ($car_types as $car_type)
{
$car_type_new[$car_type['Car_type']['id']]=
$car_type['Car_type']['car_type'];
}
echo $this->Form->input('car_type',
array( 'label'=>false,
'options'=>$car_type_new,
'empty'=>' Select ',
'class'=>'styledselect_form_1'));
?>
</div>
<?php echo $this->Form->end(array('label' => 'Submit',
'name' => 'Submit',
'div' => array('class' => 'ls-submit')));?>
users_controller.php (controller)
class UsersController extends AppController{
var $name = "Users";
public function dashboard(){
$this->loadModel('Car_type'); // your Model name => Car_type
$this->set('car_types', $this->Car_type->find('all'));
if(!empty($this->data))
{
$this->loadModel('Car');
if($this->Car->save($this->data))
{
$this->Session->setFlash('Detail has Been Saved');
$this->redirect(array('action'=>'dashboard'));
}
else
{
$this->Session->setFlash('Detail could not save');
}
}
}
car.php (model)
<?php
class Car extends appModel{
var $name = "Car";
}
?>
i want to inset data car_type_id field in (table name cars) , but i m not able to do it
so plz help me
thanks in advance, vikas tyagi

You may try this:
echo $this->Form->input('Car.car_type_id', array(...));

Related

is active field name breaking this form

I've been getting weird behaviour since I added the active checkbox to this form, the Submit button can only be clicked outside of the the text, and the checked status of the active field is not registering when the form is submitted, it always shows as true. I checked to see if active was a reserved word because the behaviour is so funky, but to no avail. Am I being a noob?
<div class="add-form">
<?= $this->Form->create($member) ?>
<legend><?= __('Edit Member') ?></legend>
<?php
echo $this->Form->control('membership_no');
echo $this->Form->control('password');
echo $this->Form->control('email');
echo $this->Form->control('company_id', ['options' => $companies]);
echo $this->Form->control('terms_agreed');
echo $this->Form->control('change_password');
echo $this->Form->control('active');
?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
in case it's of any import here is the migration where I added the active column
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();
}
}
Try with other field name once.
'default'=>true may be returning value '1' ...Remove and check the output.
That is
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active_col', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();
}
}
OR
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active', 'boolean', [
'null' => false,
]);
$table->update();
}
}

Incrementing Shared Value in Database

I want to create a polling system where user can select radio button and the choice will be incrementing in database
insert_view.php - Simple form insertion for selecting the required fields
<html>
<body>
<?php echo form_open('insert_ctrl'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER> <br>
<?php } ?>
<?php echo "which language do you prefer..?";?><br/>
<?php echo form_radio(array('id' => 'rad1', 'name' => 'rad1','value'=>0)); ?>
<?php echo form_label('JAVA'); ?> <?php echo form_error('rad1'); ?><br />
<?php echo form_radio(array('id' => 'rad2', 'name' => 'rad2','value'=>0)); ?>
<?php echo form_label('PHP'); ?> <?php echo form_error('rad2'); ?><br />
<?php echo form_radio(array('id' => 'rad3', 'name' => 'rad3','value'=>0)); ?>
<?php echo form_label('C\C++'); ?> <?php echo form_error('rad3'); ?><br />
<?php echo form_radio(array('id' => 'rad4', 'name' => 'rad4','value'=>0)); ?>
<?php echo form_label('DOTNET'); ?> <?php echo form_error('rad4'); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</body>
</html>
insert_ctrl.php - for sending the request through the help of controller from the view to the model.
<?php
class insert_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('insert_model');
}
function index() {
$data = array(
'Option1' => $this->input->post('rad1'), //for selecting the required option
'Option2' => $this->input->post('rad2'), //for selecting the required option
'Option3' => $this->input->post('rad3'), //for selecting the required option
'Option4' => $this->input->post('rad4')
);
$vote = $this->input->post('Submit');
if($vote=="Option1"){
$value=$value+1;
}
elseif($vote=="Option2"){
$value=$value+1;
}
elseif($vote=="Option3"){
$value=$value+1;
}
elseif($vote=="Option4"){
$value=$value+1;
}
//Setting values for tabel columns
//Transfering data to Model
$this->insert_model->form_insert($data);
//$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view', $data);
}
}
?>
insert_model.php
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(students) of Database(college)
$this->db->insert('students', $data);
}
}
?>
Class name should start with capital letter,
class Insert_ctrl extends CI_Controller {
If the value to be incremented is in same table,then pass the value to model,
$data['value'] = $value;
$this->insert_model->form_insert($data);
And first of all what is the value of $value.??
If the value is present in different table,increment like this,
Update table name set value = value + 1 where question_id = id.
Instead of so many if else,
if($vote=="Option1" || $vote=="Option2" || $vote=="Option3" || $vote=="Option4"){
$value=$value+1;
}

codeigniter form data is not inserting into MSSQL database

I have a simple form setup. It shows up fine and when I hit submit looks like it is working, but when I go to check the datat in MSSQL Server it is not there. I am not sure why it is not working.
Controller is insert.php
<?php
class insert extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('insert_model');
}
function index()
{
// Including Validation Library
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// Validating Name Field
$this->form_validation->set_rules('EmpName', 'Employee_Name', 'required|min_length[3]|max_length[15]');
// Validating Email Field
$this->form_validation->set_rules('Department', 'Department', 'required|min_length[3]|max_length[15]');
// Validating Email Field
$this->form_validation->set_rules('LanID', 'LanID', 'required|min_length[3]|max_length[15]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('insert_view');
}
else
{
// Setting Values For Tabel Columns
$data = array(
'Employee_Name' => $this->input->post('EmpName'),
'Department' => $this->input->post('Department'),
'LanID' => $this->input->post('LanID'),
);
// Transfering Data To Model
$this->insert_model->form_insert($data);
// Loading View
$this->load->view('insert_view');
}
}
}
?>
Model is insert_model.php
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(requests) of Database(employee)
$this->db->insert('requests', $data);
}
}
?>
View is insert_view.php
<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>
<div id="container">
<?php echo form_open('insert'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1>
<?php echo form_label('Employee Name :'); ?> <?php echo form_error('EmpName'); ?>
<?php echo form_input(array('id' => 'EmpName', 'name' => 'EmpName')); ?>
<?php echo form_label('Department :'); ?> <?php echo form_error('Department'); ?>
<?php echo form_input(array('id' => 'Department', 'name' => 'Department')); ?>
<?php echo form_label('LanID :'); ?> <?php echo form_error('LanID'); ?>
<?php echo form_input(array('id' => 'LanID', 'name' => 'LanID')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit'));?>
<?php echo form_close(); ?>
</div>
</body>
</html>
have you tried debugging?
in your controller:
$data = array(
'Employee_Name' => $this->input->post('EmpName'),
'Department' => $this->input->post('Department'),
'LanID' => $this->input->post('LanID'),
);
does all array index matches exactly with your database table's column name?
in your model:
print_r($data);
before:
$this->db->insert('requests', $data);
if $data contains data, try set function.
$this->db->set('Employee_Name', $EmpName);
$this->db->set('Department', $Department);
$this->db->set('LanID', $LanID);
$this->db->insert('requests');
try debugging...

How to use different model from the controller?

I am trying to insert record using Cakephp.My model name is something like User.php.
And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :
View :
<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
<div class="row-fluid">
<div class="span5">
<label class="">*First Name</label>
<?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
<div class="span5">
<label class="">*Last Name</label>
<?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
My controller code is given below :
class SignupsController extends AppController {
var $name = 'Signups';
var $uses=array("User");
public function registration()
{
$this->layout="reserved";
$this->Club->create();
if (isset($_POST['registration'])) {
echo "This is";
echo "<pre>";print_r($this->request->data);echo"</pre>";
$this->User->save($this->request->data);
//$this->Session->setFlash(__('Promoter registration has been done successfully'));
//$this->redirect('registration');
//$this->redirect(array('action' => 'registration'));
}
}
}
My model name is different which's name is User.php
I want to insert the record using this above code.Any idea how to insert?
you can do this by loading the users model in current controller just write the following line
$this->loadModel('Name of the Model').
then
$this->nameofmodel->save()
As you are unable to understand see this
Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the controller’s default model or its associated model:
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);
$this->loadModel('User', 2);
$user = $this->User->read();
Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand
you can use it with $uses variable in SignupController
class SingupController extends AppController
{
public $uses = array('User');
//rest of stuff
}
Or, if you want, you can load it on-demand inside a method:
$this->loadModel('User'); //now model is loaded inside controller and used via $this->User
EDIT: Your data array has to include the name of the model you're saving. So, replace:
$this->Form->create('Signups',array('action' => 'registration')
with:
$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));

Show the user's username in post view w/ related comment

I am a newbie in CakePHP 1.3... I want to display or show the USER's username instead of USER's id, who post the COMMENTS in the POST view... Anyone can help me please?
Here is the model association I made:
POSTS 'has many' COMMENTS
COMMENTS 'belongs to' USERS
POST->COMMENT->USER
I already read the Containable Behavior of CakePHP 1.3, but still I can't understand it well... Please help me what codes to put in the post_controller's view & view.ctp that can show the related's related table in the POST view.
And How to call the USER's data in the POST view.
I'm still confused.
Thanks in Advance, Azure
Assumptions
You have three tables as below
(1) Posts
* id
* title
(2) Comments
* id
* post_id
* user_id
(3) Users
* id
* name
View Function in PostsController.php file
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$this->Post->recursive=2;
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
Content of view.ctp file in app/View/Posts/ folder
<!-- File: /app/View/Posts/view.ctp -->
<h1><?php echo 'Post ID : '.h($post['Post']['id']); ?></h1>
<h1><?php echo 'Post Title : '.h($post['Post']['title']); ?></h1>
<?php
echo 'Comments By Users : ';
if(!empty($post['Comment'])){
foreach ($post['Comment'] as $key=>$value){?>
<p>User Name : <?php echo $value['User']['name'];?></p>
<?php }
}
else {
echo '<br/>';
echo 'No Comments Yet';
} ?>
Model File : User.php
<?php
class User extends AppModel {
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
)
);
}
?>
Model File : Comment.php
<?php
class Comment extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
Model File : Post.php
<?php
class Post extends AppModel {
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
)
);
}
?>
I assume you are saving user id in comments table and user names are in users table who posts the comment, then use the below solution
In Controller method:
$userdata=$this->User->find('all',array('fields'=>array('id','username'),'recursive'=>-1));
$userid=Set::extract('/User/id', $userdata);
$username=Set::extract('/User/username', $userdata);
$data=array_combine($username,$userid);
$this->set('name',$data);
In View:
$cid=$var['Comment']['user_id'];
$username=array_search($cid, $name);
echo $username;

Categories