mheavers
mheavers

Reputation: 30208

PHP Code Igniter Database Error

Can't figure out what's wrong here:

class Model_form extends CI_Model
{
  function __construct()
  {
        // Call the Model constructor
        parent::__construct();
  }

  function add_tree()
  {
    $v_treename = $this->input->post('f_treename');
    $v_treedesc = $this->input->post('f_treedesc');
    $v_treeid = $v_treename;
    $this->db->query("INSERT INTO trees (index, tree_name, tree_desc, tree_id) VALUES (NULL, '$v_treename', '$v_treedesc', '$v_treeid') "); //PROBLEM OCCURS HERE
} 

Get this error:

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index, tree_name, tree_desc, tree_id) VALUES (NULL, 'TEST', 'TEST', 'TEST')' at line 1 

I've used similar code on another project and it worked fine. Running on local server with MAMP. Thanks for any help you can provide.

Upvotes: 0

Views: 982

Answers (3)

Kristoffer Bohmann
Kristoffer Bohmann

Reputation: 4114

This is a MySQL error and it happens because your 'trees' table is using a reserved word for the 'index' column.

You can add `index` backticks around the column name - or even better: change the 'index' column name to 'id' or similar.

For a list of reserved words in MySQL, please see: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Upvotes: 1

cOle2
cOle2

Reputation: 4784

Index is a reserved word in mysql. You need to put backticks around the column names ie `index`, `tree_name` etc.

Upvotes: 5

Dormouse
Dormouse

Reputation: 5198

Index is a protected word in mysql. Use `index` with backticks.

Protected words are those which must be escaped inside a query when referring to them as a field. A full list is available here

Upvotes: 2

Related Questions