Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/debugging/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging CodeIgniter查询语法错误_Debugging_Codeigniter_Syntax - Fatal编程技术网

Debugging CodeIgniter查询语法错误

Debugging CodeIgniter查询语法错误,debugging,codeigniter,syntax,Debugging,Codeigniter,Syntax,我面临一个CodeIgniter数据库查询的语法问题。不知道怎么了 $query = $this->db->query(" INSERT IGNORE INTO ".$table." (email, lang, ip_address) VALUES (".$this->db->escape($_POST['email']).", ".$this->db->escape($lang).", ".$this->input->ip_addr

我面临一个CodeIgniter数据库查询的语法问题。不知道怎么了

$query = $this->db->query("
   INSERT IGNORE INTO ".$table." (email, lang, ip_address) 
   VALUES (".$this->db->escape($_POST['email']).", ".$this->db->escape($lang).", ".$this->input->ip_address().")");
我也在寻找一种方法,在替换占位符后输出查询的外观,因为我对CodeIgniter调试选项有点困惑

使用

echo $this->db->last_query();
用于检索运行的查询


然后检查查询的格式是否正确。

以了解要传递到数据库的查询。使用下面的语句和将数据插入数据库。请按照以下程序操作

  echo $this->db->last_query();

     $data = array(
           'email' =>  $this->db->escape($_POST['email']),
           'lang' = >  $this->db->escape($lang),
           'ip_address' => $this->input->ip_address(),
     );

 Call your model function $this->model->insert_function_name($data);

Your model function in your model file
public function insert_function_name($data)

  {
         $this->db->insert($table_name,$data);
    return $this->db->insert_id();
  } 

看起来好像您没有转义要输入到数据库中的字符串。您发布的查询的计算结果如下:

$query = $this->db->query("
    INSERT IGNORE INTO table_name (email, lang, ip_address) 
    VALUES (email@email.com, en, 192.168.0.1)
");
$query = $this->db->query("
    INSERT IGNORE INTO ".$table." (email, lang, ip_address) 
    VALUES ('".$this->db->escape($_POST['email'])."', '".$this->db->escape($lang)."', '".$this->input->ip_address()."')
");
这将引发一个错误,因为值中的字符串没有正确转义。您不应使用正在运行的查询,而应使用以下内容:

$query = $this->db->query("
    INSERT IGNORE INTO table_name (email, lang, ip_address) 
    VALUES (email@email.com, en, 192.168.0.1)
");
$query = $this->db->query("
    INSERT IGNORE INTO ".$table." (email, lang, ip_address) 
    VALUES ('".$this->db->escape($_POST['email'])."', '".$this->db->escape($lang)."', '".$this->input->ip_address()."')
");

请注意每个字符串周围的新“字符”。

尝试以下操作:您的查询在字符串类型的值(如email、lang和ip)中缺少单引号

$query = $this->db->query("
   INSERT IGNORE INTO ".$table." (email, lang, ip_address) 
   VALUES ('".$this->db->escape($_POST['email'])."', '".$this->db->escape($lang)."', '".$this->input->ip_address()."')");

好的,如果你认为这是正确的答案,请选择它作为答案。