codeigniter form data is not inserting into MSSQL database - php

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...

Related

Yii ajax dropdown list not working

hello im so confused with my problem
the problem is the ajax didnt update the value
screenshot
while i choose kegiatan ajax didnt react/update.
idk were the error
please help me
this my controler code
public function actionDynamiclocation()
{
$data=Kegiatan::model()->findAll('nid=:nid',
array(':nid'=>(int) $_POST['nid']));
$data=CHtml::listData($data,'nid','lokasi','pengajuan','realisasi');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionAjaxCreate() {$model=new Reportbean;
$outlets = Kegiatan::model()->findAll('nid=:nid',
array(':nid'=>(int) $_POST['nid']));
$model->lokasi=0;
$model->pengajuan=1;
$model->realisasi=2;
$this->renderPartial('_keg ', array('model'=>$model),false,true);
}
and view code
<div class="row">
<?php echo $form->labelEx($model,'kegiatan'); ?>
<?php echo $form->DropDownList($model,'kegiatan',
array(),
array(
'empty'=>'--pilih--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Reportbean/ajaxCreate'), //url to call.
'data'=>array('nid'=>'js:this.value'),
'update'=>'.outlet',
//'update'=>'#'.CHtml::activeId($model,'lokasi'), //selector to update
//'data'=>array('nid'=>'js:this.value'),
))); ?>
<div class="outlet">
<?php
$this->renderPartial('_keg', array('model'=>$model),false,true);
?>
</div>
<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'id'=>'keg',
'attributes'=>array(
'lokasi',
'pengajuan',
'realisasi',
),
));
?>

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;
}

zf2 form validation (zfcAdmin and BjyAuthorize related)

I'm facing validation problems integrating my custom module in zfcAdmin and BjyAuthorize.
My form class:
...
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $field){
if (isset($field['field']))
$this->add($field['field']);
}
...
My filter class:
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $filter){
if (isset($filter['filter']))
$this->add($filter['filter']);
}
...
Fields, filters and other options are retrieved from config file.
Basically everything works fine: form data can be added, edited or deleted from db.
Also after the zfcAdmin module installation no problem rose. Everything works fine using both 'site/mymodule' route and 'site/admin/mymodule' route: i can still add, edit and delete items from db.
Here the problem: I need some form elements (a Select in this particular case) editable/viewable only by administrator. (I can write a new controller/entity class 'ad hoc' for admin but i would like to use the same code for the whole site.)
I installed and configured bjyoungblood/BjyAuthorize module: it allowed me to display some form elements/fields only to admin but when i'm in edit mode a form validation error is displayed: "Value is required and can't be empty"
Here the code:
//view/mymodule/mymodule/update.phtml
<div id="page" style="margin-top: 50px;">
<?php if (isset($this->messages) && count($this->messages) > 0 ): ?>
<?php foreach ($this->messages as $msg): ?>
<div class="alert alert-<?php echo $this->escapeHtmlAttr($msg['type']); ?>">
<?php if (isset($msg['icon'])) echo '<i class="'.$this->escapeHtmlAttr($msg['icon']).'"></i> '; ?><?php echo $this->escapeHtml($msg['message']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
$title = 'Edit Item';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url($this->route . 'mymodule/update', array('action' => 'update', 'id' => $this->id )));
$form->prepare();
$form->setAttribute('method', 'post');
$input = $form->getInputFilter();
?>
<?php echo $this->form()->openTag($form) ?>
<dl class="zend_form">
<?php foreach ($form as $element): ?>
<?php
//CHECK USER PRIVILEDGES
$elName = $element->getName();
$elResource = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['resource'] : "userresource";
$elPrivilege = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['privilege'] : "view";
//SHOW THE ELEMENT IF ALLOWED
if($this->isAllowed($elResource, $elPrivilege)):
?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->formLabel($element) ?></dt>
<?php endif ?>
<?php if ($element instanceof Zend\Form\Element\Button): ?>
<dd><?php echo $this->formButton($element) ?></dd>
<?php elseif ($element instanceof Zend\Form\Element\Select): ?>
<dd><?php echo $this->formSelect($element) . $this->formElementErrors($element) ?></dd>
<?php else: ?>
<dd><?php echo $this->formInput($element) . $this->formElementErrors($element) ?></dd>
<?php endif ?>
<?php else: ?>
<?php
?>
<?php endif ?>
<?php endforeach ?>
</dl>
<?php echo $this->form()->closeTag() ?>
</div>
<div class="clear-both"></div>
My controller action
//controller
public function updateAction(){
$messages = array();
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
$form = $this->getServiceLocator()->get('FormItemService');
$itemMapper = $this->getItemMapper();
$item = $itemMapper->findById($id);
$form->bind($item);
$request = $this->getRequest();
if($request->isPost()){
$form->setData($request->getPost());
if ($form->isValid()) {
die('c');//never here
$service = $this->getServiceLocator()->get('mymodule\Service\Item');
if ( $service->save($form->getData()) )
{
$messages[] = array(
'type' => 'success',
'icon' => 'icon-ok-sign',
'message' => 'Your profile has been updated successfully!',
);
}
else
{
$messages[] = array(
'type' => 'error',
'icon' => 'icon-remove-sign',
'message' => 'Profile update failed! See error messages below for more details.',
);
}
}else{
var_dump($form->getMessages());//Value is required and can't be empty
}
}
return array(
'messages' => $messages,
'form' => $form,
'id' => $id,
'form_options' => $this->getServiceLocator()->get('mymodule_module_options')->getFormSettings(),
'route' => $this->checkRoute($this->getEvent()->getRouteMatch()->getmatchedRouteName())
);
}
If user is not allowed to view the resource, the element is not echoed. So $request->getPost() has no value for that form element and an error is returned by isValid().
Has anyone solved a similar problem or can anyone point me to the right direction?
Thanks
The problem is that you don't do any security check in your FormFilter class, where you define your required fields.
The $form->isValid() function checks the posted data against those filter elements. So it's not enough to prevent the 'echo field' in your view, you still need to apply the security check to the filter element.
One other approach would be to make two forms one for the front end and one for the admin. Since the one for the admin will have the same fields plus one extra select field you can make the admin form extends the front end one. E.g.
class myForm
{
public function __construct(...)
{
// add fields and set validators
}
}
and the admin form could be:
class MyAdminForm extends myForm
{
public function __construct(...)
{
parent::__construct(...);
// add the extra field and extra validator
}
}
In that way even if you edit the front end form (or validators) the back end will always be up to date.
Hope this helps :),
Stoyan

