This is my form :
<form method = "POST" action = "<?php echo base_url('Usercontroller/insert') ?>">
<div class="form-group">
<label for="exampleInputEmail1">Apartament</label>
<input type="text" name ="txtApartament" class="form-control" id="txtApartament" placeholder="Apartament">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Status</label>
<select name ="txtStatus" class="form-control">
<?php foreach($getStatus as $value) { ?>
<option value = "<?php echo $value->per_id ?>"><?php echo $value->status_name;?></option>
<?php }?>
</select>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Nume</label>
<input type="text" name ="txtNume" class="form-control" id="txtNume" placeholder="Nume">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Persoane</label>
<input type="text" name ="txtPersoane" class="form-control" id="txtPersoane" placeholder="Personae">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Mp</label>
<input type="text" name ="txtMp" class="form-control" id="txtMp" placeholder="Mp">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Comentariu</label>
<input type="text" name ="txtComentariu" class="form-control" id="txtComentariu" placeholder="Comentariu">
</div>
<button type="submit" class="btn btn-default">Salveaza</button>
</form>
And this is my controller insert function side:
public function insert() {
$datai= $this->input->post();
if(isset($datai)){
$txtApartament = $datai['txtApartament'];
$txtStatus = $datai['txtStatus'];
$txtNume = $datai['txtNume'];
$txtPersoane = $datai['txtPersoane'];
$txtMp = $datai['txtMp'];
$txtComentariu = $datai['txtComentariu'];
$this->Usermodel->insertUser($txtApartament,$txtStatus,$txtNume,$txtPersoane,$txtMp,$txtComentariu);
redirect('');
}
}
Model side:
public function insertUser($apartament, $status, $nume, $persoane, $mp, $comentariu){
$arrayDates = array(
'apartament' => $apartament,
'per_id' => $status,
'nume' => $nume,
'persoane' => $persoane,
'mp' => $mp,
'comentariu' => $comentariu
);
$this->db->insert('membri', $arrayDates);
}
When I will submit on my forum I will get this :
A Database Error Occurred
Error Number: 1048
Column 'apartament' cannot be null
INSERT INTO `membri` (`apartament`, `per_id`, `nume`, `persoane`, `mp`, `comentariu`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL)
Filename: models/Usermodel.php
Line Number: 28
Also this:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: txtApartament
Filename: controllers/Usercontroller.php
Line Number: 18
Backtrace:
File: /var/www/html/adminigniter1/application/controllers/Usercontroller.php
Line: 18
Function: _error_handler
File: /var/www/html/adminigniter1/index.php
Line: 315
Function: require_once
When my controller gets the data, will make it NULL, my form is passing the data correctly(checked the headers), what can be the issue on the controller side ?
And yes, I have set the helper with url and form!
I found the solution, wasn't from my code, it was apache2 ...
in the rewrite.load file in apache, I added the line :
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
And it worked!
Related
My model:
class Admin_model extends CI_Model{
function add_blog($data){
$this->db->insert('blog',$data);
return true;
}
}
My controller function:
function add_blog(){
$this->form_validation->set_rules('heading','Heading', 'trim|required');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error',"Please check the required field");
redirect('admin/dashboard/add');
}else{
//for upload file
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'pdf|doc|docx|txt|jpeg|png|ppt';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
$data['picture'] = $image_path;
$data_image = array('upload_data' => $this->upload->data());
}
//Upload file end
$data['picture'] = $data_image['upload_data']['file_name'];
$data['heading'] = ucwords($this->input->post('heading'));
$data['description'] = ucfirst($this->input->post('description'));
$data['type_id'] = ucwords($this->input->post('type_id'));
$data['price'] = $this->input->post('price');
$insert_data = $this->admin_model->add_blog($data);
if($insert_data){
echo '<script>alert("Note added sucessfully.");</script>';
redirect('admin/dashboard');
}else{
$this->session->set_flashdata('message',"There is problem in inserting data, please retry once");
echo "error at last ";
redirect('admin/dashboard/add');
}
}
}
My view:
<form role="form" name="myForm" method="post" enctype="multipart/form-data" action="<?php echo site_url('admin/dashboard/add_blog');?>">
<label for="">Category</label>
<select name="type_id" id="" class="form-control" >
<?php
if(count($product_list )>0 ){
foreach ($product_list as $key => $value) {
?>
<option value="<?php echo $value['type_id'];?>"><?php echo $value['type'];?></option>
<?php
}}?>
</select>
<label for=""> Heading </label>
<input type="text" name="heading"class="form-control">
<label for="">Price</label>
<input type="number" name="price" class="form-control">
<label for="">Description</label>
<textarea name="description" id="" cols="30" rows="5" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="exampleInputFile">Upload pic</label>
<div class="input-group">
<div class="custom-file">
<!-- <input type="file" name="userfile" class="form-control"> -->
<?php echo form_upload(['name'=>'userfile']); ?>
</div>
</div>
<?php if(isset($upload_error)){echo $upload_error;}?>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data_image
Filename: admin/Dashboard.php
Line Number: 64
Backtrace:
File:
/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php
Line: 64 Function: _error_handler
File: /opt/lampp/htdocs/shremad/index.php Line: 315 Function:
require_once
A PHP Error was encountered
Severity: Notice
Message: Trying to access array offset on value of type null
Filename: admin/Dashboard.php
Line Number: 64
Backtrace:
File:
/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php
Line: 64 Function: _error_handler
File: /opt/lampp/htdocs/shremad/index.php Line: 315 Function:
require_once
A Database Error Occurred
Error Number: 1048
Column 'picture' cannot be null
INSERT INTO blog (picture, heading, description, type_id,
price) VALUES (NULL, 'Djjdjdj', 'Jjdjdj', '1', '')
Filename: models/Admin_model.php
Line Number: 5
Let's analyze the error message first.
Message: Undefined variable: data_image
Which means, somewhere in the code you're calling a variable named "data_image" which is undefined. Now, please notice that this is a NOTICE, so there might be cases that your code would work well BUT it's a bad practice and might cause troubles. (As in your case for instance).
Looking at your code, the following "block":
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
$data['picture'] = $image_path;
$data_image = array('upload_data' => $this->upload->data());
}
//Upload file end
$data['picture'] = $data_image['upload_data']['file_name'];
The $data_image variable is defined only under the "else" statement.
So if the condition returns true, the $data_image variable is truly undefined.
In that case, the following command:
$data['picture'] = $data_image['upload_data']['file_name'];
which is based on the $data_image variable, would return another problem:
Message: Trying to access array offset on value of type null
so $data['picture'] now is NULL, which cause your last problem:
Column 'picture' cannot be null
The solution?
Make sure you have a value for $data_image even in the case that the condition returns true. Or, if this condition returns true - handle the things differently. Please also notice that there might be a logical problem with the function in the condition that returns an unexpected result.
(The condition: if ( ! $this->upload->do_upload()))
Trying to learn Coedigniter here and struggling a little bit from the start. I basically want to create a query in my model using a value in the url.
Controller (Customer.php)
public function customer_edit($id) {
$this->load->model( 'Customers_model' ); //loads model
$data[ 'results' ] = $this->Customers_model->edit_customers($id);
$this->load->view( 'customers/customer_edit', $data );
}
Model (customers_model.php)
public function edit_customers($id)//This function returns an array
{
$this->load->database('default', TRUE); //connect to database
$this->db->where(['idcustomers'=>$id]); //creates where statement from url
$query = $this->db->get('customers');//create query
return $query->result_array(); //creates array from query
}
View (customer_edit.php)
<div class="form-group">
<label class="control-label col-sm-2">First Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtfname" value="<?php echo $results['fname'];?>">
</div>
<label class="control-label col-sm-2">Last Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtlname" value="">
</div>
</div>
When I go to http://blahblah.com/customers/customer_edit/454 I get the following error:
Message: Undefined index: fname
Filename: customers/customer_edit.php
Line Number: 10
This is what I know:
On other pages where I am just displaying all the records in a table it works fine so I am not having a database connection problem. fname and idcustomers are the correct column names in the database.
Thanks in advance for your help.
Here is how I solved the problem. I added a foreach at the beginning of the form like this:
<div class="form-group">
<label class="control-label col-sm-2">First Name:</label>
<div class="col-sm-3"><input type="text" class="form-control" name="txtfname" value="<?php echo ucwords(strtolower($object['fname']));?>">
</div>
Thanks Ghost for helping answer this question.
I have been having a problem... a frustrating problem at that. I cannot seem to edit a specific row in Codeigniter. I have found previous questions on the same here, and even tried the solutions but to no avail. I am press for time on this project I am undertaking. Before you regard this question as a duplicate please have a look see. Any help will be greatly appreciated... Thank you in advance My code snippets are below:
Codeigniter/Controller
<?php..
//Selects all from admin table
function get_admin(){
$data['query'] = $this->Superuser_Model->selectadmin();
}
//brings in the view
// function editAdmin(){
// $data['content'] = 'admin/edit_admin';
// $this->load->view('include/template_back', $data);
// }
//click a specific row in the view (tabulated data)
function edit($id){
$data['array']= $this->Superuser_Model->editadmin($id);
// $data['content'] = 'admin/edit_admin';
$this->load->view('include/header_back');
$this->load->view('admin/edit_admin', $data);
$this->load->view('include/footer_back');
}
//Should update the from
function update_superuser(){
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run()==FALSE)
{
$data['content'] = 'admin/add_admin';
$this->load->view('include/template_back', $data);
}
else
{
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$date_added = $this->input->post('date_added');
$this->Superuser_Model->update_superuser($username,$password,$date_added);
redirect('login/index', 'refresh');
}
}
..?>
Codeigniter/Model
<?php...
function selectadmin(){
// $id = $this->uri->segment(3);
$query = $this->db->get('admin');
return $query->result_array();
}
function editadmin($id){
$id = $this->uri->segment(3);
$query = $this->db->get('admin');
$this->db->where('adminID', $id);
return $query->result_array();
}
function update_superuser($data, $id){
$this->uri->segment(3);
$id = $this->input->post('adminID');
$data = array(
'username'=> $this->input->post('username'),
'password'=> $this->input->post('password'),
'date_added'=> $this->input->post('date_added')
);
$this->db->where('adminID', $id);
$this->db->update('admin', $data);
}
...?>
Codeigniter/View
...
<?php echo form_open('superuser/update_superuser', array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')); ?>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
×
−
</div>
<h4 class="panel-title">Admin Details</h4>
<p>Please, Insert your details here below... (for Superuser use only)</p>
</div>
<div class="panel-body panel-body-nopadding">
<!--username-->
<div class="form-group">
<!-- <input type="hidden" name="adminID" class="form-control" value="<?php echo $array->adminID;?>"/> -->
<label class="col-sm-4 control-label">Username</label>
<div class="col-sm-8">
<input type="text" name="username" class="form-control" value="<?php echo $array['username'];?>"/>
</div>
// <?php // form_error('username');?>
</div>
<!--password-->
<div class="form-group">
<label class="col-sm-4 control-label">Password</label>
<div class="col-sm-8">
<input type="password" name="password" class="form-control" value="<?php echo $array['password'];?>"/>
</div>
<?php //echo form_error('date_added');?>
</div>
<!--Date Added-->
<div class="form-group">
<label class="col-sm-4 control-label">Date</label>
<div class="col-sm-8">
<input type="text" name="date_added" class="form-control" id="datepicker" value="<?php echo $array['date_added'];?>" /> <img src="<?php echo base_url();?>components/backend/images/calendar.gif" alt="" /><br /><br />
</div>
<?php //echo form_error('date_added');?>
</div>
</div><!-- panel-body -->
<div class="panel-footer">
<button class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</div><!-- panel-footer -->
</div><!-- panel-default -->
<?php form_close();?>
...</body></html>
Errors Displayed
A PHP Error was encountered
Severity: Notice
Message: Undefined index: username
Filename: admin/edit_admin.php
Line Number: 54
A PHP Error was encountered
Severity: Notice
Message: Undefined index: password
Filename: admin/edit_admin.php
Line Number: 62
A PHP Error was encountered
Severity: Notice
Message: Undefined index: date_added
Filename: admin/edit_admin.php
Line Number: 70
Seems like not correct $array['username'], $array['password'] and $array['date_added']. Print out $array first and you'll see what's wrong.
I see a few things... Firstly, you are trying to use the $this->input->post method which is beyond the scope of the model (relevant only to the controller). This is what provides you with the php errors...
Secondly, you are passing the right parameters to the model (minus cleaning them from XSS attacks, notice), but you are accepting different ones, so in the model your function should look something like this
function update_superuser($username, $password, $data_added){
$id = $this->input->post('adminID'); // I believe you should be getting the id from the database/session honestly, much safer in these cases
$data = array(
'username'=> $username,
'password'=> $password,
'date_added'=> $data_added
);
$this->db->where('adminID', $id);
$this->db->update('admin', $data);
}
Hope this helps!
You are passing 3 parameters to your update_superuser() function here
$this->Superuser_Model->update_superuser($username,$password,$date_added);
and your function in your model only takes 2 params:
function update_superuser($data, $id){
$this->uri->segment(3);
$id = $this->input->post('adminID');
$data = array(
'username'=> $this->input->post('username'),
'password'=> $this->input->post('password'),
'date_added'=> $this->input->post('date_added')
);
$this->db->where('adminID', $id);
$this->db->update('admin', $data);
}
Im trying to insert multiple checkboxes but cant get their values in codeigniter 2
this is my code in View
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">
<div class="col-md-4">
<label class="checkbox-inline" for="checkboxes-0">
<input type="checkbox" name="checkboxes[]" id="checkboxes-0" value="22">
Пентхаус
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
<label class="checkbox-inline" for="checkboxes-1">
<input type="checkbox" name="checkboxes[]" id="checkboxes-1" value="21">
Гараж/Паркомясто
</label>
<br>
<!-- Text input-->
<div class="form-group">
<div class="col-md-8">
<input id="cena" name="cena[]" type="text" placeholder="Въведи цена" class="form-control input-md">
</div>
</div>
This is my Model:
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$this->db->insert_batch('property_type_details', $property_typesRequest);
}
}
in Controller i just use this:
$this->estate_m->InsertCheckbox();
And this going insert 0 in database, when i var_dump $property_typesRequest theres shows bool(false). I cant get values of checkboxes...
EDIT...
I have try to edit my code but still no result:
public function edit()/*this is controller */
{
$data=array('column_name'=>$this->input->post('checkboxes');
$result=$this->estate_m->InsertCheckbox($data);
if($result==true)
{
echo "Success";
}
else
{
echo "Fail";
}
}
public function InsertCheckbox($data) /*this is Model */
{
$this->db->insert('property_type_details', $data);
return ($this->db->affected_rows() != 1 ) ? false : true;
}
With this edited code always gives me Succes
Form submitted values should be in multi dimension array
To achieve that your form inputs should be in multi dimension.
For insert_batch() function, the array should be in multi dimension.
In array every key must be field names in db table and value must be the form input value.
So change the form structure like the below array.
array(
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
),
array(
'checkboxes' => 'checkboxe value' ,
'cena' => 'cena values'
)
);
Form inputs should be like below:
<input name="data[1][checkbox_columnname]" type="checkbox" value="21">Пентхаус
<input name="data[1][textbox_columnname]" type="text">
<input name="data[2][checkbox_columnname]" type="checkbox" value="22">Гараж/Паркомясто
<input name="data[2][textbox_columnname]" type="text">
And the model should not have foreach loop.
Simply pass the data as below.
$data=$this->input->post('data');
$this->db->insert_batch('mytable', $data);
Please use this code in model.
public function InsertCheckbox() {
$property_typesRequest = $this->input->post('checkboxes');
foreach($property_typesRequest as $value){
$data['columnename'] = $value;
$this->db->insert('tablename', $data);
}
}
Hope it inserts into the database
Thank you
I want to make an edit form , but I have a problem when displaying the data . This is a warning in my code ..
A PHP Error was encountered
Severity: Notice
Message: Undefined index: pakar_username
Filename: m_pakar/edit_pakar.php
Line Number: 20
This is my view:
<form method="post" role="form" action="<?=base_url()?>admin/m_pakar/edit_pakar?>">
<label>Username</label>
<input class="form-control" type="username" name="username" value="<?php echo $coba['pakar_username']?>" ><br>
<label>Password</label>
<input class="form-control" type="text" name="password" value="<?php echo $coba['pakar_password']?>"><br>
<label>Email</label>
<input class="form-control" type="email" name="email" value="<?php echo $coba['pakar_email']?>" ><br>
<button type="submit" class="btn btn-success">Update</button>
</form>
My Controller:
public function edit_pakar($id){
$this->general->set_table('data_pakar');
$this->general->order('pakar_id', 'asc');
$datasend['coba'] = $this->general->get_result_array();
$datasave = array(
'pakar_username' => $this->input->post('username', TRUE),
'pakar_password' => md5($this->input->post('password', TRUE)),
'pakar_email' => $this->input->post('email', TRUE),
);
$this->general->set_table('data_pakar');
$this->general->where($datasave);
$this->general->update($datasave);
$dataview['content'] = $this->load->view('admin/m_pakar/edit_pakar', $datasend, TRUE);
$this->load->view($this->template, $dataview);
}
Is there something wrong with my code ? not only pakar_username , but all existing data on the edit form doesn't show.
Why have you got all this bloated code in your controller? Move the Database stuff to a model...
HTML Form Fix:
<form method="post" role="form" action="<?=base_url()?>admin/m_pakar/edit_pakar">
Secondly, you're not pushing the details in "datasave" to the view, you're only pushing "datasend", so I adjusted for "coba2" incase you need coba...
$datasend['coba'] = $this->general->get_result_array();
$datasave = array(
'pakar_username' => $this->input->post('username', TRUE),
'pakar_password' => md5($this->input->post('password', TRUE)),
'pakar_email' => $this->input->post('email', TRUE),
);
$datasend['coba2'] = $datasave;
Now edit input fields as so:
value="<?php echo $coba2['pakar_username']?>"