我希望以下列方式将我的输入数据发送到CodeIgniter应用程序中的SQL查询:
$sdata['department'] = $this->input->post('department'); //Data as text input
$sql = "SELECT MAX(roll) FROM student WHERE department = ".this->db->escape_str($sdata['department']);
以上代码在我的控制器内。
我一直收到以下错误:
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
最有可能的是,这是一个语法错误,但我无法弄清楚我的代码的哪一部分负责。
我也浏览过CodeIgniter用户指南的Queries部分,但在那里没有清楚解释。
任何人都可以告诉我我的错误在哪里,以及我尝试做什么的正确语法是什么?
我的控制器 -
<?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());
}
}
}
?>
答案 0 :(得分:0)
[礼貌:用户Ismael Miguel]
问题在于我的控制器student.php
内的这一行:
$sql = "SELECT MAX(roll) FROM student WHERE department = ".this->db->escape_str($sdata['department']);
一个非常愚蠢的错误,在一个非常基本的事情。 PHP 中的所有变量必须前面带有“$
”符号。我错过了。
因此,在纠正该位之后,它看起来像这样:
$sql = "SELECT MAX(roll) FROM student WHERE department = ".$this->db->escape_str($sdata['department']);
现在工作正常。
如果没有“this
”符号,PHP无法识别$
。这就是它抛出解析错误的原因。