callback_url_duplicated 使用 CodeIgniter 的数据库中的函数错误
callback_url_duplicated function error in Database with CodeIgniter
我有一个用于在数据库中插入页面的表单,问题是如果 URL 在我的数据库中重复,我无法在表单中检测到,因为我应该配置一个回调 function.The 错误仅在回调函数上,因为我可以使用此表单插入、删除和创建页面。
这是我的控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage[$str]|xss_clean|url_title');
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
function check_namepage($str)
{
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
}
这是我的模型代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('view_error');
}
public function addPage(){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->database();
$add_page = $this->input->post();
unset($add_page['insert']);
$this->db->insert('pages',$add_page);
return $this->db->insert_id();
}
else
{
$this->load->view('view_error');
}
}
}
这是我的查看页面,用于将页面插入数据库:
<body>
<div id="main">
<div class="container">
<div class="page-header">
<h1>Create a New Page</h1>
</div>
<div class="row">
<div class="col-sm-12">
<p>Please enter your details below to form.</p>
<?php
$title_page = array(
'name' => 'TitlePage',
'id' => 'TitlePage',
'maxlength' => '490',
'size' => '90',
'type' => 'text',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('TitlePage'),
);
$section_page = array(
'name' => 'SectionPage',
'id' => 'SectionPage',
'maxlength' => '390',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('SectionPage'),
);
$category_page = array(
'name' => 'CategoryPage',
'id' => 'CategoryPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('CategoryPage'),
);
$name_page = array(
'name' => 'NamePage',
'id' => 'NamePage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('NamePage'),
);
$description_page = array(
'name' => 'DescriptionPage',
'id' => 'DescriptionPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('DescriptionPage'),
);
$body_page = array(
'name' => 'BodyPage',
'id' => 'BodyPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:320px;width:100%;',
'value' => set_value('BodyPage'),
'rows' => '15',
'cols' => '80',
);
?><?php echo form_open();?>
<?php echo form_label('Title of the Page:','lbl_titlepage');?><br/>
<?php echo form_input($title_page);?><?php echo form_error('TitlePage'); ?><br/>
<?php echo form_label('Section of the Page:','lbl_sectionpage');?><br/>
<?php echo form_input($section_page);?><?php echo form_error('SectionPage'); ?><br/>
<?php echo form_label('Category of the Page:','lbl_categorypage');?><br/>
<?php echo form_input($category_page);?><?php echo form_error('CategoryPage'); ?><br/>
<?php echo form_label('Name of the Page (URL):','lbl_namepage');?><br/>
<?php echo form_input($name_page);?><?php echo form_error('NamePage'); ?><br/>
<?php echo form_label('Description of the Page:','lbl_descriptionpage');?><br/>
<?php echo form_input($description_page);?><?php echo form_error('DescriptionPage'); ?><br/>
<?php echo form_label('Contentent of the Page:','lbl_BodyPage');?><br/>
<?php echo form_textarea($body_page);?><?php echo form_error('BodyPage'); ?><br/><br/>
<?php echo form_submit('insert','Create Page')?><br/>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
这是我的数据库:
CREATE TABLE `pages` (
`ID_Page` int(11) NOT NULL,
`TitlePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`SectionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`CategoryPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`NamePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`BodyPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`DescriptionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
ALTER TABLE `pages`
ADD PRIMARY KEY (`ID_Page`);
ALTER TABLE `pages`
MODIFY `ID_Page` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;
将 check_namepage()
方法移到 add_new_page()
方法之外。
并且您不需要在 callback_check_namepage[$str]
中传递 $str
。它应该只是 callback_check_namepage
因为 CI 会自动将 用户提供的字段数据 传递给回调函数。因此,在回调 check_namepage($str)
中,$str
将自动可用。
所以,你的控制器将是这样的:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage|xss_clean|url_title'); // [$str] is removed
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
// moved below
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
private function check_namepage($str)
{
// you have error here, solve it
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
我有一个用于在数据库中插入页面的表单,问题是如果 URL 在我的数据库中重复,我无法在表单中检测到,因为我应该配置一个回调 function.The 错误仅在回调函数上,因为我可以使用此表单插入、删除和创建页面。
这是我的控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage[$str]|xss_clean|url_title');
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
function check_namepage($str)
{
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
}
这是我的模型代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('view_error');
}
public function addPage(){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->database();
$add_page = $this->input->post();
unset($add_page['insert']);
$this->db->insert('pages',$add_page);
return $this->db->insert_id();
}
else
{
$this->load->view('view_error');
}
}
}
这是我的查看页面,用于将页面插入数据库:
<body>
<div id="main">
<div class="container">
<div class="page-header">
<h1>Create a New Page</h1>
</div>
<div class="row">
<div class="col-sm-12">
<p>Please enter your details below to form.</p>
<?php
$title_page = array(
'name' => 'TitlePage',
'id' => 'TitlePage',
'maxlength' => '490',
'size' => '90',
'type' => 'text',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('TitlePage'),
);
$section_page = array(
'name' => 'SectionPage',
'id' => 'SectionPage',
'maxlength' => '390',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('SectionPage'),
);
$category_page = array(
'name' => 'CategoryPage',
'id' => 'CategoryPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('CategoryPage'),
);
$name_page = array(
'name' => 'NamePage',
'id' => 'NamePage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('NamePage'),
);
$description_page = array(
'name' => 'DescriptionPage',
'id' => 'DescriptionPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('DescriptionPage'),
);
$body_page = array(
'name' => 'BodyPage',
'id' => 'BodyPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:320px;width:100%;',
'value' => set_value('BodyPage'),
'rows' => '15',
'cols' => '80',
);
?><?php echo form_open();?>
<?php echo form_label('Title of the Page:','lbl_titlepage');?><br/>
<?php echo form_input($title_page);?><?php echo form_error('TitlePage'); ?><br/>
<?php echo form_label('Section of the Page:','lbl_sectionpage');?><br/>
<?php echo form_input($section_page);?><?php echo form_error('SectionPage'); ?><br/>
<?php echo form_label('Category of the Page:','lbl_categorypage');?><br/>
<?php echo form_input($category_page);?><?php echo form_error('CategoryPage'); ?><br/>
<?php echo form_label('Name of the Page (URL):','lbl_namepage');?><br/>
<?php echo form_input($name_page);?><?php echo form_error('NamePage'); ?><br/>
<?php echo form_label('Description of the Page:','lbl_descriptionpage');?><br/>
<?php echo form_input($description_page);?><?php echo form_error('DescriptionPage'); ?><br/>
<?php echo form_label('Contentent of the Page:','lbl_BodyPage');?><br/>
<?php echo form_textarea($body_page);?><?php echo form_error('BodyPage'); ?><br/><br/>
<?php echo form_submit('insert','Create Page')?><br/>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
这是我的数据库:
CREATE TABLE `pages` (
`ID_Page` int(11) NOT NULL,
`TitlePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`SectionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`CategoryPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`NamePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`BodyPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`DescriptionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
ALTER TABLE `pages`
ADD PRIMARY KEY (`ID_Page`);
ALTER TABLE `pages`
MODIFY `ID_Page` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;
将 check_namepage()
方法移到 add_new_page()
方法之外。
并且您不需要在 callback_check_namepage[$str]
中传递 $str
。它应该只是 callback_check_namepage
因为 CI 会自动将 用户提供的字段数据 传递给回调函数。因此,在回调 check_namepage($str)
中,$str
将自动可用。
所以,你的控制器将是这样的:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage|xss_clean|url_title'); // [$str] is removed
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
// moved below
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
private function check_namepage($str)
{
// you have error here, solve it
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}