2011-05-27 96 views
0

不能找出什么是错在这里:PHP代码点火器数据库错误

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 
} 

得到这个错误:

 
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 

我已经用类似的代码的另一个项目,它能正常工作。使用MAMP在本地服务器上运行。感谢您的任何帮助,您可以提供。

回答

5

索引是mysql中的保留字。你需要在列名称周围加上反引号,例如`index`,`tree_name`等。

2

索引是mysql中的一个保护字。用反引号使用`index`。

受保护字是那些在将字段引用到查询时必须转义的字。完整列表可用here

1

这是一个MySQL错误,它发生是因为你的'树'表使用'index'列的保留字。

您可以在列名称周围添加`index`反引号 - 甚至更好:将'index'列名更改为'id'或类似名称。

对于MySQL中保留字列表,请参阅: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html