Refreshing page null record automatically inserting in database - php

I am using codeigniter MVC pattern . Whenever i am refreshing the page, Null record automatically inserting in database. In database, I set the all column as null except primary key.
Please help me.
View page code(index.php)
<div class="title">
<p><strong>ADD USER</strong></p>
</div>
<?php echo form_open("Welcome/index"); ?>
<input type="text" name="userId" placeholder="USER ID" style="width:500px">
<input type="date" name="date" style="width:500px">
<input type="text" name="firtsname" placeholder="FIRST NAME" style="width:330px">
<input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:330px">
<input type="text" name="lastname" placeholder="LAST NAME" style="width:330px">
<input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:500px">
<input type="text" name="landline" placeholder="LANDLINE No." style="width:500px">
<textarea name="address" placeholder="ENTER ADDRESS"></textarea>
<input type="text" name="city" placeholder="CITY" style="width:500px">
<input type="text" name="locality" placeholder="LOCALITY" style="width:500px">
<input type="text" name="email" placeholder="ENTER EMAIL" style="width:1010px"></br>
<input type="submit" class="submit" name="SUBMIT">
<?php echo form_close(); ?>
</div>
Model page code(model_add_user.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_add_user extends CI_Model {
public function __construct() {
parent:: __construct();
$this->load->database();
}
public function add_user() {
$data = array(
'userId' => $this->input->post('userId'),
'date' => $this->input->post('date'),
'firtsname' => $this->input->post('firtsname'),
'middlename' => $this->input->post('middlename'),
'lastname' => $this->input->post('lastname'),
'mobileno' => $this->input->post('mobileno'),
'landline' => $this->input->post('landline'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'locality' => $this->input->post('locality'),
'email' => $this->input->post('email'),
);
$this->db->insert('add_user', $data);
}
}
?>
Controller page code(Welcome.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
//$this->load->view('welcome_message');
$this->load->view('index');
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
}
?>

In your controller make the following change :
public function index() {
if ($this->input->post()) {
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
$this->load->view('index');
}

Path up to controller name directly hit the index function of your controller. Here in your code you load view and call add_user function.So every time you refresh the page your model call and null insert into your database
You need to create a separate function for your insert data
public function index()
{
//$this->load->view('welcome_message');
$this->load->view('index');
}
function add_user()
{
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
And change form action to
<?php echo form_open("Welcome/add_user"); ?>

After successful insertion.. Just redirect it to... function / controller it will refresh the page and prevent the form submission again.
public function index() {
//$this->load->view('welcome_message');
$this->form_validation->set_rules('field', 'Field', 'trim|required');
if ($this->form_validation->run()) {
$this->load->model('model_add_user');
$this->model_add_user->add_user();
redirect('welcome')
} else {
$this->load->view('index');
}
}

Related

Html disappear after using form_error function in codeigniter

I am trying to add the form error messages in my html form. Problem is there in first view. In user function html is disappear from where I start to use form_error(). My Original form design is like this: my original form
but after adding the form_error under the first input: my form error image
Here is my html code
<div class="form-group">
<label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
<input type="text" name="username" id="name" placeholder="Your Name" />
<?php echo form_error('username','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
<div class="form-group">
<label for="email"><i class="zmdi zmdi-email"></i></label>
<input type="text" name="email" id="email" placeholder="Your Email"/>
<?php echo form_error('email','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
Here is my controller
<?php
class Register extends CI_Controller
{
public function user()
{
$this->load->view('files/registration/signup');
}
public function login()
{
$this->load->view('files/registration/login');
}
public function description()
{
$this->load->view('files/registration/description');
}
public function registerMe()
{
$this->load->helper('form','url');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|alpha|max_length[15]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('re_pass', 'Confirm Password', 'required|matches[pass]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run()===FALSE) {
$this->load->view('files/registration/signup');
}
else
{
$this->load->model('RegisterUser');
$this->RegisterUser->registerme($_POST);
}
}
}
?>
you need to load the form helper in signup function too. form_error is a function in form_helper. (this is required for other functions with a form view too)
public function user()
{
$this->load->helper('form');
$this->load->view('files/registration/signup');
}
html is disappeared because there was php error. check your error log when this happens or change codeigniter environment to development in index.php

How to insert form value in ci?

controller: Test.php
<?php
class Test extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->model('Users_model');
$this->Users_model->insert_user();
$this->load->view('home');
}
}
?>
Model: User_model.php
<?php
class Users_model extends CI_Model
{
public function insert_user()
{
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);
return $this->db->insert('user', $data);
}
}
?>
view: home.php
<form method="post">
<label for="name">your Name</label>
<input class="form-control" id="name" type="text" placeholder="Name" name="name" />
<label for="email">Your email</label>
<input class="form-control" id="email" type="text" placeholder="Email" name="email" />
<label for="message">Your Message</label>
<textarea class="form-control" id="message" placeholder="Message" name="message"></textarea>
<button type="submit">send</button>
</form>
I am new in codeigniter here I want to insert form value into database but it showing some error i.e.
how can I fix this error ? please help.
Thank You
You got the errors because you call User_model->insert_user() before any data submitted in the form. You should check if there is any posted data then save to the database. Here is the example of your controller should be:
<?php
class Test extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->model('Users_model');
// do checking right here
if (!empty($this->input->post('name'))) {
$this->Users_model->insert_user();
}
$this->load->view('home');
}
}
You forget to pass POST data in insert method of model in controller .
Contoller
<?php
class Test extends CI_Controller {
public function index()
{
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);
$this->load->helper(array('form', 'url'));
$this->load->model('Users_model');
$this->Users_model->insert_user($data );
$this->load->view('home');
}
}
Model
<?php
class Users_model extends CI_Model
{
public function insert_user($data = [])
{
return $this->db->insert('user', $data);
}
}
?>
You extend CI_Model But did not call parent construct. Use this:
function __construct() {
// Call the Model constructor
parent :: __construct();
}
Hope this will work. Thanks.
In your "home.php" you haven't specify an action attribute. Due to this form value is not posting.
for example:- <form action="<?php echo base_url();?>YourfileName/DeclaredFunctionName" method="post">
and then try to fetch the value of your form in your controller.
In controller please execute this function insert_user() { print_r($_POST); exit(); }
If you are getting values in your browser, then remove exit() and execute your code. Otherwise, please share your error here.
Thanks

