dpblwmh5218 2014-09-30 10:47
浏览 248
已采纳

在CodeIgniter中使用this-> db-> escape_str()函数时出现语法错误

I want to send my input data to an SQL Query in my CodeIgniter application in the following way:

  $sdata['department'] = $this->input->post('department'); //Data as text input 
  $sql = "SELECT MAX(roll) FROM student WHERE department = ".this->db->escape_str($sdata['department']);

The above code is inside my controller.

And I keep getting the following error:

  Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

Most likely, this is a syntax error, but I just can't figure out which part of my code is responsible.

I've also gone through the Queries section of the user guide of CodeIgniter, but it wasn't explained clearly there.

Can anyone please tell me where my mistake is, and what is the correct syntax for what I'm trying to do?

My controller -

   <?php
   if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
   class Student extends CI_Controller
   {
       function __construct()
       {
           parent::__construct();
           #$this->load->helper('url');
           $default_roll = '20141000';
           $this->load->model('student_model');
           $this->load->helper('string');
       }

       //Show all Students
       public function index()
       {
            $data['student_list'] = $this->student_model->get_all_students();
            $this->load->view('student_view', $data);
       }


      //Insert a student
      public function insert_student_db()
      {
           $sdata['name'] = $this->input->post('name');
           $sdata['department'] = $this->input->post('department');

           $sql = "SELECT MAX(roll) FROM student WHERE department = ".this->db->escape_str($sdata['department']);
           $query = $this->db->query($sql);

           $rolltext = substr($query, 9);
           $year = substr($query, 3, -3);

          if($rolltext == NULL && $year == NULL)
          {
               $rolltext = str_pad(1,3,'0',STR_PAD_LEFT);
               $year = '2014';
          }

         else
         {
             $rolltext++;
             $rolltext = str_pad($rolltext,3,'0',STR_PAD_LEFT);
             if($rolltext == '100')
             {
                  $rolltext = str_pad(1,3,'0',STR_PAD_LEFT);
                  $year++;
             }
         }  

         $sdata['roll'] = $sdata['department'].$year.$rolltext;

         $sdata['email'] = $this->input->post('email');
         $sdata['mobile'] = $this->input->post('mobile');
         $res = $this->student_model->insert_student($sdata);
        if($res)
        {
            header('location:'.base_url()."index.php/student/".$this->index());

        }

    }   
}
?>  
  • 写回答

1条回答 默认 最新

  • dongyu1918 2014-10-02 03:50
    关注

    [Courtesy: user Ismael Miguel]

    Problem was with this line inside my controller, student.php:

      $sql = "SELECT MAX(roll) FROM student WHERE department = ".this->db->escape_str($sdata['department']);
    

    A very silly mistake, in a very basic thing. All variables in PHP must be preceded with a '$' sign. I just missed that.

    So, after correcting that bit, it looks like this:

    $sql = "SELECT MAX(roll) FROM student WHERE department = ".$this->db->escape_str($sdata['department']);
    

    And it's working fine now.

    PHP didn't recognise this, without the '$' sign. That's why it threw the parse error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题