create function to validate existing user / new user - php

How do i check if the user already exist in the database. I can create user but not very sure how to check if the user already exist in the databas. here are my code:
view file
<?php echo $this->navigasi->top(); ?>
<div class="container">
<br>
<h4 style="margin:0 auto;width:650px;">CREATE USER ACCOUNT</h4>
<br>
<form class="form-horizontal the-form" method="post" action="<?php echo base_url(); ?>admin/register_account">
<?php echo $this->session->flashdata('mesej'); ?>
<div class="control-group">
<label class="control-label">Name Staff:</label>
<div class="controls">
<input type="text" required name="nama_staf" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label">Password:</label>
<div class="controls">
<input type="password" required name="kata_laluan" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label"> Email:</label>
<div class="controls">
<input type="email" name="email" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label">Position:</label>
<div class="controls">
<select name="jawatan" class="span3" id="jawatan">
<option value="1">Clerk</option>
<option value="2">Technician</option>
<option value="3">Assitant officer</option>
<option value="4">Officer</option>
<option value="5">Director</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">No. Staff:</label>
<div class="controls">
<input type="text" required name="no_staf" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label"></label>
<div class="controls">
<button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i> Register</button>
controller file
class Admin extends MY_Controller {
public function index()
{
$session_data = $this->session->userdata('account');
$data['sesi_jenis'] = $session_data['jenis'];
if($data['sesi_jenis'] < 1)
{
redirect('utama');
} else {
$this->load->view('view-utama-pentadbir');
}
}
public function register()
{
$this->load->view('view-create-account');
}
public function register_account()
{
$query = $this->modeluser->createAccount();
$this->session->set_flashdata('mesej', '<span class="label label-info">Account created!</span> ');
redirect(base_url().'admin/register');
model file
class ModelUser extends CI_Model {
public function creatAccount()
{
$nameStaf = $_POST['nama_staf'];
$noStaf = $_POST['no_staf'];
$email = $_POST['email'];
$password = sha1($_POST['password']);
$jenis = 0; // user is 0 - admin is 1
$position = $_POST['position'];
$this->db->query("INSERT INTO akaun (nama_staf,no_staf,email,password,jenis,position) VALUES ('$namaStaf','$noStaf','$email','$password','$jenis','$position')");
}
public function padamAkaun($no_staf)
{
$this->db->query("DELETE FROM akaun WHERE no_staf = '$no_staf'");
}

In your controller function set validation rule as:
$this->form_validation->set_rules('email', 'Email', trim|required|valid_email|is_unique[tabelname.columnname]|xss_clean');

change model as per.considering email is unique for each user
class ModelUser extends CI_Model {
public function creatAccount()
{
$nameStaf = $_POST['nama_staf'];
$noStaf = $_POST['no_staf'];
$email = $_POST['email'];
$password = sha1($_POST['password']);
$jenis = 0; // user is 0 - admin is 1
$position = $_POST['position'];
if ($this->checkEmailExist($email) == false)
return 'ERROR! DUPLICATE USER';// or handle as you like
$this->db->query("INSERT INTO akaun (nama_staf,no_staf,email,password,jenis,position)
VALUES ('$namaStaf','$noStaf','$email','$password','$jenis','$position')");
}
public function padamAkaun($no_staf)
{
$this->db->query("DELETE FROM akaun WHERE no_staf = '$no_staf'");
}
private function checkEmailExist($email)
{
$this->db->where('email', $email);
$query = $this->db->get('akaun');
if( $query->num_rows() == 0 ){ return TRUE; } else { return FALSE; }
}

$this->form_validation->set_rules('email', 'Email','trim|required|valid_email|is_unique[tabelname.columnname]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == TRUE)
{
// action after validation success.
}else{
//action after validation failure.
}

Related

Codeigniter form_open_multipart didn't respond

Been trying out codeigniter and wanted to create a form with file upload using the upload class. However when i try to save the form, it didn't respond and just show the same page. I've been looking around, but I can't seem to find out how to make this work.
This is the controller Product.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Product extends CI_Controller{
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
}
public function index(){
$data['row'] = $this->product_m->get();
$this->template->load('template', 'product/data/product_data', $data);
}
public function add(){
$product = new stdClass();
$product->product_id = null;
$product->product_name = null;
$product->customer_name = null;
$product->supplier_name = null;
$product->weight = null;
$product->product_date = null;
$product->expired_date = null;
$product->image = null;
$product->barcode = null;
$data = array(
'page' => 'add',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}
public function edit($id){
$query = $this->product_m->get($id);
if($query->num_rows()>0){
$product = $query->row();
$data = array(
'page' => 'edit',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}else{
echo "<script>alert('Data not found');";
echo "window.location='".site_url('product')."';</script>";
}
}
public function process(){
$post = $this->input->post(null, TRUE);
if(isset($_POST['add'])){
$config['upload_path'] = './uploads/product';
$config['allowed_types'] = 'jpeg|pdf|jpg|png|doc|docs|xls|xlsx';
$config['max_size'] = 5120;
$config['file_name'] = 'product-'.date('ymd').'-'.substr(md5(rand()),0,10);
$this->load->library('upload', $config);
if(#$_FILES['image']['name'] !=null){
if($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}else{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('product/add');
}
}
else{
$post['image'] = null;
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}
}else if(isset($_POST['edit'])){
$this->product_m->edit($post);
}
}
public function del($id){
$this->product_m->del($id);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('Success', 'Data successfully deleted');
}
redirect('product');
}
}
Script for the form product_form.php
<section class="content-header">
<h1>
Product
<small>Add product</small>
</h1>
</section>
<section class ="content">
<div class="box">
<div class="box-header">
<h3 class="box-title"> product</h3>
<div class="pull-right">
<a href="<?=site_url('product')?>" class="btn btn-warning btn-flat">
<i class="fa fa-undo"></i> Back
</a>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php echo form_open_multipart('product/process');?>
<div class="form-group">
<label>Product Name *</label>
<input type="hidden" name="id" value="<?=$row->product_id?>">
<input type="text" name="product_name" value="<?=$row->product_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Customer Name *</label>
<input type="text" name="customer_name" value="<?=$row->customer_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Supplier Name *</label>
<input type="text" name="supplier_name" value="<?=$row->supplier_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Weight(kg) *</label>
<input type="number" name="weight" value="<?=$row->weight?>" class="form-control" required>
</div>
<div class="form-group">
<label>Product Date(dd/mm/yyyy) *</label>
<input type="text" name="product_date" value="<?=$row->product_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Expired Date(dd/mm/yyyy) *</label>
<input type="text" name="expired_date" value="<?=$row->expired_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Technical Data</label>
<input type="file" name="image" class="form-control" >
</div>
<!-- FORM BARCODE -->
<div class="form-group">
<label>Barcode</label>
<input name="barcode" class="form-control" value="
<?php
$angka = rand(1000000,9999999);
$random = rand(0,25);
$random1 = rand(0,25);
$random2 = rand(0,25);
$name = array("A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
echo $name[$random],$name[$random1],$name[$random2],$angka;
$row->barcode
?>" readonly>
</div>
<div class="form-group">
<button type="submit" name="<?=$page?>" class="btn btn-success btn-flat">
<i class="fa fa-paper-plane"> Save</i>
</button>
<button type="Reset" class="btn btn-flat"> Reset</button>
</div>
<!-- </form> -->
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</section>
Load the form helper.
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
$this->load->helper('form');
}

session in codeigniter not working

The session is not working properly. when login, it is directing member_area function which is correct but in member_area function if the condition is not working. The result is directly going in else part of member_area function that is output dies. Is there any mistake in member_area function if condition?
This is my main controller i.e., Welcome controller
function login()
{
$this->form_validation->set_rules('email', 'E-mail', 'valid_email');
$this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
if ($this->form_validation->run()==FALSE)
{
$data['view'] = 'login_view';
$this->load->view('load_view',$data);
}
else
{
$email=$_POST['email'];
$password = md5($_POST['password']);
$this->load->model('Sample_model');
$credentials = array(
'email' => $_POST['email'],
'password' => md5($_POST['password'])
);
$user = $this->Sample_model->check_user($credentials);
if($user->num_rows() == 1)
{
$user = $user->row();
$session = array(
'name' => $user->name,
'is_logged_in' => TRUE
);
$this->session->set_userdata($sesson);
redirect('welcome/member_area');
}
else {
$data['view'] = 'error_view';
$data['msg'] = 'Login failed';
$this->load->view('load_view',$data);
}
}
}
public function member_area()
{
if($this->session->userdata('is_logged_in'))
{
$data['view'] = 'memberarea_view';
$this->load->view('load_view',$data);
}
else die('die');
}
I have added session in config/Autoload
$autoload['libraries'] = array('database','session','form_validation');
This is the model used in the present login.
Sample_model
<?php
class Sample_model extends CI_Model {
public function __construct()
{
//Call the CI_Model constructor
parent::__construct();
}
public function add_user($user)
{
return $this->db->insert('users', $user);
}
public function check_user($credentials)
{
$this->db->where($credentials);
return $this->db->get('users');
}
}
This is login_view form. I am entering email and password from this form.
<section class="title">
<div class="container">
<div class="row-fluid">
<div class="span6">
<h1>Login</h1>
</div>
<div class="span6">
<ul class="breadcrumb pull-right">
<li>Home <span class="divider">/</span></li>
<li>Pages <span class="divider">/</span></li>
<li class="active">Login</li>
</ul>
</div>
</div>
</div>
</section>
<!-- / .title -->
<section id="login-page" class="container">
<form class="center" action='' method="POST">
<fieldset class="login-form">
<div class="control-group">
<!-- E-mail -->
<div class="controls">
<input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="control-group">
<!-- Password-->
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
<?php echo form_error('password'); ?>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success btn-large btn-block">Login</button>
</div>
</div>
</fieldset>
</form>
</section>
<!-- /#login-page -->
Libraries
$autoload['libraries'] = array('database','session','form_validation');
helper
$autoload['helper'] = array('url', 'file');
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function login()
{
$this->form_validation->set_rules('email', 'E-mail', 'valid_email');
$this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
if ($this->form_validation->run()==FALSE)
{
$data['view'] = 'login_view';
$this->load->view('login_view',$data);
}
else
{
$email=$_POST['email'];
$password = md5($_POST['password']);
$this->load->model('Sample_model');
$credentials = array(
'email' => $_POST['email'],
'password' => md5($_POST['password'])
);
$user = $this->Sample_model->check_user($credentials);
if($user->num_rows() == 1)
{
$user = $user->row();
$session = array(
'name' => $user->name,
'is_logged_in' => TRUE
);
$this->session->set_userdata($session);
redirect('welcome/member_area');
}
else {
$data['view'] = 'error_view';
$data['msg'] = 'Login failed';
$this->load->view('login_view',$data);
}
}
}
public function member_area()
{
if($this->session->userdata('is_logged_in'))
{
$data['view'] = 'member_view';
$this->load->view('member_view',$data);
}
else{ die('die');
}
}
function logout()
{
$newdata = array(
'name' =>'',
'is_logged_in' => FALSE,
);
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
redirect('welcome/login','refresh');
}
}
Model
class Sample_model extends CI_Model {
public function __construct()
{
//Call the CI_Model constructor
parent::__construct();
}
public function add_user($user)
{
return $this->db->insert('users', $user);
}
public function check_user($credentials)
{
$this->db->where($credentials);
return $this->db->get('users');
}
}
Login View
<div class="control-group">
<!-- E-mail -->
<div class="controls">
<input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="control-group">
<!-- Password-->
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
<?php echo form_error('password'); ?>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success btn-large btn-block">Login</button>
</div>
</div>
</fieldset></form></section>
Member View
Welcome Member

How to starts adding data to MySQL table with id of !=0

I have a page to add new users to the MySQL with PHP. And the problem with this is, that it sets id of a new user to 0 whenever it runs. So I don't want that.. I want it to start from id of 1 and if it exists in table, try more than that like 2 for example.
I don't think for this example, you won't need to look at my php files but if you do, here you can see the admin_new.php:
<?php
$notice = array();
if (isset($_POST['submit'])){
$username = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['pass'];
$groups = $_POST['groups'];
if($groups == "Main Admin"){
$level = 1;
}else if($groups == "Administrator"){
$level = 2;
}else if($groups == "Content Creator"){
$level = 3;
}else if($groups == "Social Media Manager"){
$level = 4;
}else{
$level = 5;
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$notice['email'] = "The email that you have entered is not a valid one";
}else{
$registration = new Register();
$registration->CheckUname($username,$email,$password,$groups,$level);
}
}
?>
<div class="content-wrapper">
<section class="content-header">
<h1>
Add New Admin
<small>You can add new admin here</small>
</h1>
<ol class="breadcrumb">
<li class="active">addnewadmin.php</li>
</ol>
</section>
<?php
if(isset($notice['validation_email'])) {
echo "
<div class='alert alert-danger'>
<strong>Hey!</strong> ".$notice['validation_email'].".
</div>
";
}
if(isset($notice['username_exists'])) {
echo "
<div class='alert alert-danger'>
<strong>Hey!</strong> ".$notice['username_exists'].".
</div>
";
}
if(isset($notice['email_exists'])) {
echo "
<div class='alert alert-danger'>
<strong>Hey!</strong> ".$notice['email_exists'].".
</div>
";
}
if(isset($notice['success_message'])) {
echo "
<div class='alert alert-danger'>
<strong>Hey!</strong> ".$notice['success_message'].".
</div>
";
}
?>
<section class="content">
<div class="row">
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Required Information</h3>
</div>
<form role="form" method="POST" action="">
<div class="box-body">
<div class="form-group">
<label>User name</label>
<input type="text" class="form-control" placeholder="Enter username" name="uname" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" name="email" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Temporary password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Enter password" name="pass" required>
</div>
<div class="form-group">
<label>Group admin</label>
<select class="form-control" name="groups">
<option value="Main Admin">Main Admin</option>
<option value="Administrator">Administrator</option>
<option value="Content Creator">Content Creator</option>
<option value="Social Media Manager">Social Media Manager</option>
<option value="Analyst">Analyst</option>
</select>
</div>
</div>
<div class="box-footer">
Visit admin types documentation to know the differences between each admin.
</div>
<div class="box-footer">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
And here is the class that I have used which is called Register.class.php:
<?php
class Register
{
private $db;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function CheckUname($username,$email,$password,$groups,$level)
{
if(!empty($username)&&($email))
{
$chk1 = $this->db->prepare("SELECT username FROM admins WHERE user_name= ?");
$chk1->bindParam(1,$username);
$chk1->execute();
if($chk1->rowCount() == 1)
{
$notice['username_exists'] = "Try different username";
return $notice;
}else{
$chk2 = $this->db->prepare("SELECT email FROM admins WHERE email_address= ?");
$chk2->bindParam(1,$email);
$chk2->execute();
if($chk2->rowCount() == 1)
{
$notice['email_exists'] = "The email address that you have entered is already exists in database";
return $notice;
}else{
$this->NewAdmin($username,$email,$password,$groups,$level);
$notice['success_message'] = "New admin was successfully added";
return $notice;
}
}
}
}
public function NewAdmin($username,$email,$password,$groups,$level)
{
if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
{
$reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash, group_admin, date_joined, admin_level) VALUES ( ?, ?, ?, ?, NOW(), ?)");
$reg->bindParam(1,$username);
$reg->bindParam(2,$email);
$reg->bindParam(3,$password);
$reg->bindParam(4,$groups);
$reg->bindParam(5,$level);
$reg->execute();
}
}
}
?>
Run this commend on mysql terminal:
ALTER TABLE tablename MODIFY COLUMN id INT auto_increment PRIMARY KEY
the above command will make the id an auto_increment as well as primary key. So for every value you insert, auto_increment will generate a new value for column id, you don't have to assign some value in it.

I need to create a function which will check for old password and update the current one

I have already created a login and registration system in my website. I have a table in my database where all the user information is stored called 'users'.I searched a lot but could not get much far.
My class
public function change_password()
{
$this->load->view('templates/header');
$this->load->view('registration/changepassword');
$this->load->view('templates/footer');
}
public function change()
{
$this->form_validation->set_rules('newpassword', 'New Password', 'required|matches[rpassword]');
$this->form_validation->set_rules('rpassword', 'Retype Password', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('registration/change_password');
}else{
$query = $this->Login_Database->checkOldPass(sha1($this->input->post('oldpassword')));
if($query){
$query = $this->Login_Database->saveNewPass(sha1($this->input->post('newpassword')));
if($query){
redirect('registration/change_password');
}else{
redirect('registration/change_password');
}
}
redirect('');
}
}
My model
// Insert registration data in database
public function checkOldPass($old_password)
{
$this->db->select('*');
$this->db->from('users');
$this->db->select('id');
$this->db->where('id', 'id');
$this->db->where('password', $this->input->post('oldpassword'));
$query = $this->db->get();
if ($query->num_rows > 0) {
return true;
} else {
$this->form_validation->set_message('checkOldPassword', 'wrong old password.');
return false;
}
}
public function saveNewPass($new_pass)
{
$data = array(
'password' => $new_pass
);
$this->db->where('id', 'id');
$this->db->update('users', $data);
return true;
}
}
My form
<h1><p class="text-center" style="font-family: 'Passion One',
cursive;">Change Password</p></h1>
<div>
<form class="form-horizontal" method="post"
action="<?php echo base_url() ?>Registration/change">
<fieldset>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="phone">Old
Password</label>
<div class="col-md-4">
<input id="oldpassword" name="oldpassword" type="password"
placeholder="Old Password"
class="form-control input-md"
required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="phone">New
Password</label>
<div class="col-md-4">
<input id="newpassword" name="newpassword" type="password"
placeholder="New Password"
class="form-control input-md"
required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="phone">Conform
Password</label>
<div class="col-md-4">
<input id="rpassword" name="rpassword" type="password"
placeholder="Retype Password"
class="form-control input-md"
required="">
</div>
</div>
<br>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="save"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-success">Save</button>
<a id="cancel" name="cancel" class="btn btn-danger" href="<?
php echo base_url(); ?>">
Cancel</a><br><br>
</div>
<br><br><br>
</fieldset>
</form>
</div>
My class is inside a class called Registration.Any help would be much appreciated. Also the name of my model is 'Login_Database.php' and the name of my form is 'changepassword.php'
Please use below code in controller and your model. if you have any problem please reply me.
// controller function
public function change(){
if ($this->form_validation->run('update_password') == True)
{
$old_password = sha1($_POST['oldpassword']);
unset($_POST['oldpassword']);
$post = $this->input->post();
$status = $this->model_name->check_old_password($old_password, $user_id); //check old password
if($status==true){
if($_POST['newpassword']==$_POST['rpassword']){
if($this->model_name->update_password($post, $user_id)){
$this->session->set_flashdata('message_success', "Password update successully.");
redirect('registration/change_password');
}else{
$this->session->set_flashdata('article_failed', "Password not update successully.");
redirect('registration/change_password');
}
}else{
$this->session->set_flashdata('article_failed', "Sorry your new password and confirm password does not matched.");
redirect('registration/change_password');
}
}
}else{
$this->session->set_flashdata('article_failed', "Sorry your old password is wrong.");
redirect('registration/change_password');
}
}
// model function
public function check_old_password($old_password, $user_id){
$query = $this->db->select('id')
->from('users')
->where(['id'=>$user_id, 'password'=>$old_password])
->get()
->row();
if($query>0){
return TURE;
}else{
return FALSE;
}
}
public function update_password($post, $user_id){
$password = sh1($post['newpassword']);
return $this->db->where('id', $user_id)
->update('users', ['password'=>$password]);
}

insert into form doesn't insert into table ? with codeigniter framework

database details:
dbtable : guests
dbcolumns : id (A_I), guestname, guestemail
when i submit a data it's only refresh the page and take no action i'm beginner please tell me where's the problem and why
i made insert form by basic php and i passed but with codeigniter maybe i'm not fully understood yet looks like my experience about 1% or 5% :D :D
Controller/Guests.php :
<?php
class Guests extends CI_Controller {
public function index($page = 'guests')
{
if ( ! file_exists(APPPATH.'/views/main/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Guests List';
$this->load->view('include/header', $data);
$this->load->model('guests_model');
$data['records'] = $this->guests_model->getAll();
$this->load->view('main/guests', $data);
$this->load->view('include/footer', $data);
}
public function addnewguest($page = 'Add New Guest')
{
if ( ! file_exists(APPPATH.'/views/main/addnewguest.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Please Fill Informations Below';
$this->load->view('include/header', $data); // loaded the header
$this->load->model('guests_model'); // loaded the model
$this->load->view('main/addnewguest', $data); // loaded add new form
if($this->input->post('createnew')) { // if click add send information to addnewguestpost() function in the guest_model file
$this->guests_model->addnewguestfunc();
}
$this->load->view('include/footer', $data); // loaded the footer
}
}
views/main/addnewguest.php
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" name="createnew">Add</button>
</div>
</div>
</form>
models/guests_model.php
<?php
class Guests_model extends CI_Model {
public function getAll() {
$query = $this->db->get('guests');
if($query->num_rows() > 0){
return $query->result();
} else {
return FALSE;
}
}
public function addnewguestfunc() {
$data = array(
'guestname' => $this->input->post('guestname'),
'guestemail' => $this->input->post('guestemail')
);
$this->db->insert('guests', $data);
}
}
Solved !!
forgot change button to input because i copied it from bootstrap basic templates for practicing ..
views/main/addnewguest.php after change button to input :
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" name="createnew">Add</input>
</div>
</div>
</form>

Categories