I am trying to insert the data to the database by php CodeIgniter and display the result in the same page during submit the button. Below is my code.The problem is i got an error message as Duplicate entry '104' for key 'PRIMARY'. Please help. View Page
<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered']; ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6" rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4"> <span>SUBMIT</span>
</button>
</div>
</div>
</form>
Controller page
public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data);
if($this->form_validation->run() == FALSE)
{
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';
}
else
{
$data['ins_msg']='data not inserted';
}
}
}
Model Page
public function add_offer()
{
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}
Related
I have a page for adding scores of students in a school. After submitting scores of the students, the input boxes are repopulated with the results from the database.
This was done so that
Teachers who are unable to complete adding scores for students can come back and continue from where they left off.
If an edit is to be made, teachers will know where and what to be edited.
And generally for revision purposes.
However, I noticed that when an edit is made for a particular student, that subject(s) is duplicated for everyone in the class even if they have their scores already.
Please see image below to get an idea of what I mean.
MY MODEL:
public function addrn($data3) {
$this->db->insert_batch('mtscores_rn', $data3);
return $this->db->affected_rows();
}
MY CONTROLLER:
function assigngradeActionRNMT()
{
//var_dump(count($this->input->post('number')));
for($i=0; $i<count($this->input->post('number')); $i++)
{
$data3[]=array(
'section_id' => $this->input->post('section_id'),
'subject_id' => $this->input->post('subject_id'),
'class_id' => $this->input->post('class_id')[$i],
'student_id' => $this->input->post('student_id')[$i],
'session_id' => $this->input->post('session_id'),
'mt_ca1' => $this->input->post('mt_ca1')[$i],
'mt_ca2' => $this->input->post('mt_ca2')[$i],
'mt_ca3' => $this->input->post('mt_ca3')[$i],
);
}
//var_dump($data3);
$inserted = $this->mtprimary_model->addrn($data3);
if($inserted > 0)
{
$this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
//Echo back success json
redirect('admin/mtprimary/index');
}
}
MY VIEW:
<?php foreach($students as $student){ ?>
<div class="row">
<div class="col-lg-3">
<div class="form-group">
<label>Student Name</label>
<input type="hidden" name="number[]" value="">
<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
<input type="hidden" name="session_id" value="<?php echo $student->session_id; ?>">
<input type="hidden" name="student_id[]" value="<?php echo $student->student_id; ?>">
<input type="hidden" name="class_id[]" value="<?php echo $class_id; ?>">
<input type="text" value="<?php echo $CI->GetStudentNameWithID($student->student_id); ?>" class="form-control " readonly>
</div>
</div>
<div class="col-lg-3">
<label>Class Expectation </label>
<textarea name="mt_ca1[]" class="form-control" rows="3" ><?php echo $student->mtscores? $student->mtscores->mt_ca1: 0; ?></textarea>
</div>
<div class="col-lg-3">
<label>Milestone Achieved</label>
<textarea name="mt_ca2[]" class="form-control" rows="3" ><?php echo $student->mtscores? $student->mtscores->mt_ca2: 0; ?></textarea>
</div>
<div class="col-lg-2">
<div class="form-group">
<label>Remark</label>
<input type="text" name="mt_ca3[]" class="form-control" value="<?php echo $student->mtscores? $student->mtscores->mt_ca3: 0; ?>" >
</div>
</div>
</div>
Use this Updated Code in Your Model
public function addrn($data3)
{
$statusIns=$this->checkAlreadyExistOrNor();
if($statusIns==0){
$this->db->insert_batch('mtscores_rn', $data3);
}else{
$this->db->where($data3);
$this->db->update('mtscores_rn', $data3);
}
return $this->db->affected_rows();
}
public function checkAlreadyExistOrNor($data3){
$this->db->select(*);
$this->db->where($data3);
$result=$this->db->get();
$listData=$result->result_array();
if(!empty($listData)){
return 1;
}else{
return 0;
}
}
Form validation and record insert not working in codeigniter
model
class Mymodel extends CI_Model
{
function insert($data)
{
$this->db->insert("users", $data);
}
}
controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_controller
{
function index()
{
$this->load->view('myfolder/my_page');
}
function login()
{
$this->load->view('myfolder/login');
}
function signup()
{
$this->load->view('myfolder/signup');
}
function signupmethod()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("firstname","First Name",'requird|alpha');
$this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
$this->form_validation->set_rules("email","Email",'requird|alpha');
$this->form_validation->set_rules("password","Password",'requird|alpha');
$this->form_validation->set_rules("mobile","Mobile",'requird|alpha');
if ($this->form_validation->run())
{
$this->load->model("mymodel");
$data = array(
"firstname" => $this->input->post("firstname"),
"lastname" => $this->input->post("lastname"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"mobile" => $this->input->post("mobile"),
"curtime" => "NOW()"
);
if($this->input->post("Signup"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
}
}
public function Inserted()
{
$this->index();
}
}
?>
view(html code)
<?php include('header.php'); ?>
<form method="post" action="<?php echo base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h3>Login</h3>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>First Name</label>
<input type="text" name="firstname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("firstname"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("lastname"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Email</label>
<input type="text" name="email" value="" class="form-control">
<span class="text-danger"><?php echo form_error("email"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Password</label>
<input type="password" name="password" value="" class="form-control">
<span class="text-danger"><?php echo form_error("password"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" name="mobile" value="" class="form-control">
<span class="text-danger"><?php echo form_error("mobile"); ?></span>
</div>
<div class="col-md-6 form-group" style="margin-top: 23px;">
<input type="submit" name="submit" value="Signup" class="btn btn-info">
</div>
</div>
I have read every line carefully, code run but no errors showing and validation and record insert are not working. and no errors showing.
please help.
Change controller this block of code when you fetch form elements in controller always use the element name
if($this->input->post("submit"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
Correct spelling of required
$this->form_validation->set_rules("firstname","First Name",'required|alpha');
Your set_rules statements are incorrect. Use required instead of requird otherwise set_rules will fail.
When you are not sure why form_validation is failing, try getting the errors with:
If($this->form_validation->run() == false)
{
echo validation_error();
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model{
function __construct(){
$this->userTbl='login';
}
public function insert($data=array()){
if(!array_key_exists("created", $data)){
$data['created'] = date("Y-m-d H:i:s");
}
if(!array_key_exists("modified", $data)){
$data['modified'] = date("Y-m-d H:i:s");
}
$insert = $this->db->insert($this->userTbl, $data);
if($insert){
return $this->db->insert_id();;
}else{
return false;
}
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user');
}
/*
* User registration
*/
public function registration(){
$data=array();
$userData=array();
if($this->input->post(regisSubmit)){
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
}
$userData=array(
'name'=>strip_tags($this->input->post('name')),
'email'=>strip_tags($this->input->post('email')),
'password'=>strip_tags($this->input->post('password')),
'gender'=>strip_tags($this->input->post('gender')),
'phone'=>strip_tags($this->input->post('phone'))
);
if($this->form_validation->run()==true){
$insert=$this->user->insert($userData);
if($insert){
$this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.');
redirect(users/login);
}
else{
$data['error_msg']='Try again';
}
}
}
$data['user'] = $userData;
//load the view
$this->load->view('users/registration', $data);
}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
<h2>User Registration</h2>
<form action="" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required="" value="<?php echo !empty($user['email'])?$user['email']:''; ?>">
<?php echo form_error('email','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" required="">
<?php echo form_error('password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="password" class="form-control" name="conf_password" placeholder="Confirm password" required="">
<?php echo form_error('conf_password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<?php
if(!empty($user['gender']) && $user['gender'] == 'Female'){
$fcheck = 'checked="checked"';
$mcheck = '';
}else{
$mcheck = 'checked="checked"';
$fcheck = '';
}
?>
<div class="radio">
<label>
<input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>>
Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>>
Female
</label>
</div>
</div>
<div class="form-group">
<input type="submit" name="regisSubmit" class="btn-primary" value="Submit"/>
</div>
</form>
<p class="footInfo">Already have an account? Login here</p>
</div>
</body>
</html>
Here in this form I need to insert data into mysql database using codeigniter. but the data is not inserting into the database and also not showing any error. Below is the code for modal, controller and view page. In database.php file, eerything is fine. Here in this, How to debug the code, and what is the error. Thanks in advance.
Define url in routes.php
$route['user/registration'] = 'Users/registration';
also define your project base_url in config/config.php file
use
form action="user/registration"
method="post">
instead of
form action="" method="post">
and view file in
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
change to:
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo isset($user['name']) ? $user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
You never post anything to your controller. look at this line of your view file.
<form action="" method="post">
Change to :
<form action="<?php echo base_URL(); ?>Users/registration" method="post">
Outputted a row from a table ,am now trying to add some of the contents to another table using a form which is inside the outputted row but its not inserted to the database. am not getting an error.
Here is my view
<?php
foreach ($h->result() as $row)
{?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
My model
function bid($data){
$query=$this->db->update('bids',$data);
return $query;
}
My controller
public function bid(){
$this->load->database();
$this->load->model('Truckeraccount_model');
$data['a']=$this->Truckeraccount_model->accepted_bid();
$data['b']=$this->Truckeraccount_model->picked_loads();
$data['h']=$this->Truckeraccount_model->loads();
$data['g']=$this->Truckeraccount_model->notification();
$data['i']=$this->Truckeraccount_model->return_loads();
$data['accepted_return_loads']=$this->Truckeraccount_model->accepted_return_loads();
$data['bid_amount']=$this->Truckeraccount_model->bid_amount(); $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('bid_amount', 'bid_amount', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('truckeraccount_view',$data);
$this->load->view('footer');
} else {
$data = array(
'truckerid' => $this->input->post('truckerid'),
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
);
$this->Truckeraccount_model->bid($data);
$data['message'] = '';
redirect('truckeraccount_ctrl/');
}
}
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</form>
I have simple code thats not working. I made validation for checkbox and I get error:
Unable to access an error message corresponding to your field name accept_terms_checkbox.(accept_terms)
This is my code:
Controller:
public function formularz2()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//VALIDATION!!
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');
$this->form_validation->set_rules('accept_terms_checkbox', 'checkbox', 'callback_accept_terms');
//CALLBACK FUNCTION!!
function accept_terms()
{
if ($this->input->post('accept_terms_checkbox'))
{
return TRUE;
}
else
{
$error = 'Please read and accept our terms and conditions.';
$this->form_validation->set_message('accept_terms', $error);
return FALSE;
}
}
//Po przesłaniu danych
if (!empty($_POST))
{
$konsultant = $this->uri->segment(3);
$dane = array(
'email1' => $this->input->post('email'),
'imie' => $this->input->post('imie'),
'nazwisko' => $this->input->post('nazwisko'),
'nazwa_firmy' => $this->input->post('firma'),
'konsultant_id' => $konsultant,
);
//Trzeba jeszcze tutaj ogarnąć wrzucanie pivotów zaznaczonych checkboxów
$boxes = $_POST['formChecks'];
$N = count($boxes);
$ostatni_id = $this->Osoby_m->ostatni_id();
for($i=0; $i < $N; $i++)
{
$this->Osoby_m->nowy_wpis_formularz($boxes[$i]);
}
//Pobrać id dodanej osoby = policzyć ile osób w bazie +1
$this->Osoby_m->nowa_osoba($dane);
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
My View:
<?php echo form_open(); ?>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>">
<p class="help-block"><?php echo form_error('email'); ?></p>
</div>
</div>
<div class="form-group">
<label for="imie" class="col-sm-2 control-label">Imię:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="imie" name="imie" placeholder="Imię" value="<?php echo set_value('imie'); ?>">
<p class="help-block"><?php echo form_error('imie'); ?></p>
</div>
</div>
<div class="form-group">
<label for="nazwisko" class="col-sm-2 control-label">Nazwisko: </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nazwisko" name="nazwisko" placeholder="Nazwisko" value="<?php echo set_value('nazwisko'); ?>">
<p class="help-block"><?php echo form_error('nazwisko'); ?></p>
</div>
</div>
<div class="form-group">
<label for="firma" class="col-sm-2 control-label">Firma:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firma" name="firma" placeholder="Firma" value="<?php echo set_value('firma'); ?>">
<p class="help-block"><?php echo form_error('firma'); ?></p>
</div>
</div>
<div class="col-sm-12" style="margin-bottom: 30px; margin-top: 30px;"> <h3>Wybierz interesujące Cię szkolenia</h3></div>
<div class="form-group">
<label class="col-sm-2 control-label">Szkolenia:</label>
<div class="col-sm-10">
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo $szkolenie->id; ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
</div>
</div>
//CALLBACK CHECKBOX
<div class="form-group">
<div class="col-sm-12" style="margin-top: 50px;">
<input type="checkbox" name="accept_terms_checkbox" value="1"/> Zgadzam się na otrzymywanie maili od firmy Gamma<br>
<p><?php echo form_error('accept_terms_checkbox') ?></p>
</div>
</div>
<button type="submit" class="btn btn-lg btn-success" style="margin-top: 50px;">Odbierz Voucher</button>
<?php echo form_close(); ?>
In order to use your own callback method with codeigniter's validation class, you must declare the method. IN your case ( add this to your controller or a helper etc...):
public function accept_terms($user_input){
if($user_input != ""){
return True;
}else{
$this->form_validation->set_message('accept_terms_checkbo', 'The %s field can not be empty"');
return False;
}
I fixed this error by simple validation without callback function. I figure out that I don't need callback function for Terms of Services checkbox.
$this->form_validation->set_rules('accept_terms_checkbox', 'Checkbox', 'required');
$this->form_validation->set_message('required', 'You need to accept Terms of Services');