How to use post method via MVC

This is my view file called application.html.
I just started learning the Zend framework.
I have existing database table (mytable.sql), with three columns:
ID
APP_ID
APP_NAME
So now my goal is, I need create new app using post method and that new app name must be save in to database.
<form method="post" action="/admin/appmgt/createAction">
<p>
<label class="field" for="APP_ID"><span>*</span>APP_ID</label>
<input type="text" name="APP_ID" class="textbox-300" />
</p>
<p>
<label class="field" for="APP_NAME"><span>*</span>APP_NAME</label>
<input type="text" name="APP_NAME" class="textbox-300" />
</p>
<input type='submit' name='submit' value='Submit Form' />
</form>
This my controller file:
public function createAction() {
$this->view->form = $this->getForm();
if ($this->getRequest()->isPost()) {
if (!$this->view->form->isValid($_POST)) {
return $this->render('form');
}
}
}
This is my Model file:
public function createApp($APP_ID, $APP_NAME) {
$data = array(
'APP_ID' => $APP_ID,
'APP_NAME' => $$APP_NAME
);
$this->insert($data);
}
Could you please help me ?

Unidentified Property in Control in my codeigniter framework

I am new to frameworks and trying to send values to database table, I have done it simply but now I am using templates (the famous alemsaeed free AdminLTE) now after setting BASEPATH and giving database connection in database.php,I am still getting errors.
However the code looks fine but I don't know please kindly tell me the reason it's my 2nd week in codeigniter.
MODEL
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mdl extends CI_Model {
function __construct() {
parent::__construct();
}
function form_insert($data){
$data = array(
'name' => $this->input->post('name'),
'fname' => $this->input->post('fname'),
'dob' =>$this->input->post('dob')
);
$this->db->insert('folio', $data);
}
}
?>
Controller
public function index() {
$this->load->helper(array('form','url'));
$this->load->view('myself');
}
public function eid() {
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
$this->model->form_insert();
$this->load->view('edu');
}
}
?>
View
Note:i have shorted the whole view code because it's a template
For Form Loading the usual
<?php
echo form_open(site_url().'/ctrl/eid/');
$attributes = array('class' => 'sidebar-form');
?>
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</div>
<div class="box-footer">
Copyright © 2015-2016 PIET MULTAN.</strong> All rights reserved.
</div>
</div>
Remove argument $data from form_insert method of model. Like: public function form_insert() {/* rest of code here */} because you are not passing anything from controller. Although I would advice you that you do that passing from controller like:
public function eid()
{
//controller
$data = array();
$data['name'] => $this->input->post('name'),
$data['fname'] => $this->input->post('fname'),
$data['dob'] =>$this->input->post('dob'),
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
if ( $this->model->form_insert($data) )
{
//success view
}
else
{
//insert fail view
}
}
function form_insert($data)
{
//model
$this->db->insert('folio', $data);
return $this->db->affected_rows() > 0;
}
In Controller
function __construct()
{
parent::__construct();
//load default helpers in this
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('mdl');
}
public function index()
{
$this->load->view('myself');
}
public function eid()
{
$name = $_POST['name'];
$fname = $_POST['fname'];
$dob = $_POST['dob'];
if(!empty($name) || !empty($fname) || !empty($dob))
{
$this->Mdl->form_insert($name, $fname, $dob);//Model name should be Mdl
$this->load->view('edu');
}
else
{
//fields are empty
}
}
In Model
function form_insert($name, $fname, $dob)
{
$data = array(
'name' => $name,
'fname' => $fname,
'dob' => $dob
);
$this->db->insert('folio', $data);
}
In View (Form should be)
<form action="<?php echo base_url();?>index.php/ctrl/eid" method="post" class="sidebar-form">
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<label>Date Of Birth</label>
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</form>
The Problem was in the Controller
it should be the modelfileName not model in the code where we load the function
$this->mdl->form_insert();
And this did work :D
The Next problem was in the sitemap
it was this before
$config['site_url']='../../folio/';
instead of this
$config['site_url']='http://localhost/folio/index.php/';
But Once Again i appreciate all for your help it really means to me a lot .
THK_STACOVRFLW

