【问题标题】:Database error occurred in codeigniter在 codeigniter 中发生数据库错误
【发布时间】:2016-08-30 21:37:11
【问题描述】:

有人可以帮我查询我的问题吗?当我执行它时,输出显示如下。

Error Number: 1064

你有一个错误

SQL 语法;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“?,?,?,?,?,?)”附近使用正确的语法

INSERT INTO touristspot(TouristspotID, AccountID, CategoryID, Name, Description, street, city_area, city) Values(0, 111, ?, ?, ?, ?, ?, ?)

我的控制器:

   public function manager_spotdetails(){


    $data = array(

        'CategoryID' => $this->input->post('category'),
        'Name' => $this->input->post('spotname'),
        'Description' => $this->input->post('desc'),
        'street' => $this->input->post('street'),
        'city_area' => $this->input->post('city_area'),
        'city' => $this->input->post('city')
    );

    $this->load->model('managerm');
    $data['cat'] = $this->managerm->category();
    $this->managerm->createspots($data);
    $this->load->view('manager_spotdetails', $data);


}


public function createTouristspot(){

    $this->load->model('managerm');
    $data['cat'] = $this->managerm->category();
    $this->load->view('manager_spotdetails');
}

我的模特:

    public function createspots($data){


    $b = $_SESSION['accountid'];
    $this->db->trans_start();
    $spot_id= $this->db->insert_id();
    $sql3= "INSERT INTO touristspot(TouristspotID, AccountID, CategoryID, Name, Description, street, city_area, city) Values($spot_id, $b, ?, ?, ?, ?, ?, ?)";      
    $this->db->query($sql3, $data);
    $this->db->trans_complete();
    return $this->db->insert_id();
}

    public function category(){
    $this->db->select('CategoryID');
    $this->db->select('CategoryType');
    $this->db->from('category');
    $query= $this->db->get();
    return $query->result();
}
}

我的观点:

                <div class="form-group">
                         <span class="input-group-addon" id="basic-addon1">Category Type</span>
                        <select class="form-control" name="category">
                            <?php foreach($cat as $row) {?>
                            <option value="<?php echo $row->CategoryID?>"><?php echo $row->CategoryType?></option>
                <?php }?>
                        </select>

谢谢! :)

【问题讨论】:

  • 我无法保存输入到数据库中的那个
  • 在application下的config文件夹中去database.php,然后去db_debug,把它变成这样'db_debug'=&gt;true,找到实际问题并解决它...

标签: php sql codeigniter


【解决方案1】:

更改您的 $sql3 字符串,在 VALUES 中插入单引号('):

$sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) VALUES('$spot_id', '$b', ?, ?, ?, ?, ?, ?)"; 

【讨论】:

  • 我必须在每个值中加上单引号?
【解决方案2】:

使用此方法插入数据

控制器

$data = array(
      "COLUMN_NAME"=>$this->input->post("account_fname"),
      "COLUMN_NAME"=>$this->input->post("account_lname")
);
$insert = $this->Model_name->insert("table_name", $data);

型号

public function insert($table,$data){
        $this->db->insert($table,$data);
        $id = $this->db->insert_id();
        return $id;
}

【讨论】:

    【解决方案3】:

    在添加引号后尝试将列 Name 读取为 sql 中的关键字

    $sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) Values('$spot_id','$b', '', '', '','', '', '')"; 
    

    【讨论】:

      【解决方案4】:

      Controller 中的方法 manager_spotdetails 应该是

      public function manager_spotdetails(){
      $data = array(
      
          'AccountID'=>$_SESSION['accountid'],
          'CategoryID' => $this->input->post('category'),
          'Name' => $this->input->post('spotname'),
          'Description' => $this->input->post('desc'),
          'street' => $this->input->post('street'),
          'city_area' => $this->input->post('city_area'),
          'city' => $this->input->post('city')
      );
      
      $this->load->model('managerm');
      $data['cat'] = $this->managerm->category();
      $this->managerm->createspots($data);
      $this->load->view('manager_spotdetails', $data);
      

      }

      该方法在模型中创建pots应该是

      public function createspots($data){
      $this->db->trans_start();
      $this->db->insert('touristspot', $data);
      $this->db->trans_complete();
      return $this->db->insert_id();
      

      }

      希望它能正常工作。确保您的数组索引名称与数据库表列名称相同。

      【讨论】:

        猜你喜欢
        • 2011-10-16
        • 2017-07-31
        • 2014-05-17
        • 2018-04-15
        • 1970-01-01
        • 2017-01-10
        • 2018-09-18
        • 2017-06-06
        • 2018-03-16
        相关资源
        最近更新 更多