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);
Related
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.
im trying to create a templated system with CI. So far everything is working flawlessly and im testing out form validation but its not working at all. As you can see from the code below there are both html5 required statements as well as the codeigniter rules. Neither of which are working after I hit the submit button. So i'm not sure whats going wrong. Something stupid im sure.
public function show_form(){
$form_data = '<div class="row">
<div class="col-md-12">
<div class="card-box">
<h4 class="m-t-0 header-title"><b>Basic Form Wizard</b></h4>
<form id="basic-form" action="#">
<div>
<h3>Account</h3>
<section>
<div class="form-group clearfix">
<label class="col-lg-2 control-label " for="username">User name *</label>
<div class="col-lg-10">
<input class="form-control" id="username" name="username" type="text">
</div>
</div>
<div class="form-group clearfix">
<label class="col-lg-2 control-label " for="password"> Password *</label>
<div class="col-lg-10">
<input id="password" name="password" type="text" class="required form-control">
</div>
</div>
<div class="form-group clearfix">
<label class="col-lg-2 control-label " for="confirm">Confirm Password *</label>
<div class="col-lg-10">
<input id="confirm" name="confirm" type="text" class="required form-control">
</div>
</div>
</section>
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
</div>';
$this->CI->form_validation->set_rules('username', 'Username', 'required');
if ( $this->CI->form_validation->run() == FALSE ):
echo "<pre>Before Validation</pre>";
echo validation_errors();
echo "<pre>After Validation</pre>";
echo $form_data;
else:
echo "We did it";
endif;
}
Thanks in advance for any guidance.
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>
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);
When i post a Message from my web page it returns a ? in the browser such as this,
I believe both the HTML and the php to be correct
The html......
`enter code here`
<form class="form-horizontal">
<fieldset>
<div class="form-group is-empty">
<label for="Name" class="col-md-2 control- label">Name</label>
<div class="col-md-10">
<input type="text" class="form-control" id="Name" placeholder="Name">
</div>
</div>
<div class="form-group is-empty">
<label for="Email" class="col-md-2 control-label">Email</label>
<div class="col-md-10">
<input type="email" class="form-control" id="Email" placeholder="Email">
</div>
</div>
<div class="form-group is-empty">
<label for="Message" class="col-md-2 control-label">Message</label>
<div class="col-md-10">
<input type="text" class="form-control" id="Message" placeholder="Message">
</div>
</div>
<div class="form-group">
<form action="sendcontact.php" method="post">
<div class="col-md-10 col-md-offset-2">
<button type="submit">Send Message</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</section>
enter code here
you have declare form method and its action in form tag. Something like as follows
<form action="actionPage.php" method="POST"> //you can send data in get method or post method
Hope this will work for you...
action="yourLoctionUrl" tell the form where to submit data.
if you will do it empty then it will show ?yourinput fields&other inputs
<form action="sendcontact.php" method="post" class="form-horizontal">
<fieldset>
<div class="form-group is-empty">
<label for="Name" class="col-md-2 control- label">Name</label>
<div class="col-md-10">
<input type="text" class="form-control" id="Name" placeholder="Name">
</div>
</div>
<div class="form-group is-empty">
<label for="Email" class="col-md-2 control-label">Email</label>
<div class="col-md-10">
<input type="email" class="form-control" id="Email" placeholder="Email">
</div>
</div>
<div class="form-group is-empty">
<label for="Message" class="col-md-2 control-label">Message</label>
<div class="col-md-10">
<input type="text" class="form-control" id="Message" placeholder="Message">
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-2">
<button type="submit">Send Message</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
You have used form tag twice in your coding. put your action and post in starting form tag as i did.
You didn't close the second form tag. Go for this:
<form action="sendcontact.php" method="post">
<div class="col-md-10 col-md-offset-2">
<button type="submit">Send Message</button>
</div>
</form>