Codeigniter can't login

Here is the controller, when I click the login button, nothing happens. What I want is to load the success screen when user data is validated and show error messages when user data is not validated.
I have set my base_controller as Login
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function login(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
here is the view
<?php echo validation_errors(); ?>
<form method="post">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
When I click the login button, nothing happens. What am I doing wrong?
You have to give action attribute in form tag, like this:
action="http://yoursitename.com/controllername"
in your case controllername is login.
For more help, you can refer:
[https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html][1]
Hope this help!
this link shows how form_open() works, one of codeigniter's utility functions from the form helper libary.
In your case you would want this line of code:
<?php echo form_open('/login'); ?>
or something that you want to be the login url.
This line would replace
<form method="post">
in your html and when rendered would be something like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login" />
If you aren't familiar with URI routing, then you should read about that here](https://ellislab.com/codeigniter/user-guide/general/routing.html). I would recommedn setting up a route for you login, but the default format for a url is
example.com/class/function/id/
so yours might look like
example.com/login/login
And form_open() would then look like (even though its kind of cluncky)
<?php echo form_open('/login/login'); ?>
You can try below code
login.php controller file
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function validate(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
Here I have loaded one helper to give action url in view file
$this->load->helper('url');
Also I have changed function name from login to validate as function name should not be similar to class name as constructor is already defined there
login.php view file
<?php echo validation_errors(); ?>
<form method="post" action = "<?php echo site_url("login/validate"); ?>">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
Hope this will help you

Categories