1

Please help in my code to insert data to database it return with error

A PHP Error was encountered Severity: Notice Message: Undefined variable: computerName Filename: models/add_model.php Line Number: 20

View:

<form action="<?php echo base_url();?>index.php/speed/insert_into_db" method="post">

Branch: <input type="text" name="branch" /><br/>

Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />

</form>

model:

public function insert_into_db() { 

if(isset($_POST['save'])) {

$branch = $_POST['branch'];

$buinessUnit = $_POST['buinessUnit'];

$deviceType = $_POST['deviceType'];

$brand = $_POST['brand'];

$deviceModel=$_POST['deviceModel'];

$SN = $_POST['SN'];

$status = $_POST['status'];

$department = $_POST['department'];

$username = $_POST['username'];

$notes = $_POST['notes'];

$computerName = $_POST['computerName'];

}          

$this->db->query("INSERT INTO `hardware_assets`(`Branch`, `Business Unit`, `Device Type`, `Brand`, `Device Model`, `SN`, `Status`, `Departmant`, `UserName`, `Notes`, `Computer Name`)
VALUES ('$branch','$buinessUnit','$deviceType','$brand','$deviceModel','$SN','$status','$department','$username','$notes','$computerName')");
}

}

Controller:

<?php

class Speed extends CI_Controller {

public function view($page = 'home')
{

    // if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
    // {
    // // Whoops, we don't have a page for that!
    // show_404();
    // }

    // $data['title'] = ucfirst($page); // Capitalize the first letter

    // $this->load->view('templates/header', $data);
    // $this->load->view('pages/'.$page, $data);
    // $this->load->view('templates/footer', $data);

    // }

    function insert_to_db(){
    $this->load->model('add_model');
    $this->add_model->insert_into_db();
    $this->load->view('pages/home');//loading success view
    }

}
Mohamed Hamed
  • 59
  • 3
  • 11
  • This may not help but just picked up: You have a model function inside your view function on controller? To submit form with codeigniter you need to also use codeigniter form validation library. –  May 22 '15 at 14:57
  • And here is the user guide for form_validation http://www.codeigniter.com/user_guide/libraries/form_validation.html and http://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data –  May 22 '15 at 15:06

2 Answers2

1

You may not name your table fields with space in name i.e. Computer Name should be computerName or computer_name.

ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)

Reference.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
1

your model is not OK at all, the $this->db->query() is outside of condition block,I'm guessing you get this err when no $_POST['save'] is set, it's better to rewrite your model

so change model to this(we use query builder which is a better practice in most cases)

function insert_into_db() {
$post = $this->input->post();
if (!isset($post['save']) return;
$data = ['Branch'=> $post['branch'], 'Business Unit'=>$post['buinessUnit'] and rest of it.....];
$this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql

some notes which help you to avoid issues like this later or to raise security:

always use form helper to create a form

always use query builder for model and also make sure you are validating data correctly

never use $_POST, use $this->input->post() instead

if you want to use a variable in a string, always put variable in {},i.e: echo "you are at {$place}"

never use spaces for column name

it's better to name attribute match column name to make your code shorter,faster and more clear and makes no err(in this case, you even don't need $data, you could directly use $post)

Mohammadhzp
  • 498
  • 7
  • 20
  • i change it but i have new error A PHP Error was encountered Severity: Warning Message: call_user_func_array() expects parameter 1 to be a valid callback, class 'Speed' does not have a method 'view' Filename: core/CodeIgniter.php Line Number: 360 – Mohamed Hamed May 22 '15 at 16:11
  • it seems you need to ask a new question for this, This a very strange err, make sure you are using stable CI and you don't have typo in method names and class names and url is correct – Mohammadhzp May 22 '15 at 16:15
  • i install it again and it's ok,but record didn't inserted into database after change model and name of database fields without space – Mohamed Hamed May 22 '15 at 16:38
  • use [this answer](http://stackoverflow.com/a/6575502/3152155) and put the code after switch() {} statement in index.php to capture error, also turn on profiler(see CI docs for how to) to observe what query executed, tell me results – Mohammadhzp May 22 '15 at 17:06
  • No POST data exists---- No queries were run – Mohamed Hamed May 22 '15 at 17:27
  • if no post exists, then you are missing something in html, try to validate html, use print_r($_POST) to make sure data posted, then check again, use for your form – Mohammadhzp May 22 '15 at 17:31
  • thank you for help me it's okay,form_open solve problem – Mohamed Hamed May 22 '15 at 18:25