Insert data failed using codeigniter and mysql - php

i have a little bit error when doing a simple registration form. My problem is the data that have input is not inserted in database.
But before i using horizontal form the data can be inserted in my database. I'm using MySQL. this is my controller :
function create(){
$data = array(
'nik' => $this->input->post('nik'),
'nama' => $this->input->post('nama'),
'tgl_lahir' =>$this->input->post('tgl_lahir'),
'no_hp' => $this->input->post('no_hp'),
'alamat' => $this->input->post('alamat')
);
$this->db->insert('profile_mitra',$data);
redirect('admin/daftar_mitra');
}
and this my view called tambah_mitra.php.
View (tambah_mitra.php)
<?php $this->load->view('templates/head');?>
<?php $this->load->view('templates/sidebar');?>
<body>
<div class="content-wrapper">
<section class="content-header">
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-6">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Tambah Mitra</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form">
<div class="box-body">
<div class="form-group">
<?php echo validation_errors();?>
<?php echo form_open('admin/daftar_mitra');?>
<label for="exampleInputEmail1">NIK</label>
<input type="text" class="form-control" id="nik" placeholder="NIK">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Nama</label>
<input type="text" class="form-control" id="nama" placeholder="Nama">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Tanggal Lahir</label>
<input type="date" class="form-control" id="tgl_lahir" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">No.HP</label>
<input type="text" class="form-control" id="no_hp" placeholder="Nomor Handphone">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Alamat</label>
<input type="alamat" class="form-control" id="alamat" placeholder="Alamat Lengkap">
</div>
<!-- <div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div> -->
<!-- <div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div> -->
</div>
<!-- /.box-body -->
<div class="box-footer">
<input class="btn btn-success" type="submit" value="Simpan">
</div>
</form>
</div>
</div>
</div>
</section>
</div>
</body>
<?php $this->load->view('templates/footer');?>

it is not inserting because you did not define name of input filed.i think this is the reason of not inserting data. please try.
<div class="form-group">
<label for="exampleInputEmail1">Nama</label>
<input type="text" class="form-control" id="nama" name="nama" placeholder="Nama">
</div>

try closing your form
<?php echo form_close();?>
Also to see the errors use
<?php echo form_error('nik');
and continue filling the (' ') in with your variables
to see what the error is.

Related

HTML form action doesn't redirect

I am very confused that my html form action doesn't redirect me to another page, because this has always worked.
Here's my code
<section class="contact section-padding" data-scroll-index="6">
<div class="container">
<div class="row">
<div class="section-head text-center col-sm-12">
<h4>Neem contact op</h4>
<h6>Vul formulier in</h6>
</div>
<div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10">
<form method="post" class="form" id="contact-form" action="verstuurd.php">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_name" type="text" name="volledige_naam" placeholder="Volledige naam">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_email" type="email" name="email" placeholder="E-mailadres" >
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="text" name="subject" placeholder="Onderwerp">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" placeholder="Bericht" rows="4" ></textarea>
</div>
</div>
<div class="col-md-12 text-center">
<button type="submit"><span>Verstuur</span></button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
What is wrong with this? I thought it might has to do something with the div classes, but is this possible?
Edit
Verstuurd.php
<section class="contact section-padding" data-scroll-index="6">
<div class="container">
<div class="row">
<div class="section-head text-center col-sm-12">
<h4>Succesvol verstuurd</h4>
<h6>We nemen zo snel mogelijk contact met u op</h6>
</div>
</div>
</div>
</section>
At the moment I do not have any php code in verstuurd.php. I thought that aciton only redirects to PHP files and not HTML files.
I'm guessing there's some javascript that is blocking the form submission. Look out for anything similar to e.preventDefault(); in relation to form submission.
Change your button from
<button type="submit"><span>Verstuur</span></button>
to
<input id="submit" name="submit" type="submit" value="Verstuur">

Data is not insert into database, there is no error shown

