Mohamed Hamed
Mohamed Hamed

Reputation: 59

i'm new in codeigniter and have an error A PHP Error was encountered

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
    }

}

Upvotes: 1

Views: 2085

Answers (2)

Mohammadhzp
Mohammadhzp

Reputation: 558

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)

Upvotes: 1

Tpojka
Tpojka

Reputation: 7111

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.

Upvotes: 1

Related Questions