connecting pages in codeigniter

i have a simple question i have a form where user enters his details and when the submit button is clicked whit his details submitted to database it will take the user to a different page i am using codeigniter and i am new to this is there an easy way to do this ? tnx for you help. here is my cmv:
controller
<?php
class Info extends CI_Controller{
function index(){
$this->load->view('info_view');
}
// insert data
function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
}
?>
model
<?php
class Info_model extends CI_Model {
function get_records()
{
$query = $this->db->get('credentials');
return $query->result();
}
function add_record($data)
{
$this->db->insert('credentials', $data);
return;
}
}
?>
view
<html>
<head>
</head>
<body>
<?php echo form_open('info/credentials'); ?>
<ul id="info">
<li>Name:<?php echo form_input('name')?></li>
<li>Second Name: <?php echo form_input('second_name');?></li>
<li>Phone: <?php echo form_input('phone');?></li>
<li>Email: <?php echo form_input('email');?></li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
If all you need is a simple redirect upon submission of the form:
$this->info_model->add_record($data);
redirect('controller/method');
You could use the redirect() function from the URL Helper to actually redirect the user
(http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html)
Like this:
$this->load->helper('url');
redirect('/some/other/page');
Note that this has to be called before any data is outputted to the browser.
Another way of doing it is to simply have two different views that you load depending on the context. Normally you want some form validation as well so you can use that to direct the user. I usually end up with something like this in my function, which is used both for posting the data, inserting it to the database and "redirecting":
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
/* More validation */
if ($this->form_validation->run() !== FALSE) {
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
$this->load->view('some_other_view');
} else {
$this->load->view('info_view');
}
You can also use refresh as a second parameter:
$this->info_model->add_record($data);
redirect('controllerName/methodName','refresh');

insert query is not working in cake 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(...));

Categories