I have problem in insert data in database; there is no error shown, but data is not inserted into the database.
Controller Code
function FormData($data){
if (!empty($_POST['name'])) {
$data = array(
'name' => $this->input->post('name'),
'position' => $this->input->post('position'),
'about_me' => $this->input->post('about_me'),
'contact_me' => $this->input->post('contact_me'),
'email' => $this->input->post('email'),
'temp_addr' => $this->input->post('temp_addr'),
'perm_addr' => $this->input->post('perm_addr'),
'skill' => $this->input->post('skill'),
'photo' => $this->input->post('photo'),
);
$this->Maboutus->form_insert($data);
redirect('/admin');
}
}
Model Code
function form_insert($data){
$this->db->insert('aboutus',$data);
}
View file
<div class="container">
<div class="col-md-8">
<form action="#" method="POST" class="form-horizontal">
<div class="col-md-12">
<div class="form-group">
<lebel for="name" class="col-md-4" >Name :</lebel>
<input type="text" name="name" id="name" class="col-md-8" placeholder="your name " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Position :</lebel>
<input type="text" name="position" id="position" class="col-md-8" placeholder="your position " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >About Me :</lebel>
<input type="text" name="about_me" id="about_me" class="col-md-8" placeholder=" your name About me " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Mobile No :</lebel>
<input type="text" name="contact_me" id="contact_me" class="col-md-8" placeholder=" your Mobile number" required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Email :</lebel>
<input type="text" name="email" id="email" class="col-md-8" placeholder=" your email " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Temporary Addr :</lebel>
<input type="text" name="temp_addr" id="temp_addr" class="col-md-8" placeholder=" your Temporary Address " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Permanent Addr :</lebel>
<input type="text" name="perm_addr" id="perm_addr" class="col-md-8" placeholder=" your Permanent Address " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<lebel for="position" class="col-md-4" >Skill :</lebel>
<input type="text" name="skill" id="skill" class="col-md-8" class="col-md-8" placeholder="your skill " required>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<div class="form-group">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" class="col-md-8" name="photo" id="photo">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input class="col-md-8" class="btn btn-primary" type="submit" value="Submit"></br></br>
</div>
</div>
</form>
</div>
I am new in PHP. Data is not insert in database, and it shows no error; what can I do? I tried to search but I didn't find anything wrong.
You are not posting to a controller/method so the post goes nowhere. In the view change the line
<form action="#" method="POST" class="form-horizontal">
to this
<form action="<?php echo base_url('controller_name/FormData'); ?>" method="POST" class="form-horizontal">
In the controller change the definition of function FormData to this
function FormData(){ ...
You do not need to pass an argument so don't require the method to receive one.
This next bit is not relevant to your problem but a refinement to consider. You don't need to build a new array to send to the model. Because $_POST is going to be populated with your fields and your table column names are exactly the same as the form's field names you can simply send $_POST to the model.
$this->Maboutus->form_insert($_POST);
If there was data posted that you didn't want to send to the model you would unset those indexes before doing the insert. It should probably be done in the model so you don't mess with the actual $_POST array. For example:
function form_insert($data)
{
unset($data['not_to_be_inserted_key']); //remove this item from array
$this->db->insert('aboutus', $data);
}
But that is not needed in the example you give show.
I will assume you are successfully receiving the $_POST on your controller, then the problem I can see is the model, first of all you need to initialize it if you havent, you can do it with the following in your __construct
public function __construct() {
parent::__construct();
$this->load->model('Maboutus_model');
}
Please be aware you ALWAYS need to add the "_model" after the model name
and when calling the function you do it with
$this->Maboutus_model->form_insert($data);

How to save data and print form at one click in php?

I am working on invoice where I have to save form's data in database and at the same time it should be print. So, I have 3 pages: invoice.php, invoice_print.php and insert_data.php
invoice_print.php is a html form that should be printed.
Now, user will go to first invoice.php then he will fill details and either he will click on submit button to save data in db or else he will click on print button to print that invoice.
Now, lets come to the second part: If user will select print button then data will go to database first and then it will go to invoice_print.php with same data that he filled.
How to do this? What logic should I use to save data and capture that data's id in button and then send that id on another page to display?
invoice.php:
<form id="demo-form2" action="insert_data.php" method="post" data-parsley-validate class="form-horizontal form-label-left">
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
</form>
invoice_print.php:
<div class="col-md-12">
<div class="col-md-6">
<h2>DIGILIFE BIZCARE SOLUTIONS</h2>
<p>414, Vashi Infotech Park,Maharashtra</p>
</div>
<div class="col-md-6">
<h2>BILL OF SUPPLY</h2>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>GSTIN</label>
<input type="text" name="gstin" value="">
<label>Serial No & Date of Invoice</label>
<input type="text" name="serialNo">
</div>
<div class="col-md-6">
</div>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<div class="col-md-6">
<label>Mode of Transport</label><br>
<label>Vehicle No</label><br>
<label>Date & Time of Supply</label><br>
<label>Place Of Supply</label>
</div>
<div class="col-md-6s">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
</div>
</div>
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Location</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="designation">
</div>
</div>
<div class="col-md-12 form-group">
<div class="col-md-6">
<label class="control-label">Address</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" value="" name="contact">
</div>
</div>
I would put all of the code for a form into one file, structured thus:
<?php
$formDone=!empty($_POST['formDone']);
$printReq=!empty($_POST['printReq']);
if ($formDone)
{
// perform validation
}
if ($formDone && $validationPassed)
{
// write to database using $_POST data
}
if ($formDone && $databaseWriteSuccess && $printReq)
{
// print using $_POST data
}
// end of PHP
?>
<form method="post" action="<?php echo $PHP_SELF ?>">
<fieldset>
<input type="hidden" name="formDone" value="1" />
</fieldset>
<!-- field forms -->
<input type="submit" value="Save to db" />
<input type="submit" name="printReq" value="Print Invoice" />
</form>

Codeigniter insert not working Database error occured

I have following code tried out.
class Vendor extends CI_Controller{
function __construct(){
parent::__construct();
}
public function vendor_add(){
$this->load->helper('url');
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('add_vendor');
$this->load->view('footer');
}
public function save(){
$this->load->helper(array('form','url'));
$this->load->model('Save_vendor');
$vendor_data = array(
'ven_name'=> $this->input->post['firstName'],
'ven_shop'=>$this->input->post['companyName'],
'ven_email_id'=>$this->input->post['email'],
'ven_contactno'=>$this->input->post['contactno'],
'ven_othercontactno'=>$this->input->post['contactno2'],
'ven_commission'=>$this->input->post['assigncommission'],
'ven_accname'=>$this->input->post['bankaccname'],
'ven_accountno'=>$this->input->post['bankaccno'],
'ven_ifsccode'=>$this->input->post['ifsccode'],
'ven_type'=>$this->input->post['optionsRadios']
);
$this->Save_vendor->vendor_insertdata($vendor_data);
redirect(base_url().'/vendor/vendor_add');
}
}
/* Model File */
class Save_vendor extends CI_Model{
public function vendor_insertdata($vendor_data){
$this->db->insert('tbl_vendor',$vendor_data);
}
/* VIew FIle */
<!-- BEGIN CONTENT -->
<div class="page-content-wrapper">
<div class="page-content">
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<!-- BEGIN PAGE HEADER-->
<h3 class="page-title">
Add Vendor <small> Add New Vendor</small>
</h3>
<div class="row">
<div class="col-md-12">
<div class="portlet-body form">
<?php echo base_url();?>
<!-- BEGIN FORM-->
<form action="<?php echo base_url("index.php/Vendor/save") ?>" class="horizontal-form" method="post">
<div class="form-body">
<h3 class="form-section">Add Vendor</h3>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="radio-list">
<label class="radio-inline">
<input type="radio" name="optionsRadios" id="1" value="1" checked> Shop Vendor </label>
<label class="radio-inline">
<input type="radio" name="optionsRadios" id="2" value="2"> Vendor </label>
</div>
</div>
</div>
<!--/span-->
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Vendor Name<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="firstName">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Company/Shop/Business Name<span class="required">*</span></label>
<input type="text" id="companyName" class="form-control" name="companyName" >
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Email Id<span class="required">*</span></label>
<input type="text" id="email" class="form-control" name="email">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Contact No<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="contactno" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">2nd Contact No</label>
<input type="text" id="firstName" class="form-control" name="contactno2">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">PIN Code<span class="required">*</span></label>
<input type="text" id="pincode" class="form-control" name="pincode" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Assign Commision %<span class="required">*</span></label>
<input type="text" id="assigncommission" class="form-control" name="assigncommission">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">BANK Account Name<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="bankaccname" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Account Number<span class="required">*</span></label>
<input type="text" id="bankaccno" class="form-control" name="bankaccno" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">IFSC Code<span class="required">*</span></label>
<input type="text" id="ifsccode" class="form-control" name="ifsccode">
</div>
</div>
</div>
<div class="form-actions right">
<button type="submit" class="btn yellow-crusta"> Add Vendor</button>
</div>
</form>
</div>
</div>
</div>
<!-- END CONTENT -->
</div>
<!-- END CONTAINER -->
Above code but it will give an error Database error occured. Column name not be null. I am beginner in codeigniter please any suggestions for it.
I want to add data in my database.but it is not working.
Check your table "tbl_vendor" in which you are inserting values. One or more field is defined as NOT NULL means it will not accept empty value. You have to pass value to that field which is marked as NOT NULL.
Please read Codeigniter Query Builder reference first. You're making big mistakes. Your code should be like below:
$vendor_data = array(
'ven_name'=> $this->input->post['firstName'],
'ven_shop'=>$this->input->post['companyName'],
'ven_email_id'=>$this->input->post['email'],
'ven_contactno'=>$this->input->post['contactno'],
'ven_othercontactno'=>$this->input->post['contactno2'],
'ven_commission'=>$this->input->post['assigncommission'],
'ven_accname'=>$this->input->post['bankaccname'],
'ven_accountno'=>$this->input->post['bankaccno'],
'ven_ifsccode'=>$this->input->post['ifsccode'],
'ven_type'=>$this->input->post['optionsRadios']
);
$this->db->insert('your_table_name', $vendor_data);

how will i passed the json data into php

I have four textboxes and when you click on the add one button it will add up another set of textboxes. My problem is how can i pass the json data to the action form. Here is my code:
$("#test").click(function(){
var array = $('.experience').map(function() {
var obj = {};
$(this).next().addBack().find('input:text').each(function() {
obj[this.id] = this.value;
});
return obj;
}).get();
$('#json').text(JSON.stringify(array, null, 2));
});
and my form action
<form action="<?php echo base_url().'resume/update'?>" method="post" id="send_form">
<!-- Experience Start -->
<div class="row">
<div class="col-sm-12">
<p> </p>
<h2>Experience</h2>
</div>
</div>
<div class="row experience">
<div class="col-sm-6">
<div class="form-group">
<label for="resume-employer">Employer</label>
<input type="text" class="form-control" name="resumeEmployer" id="resume-employer" value="<?php echo set_value('resumeEmployer'); ?>" id="resume-employer" placeholder="Company name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="resume-experience-dates">Start/End Date</label>
<input type="text" class="form-control" name="resumeExperienceDates" name="<?php echo set_value('resumeExperienceDates'); ?>" id="resume-experience-dates" placeholder="e.g. April 2010 - June 2013">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="resume-job-title">Job Title</label>
<input type="text" class="form-control" name="resumeJobTitle" id="resume-job-title" value="<?php echo set_value('resumeJobTitle'); ?>" placeholder="e.g. Web Designer">
</div>
</div>
<div class="col-sm-6">
<div class="form-group" id="resume-responsibilities-group">
<label for="resume-responsibilities">Responsibilities (Optional)</label>
<input type="text" class="form-control" name="resumeResponsibilities" id="resume-responsibilities" value="<?php echo set_value('resumeResponsibilities');?>" placeholder="e.g. Developing new websites">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<hr class="dashed">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p><a id="add-experience">+ Add Experience</a></p>
<hr>
</div>
</div>
<!-- Experience Start -->
<div class="row text-center">
<div class="col-sm-12">
<p> </p>
<input type="submit" id="test" class="btn btn-primary btn-lg" value="Submit" />
</div>
</div>
</form>
i added a id="test" in the input type="submit button
any help is muchly appreciated. TIA
Use json_encode and json_decode function example in ref link,
json_encode($arr);
var_dump(json_decode($json));
http://php.net/manual/en/function.json-encode.php
var_dump(json_decode($json, true));

Categories