Php 在我的CodeIgniter应用程序中为已经存在的数据添加验证不会';行不通
我有一个CodeIgniter web应用程序,它获取学生信息并将其保存在名为Php 在我的CodeIgniter应用程序中为已经存在的数据添加验证不会';行不通,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我有一个CodeIgniter web应用程序,它获取学生信息并将其保存在名为student的表中。我的表格结构如下: name varchar(30) roll varchar(10) department varchar(10) email varchar(50) mobile varchar(15) 在我的应用程序中,我希望实现以下行为: 插入数据时,如果
student
的表中。我的表格结构如下:
name varchar(30)
roll varchar(10)
department varchar(10)
email varchar(50)
mobile varchar(15)
在我的应用程序中,我希望实现以下行为:
插入数据时,如果卷数据已经存在,则在卷文本字段旁边将显示验证,说明该卷编号已经存在。请输入新卷。这意味着,对于滚动数据的重复条目,将出现验证,并且不会插入该数据
我的控制器是student.php
,请注意,它包含回调函数和验证规则:
<?php
if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
class Student extends CI_Controller
{
function __construct()
{
parent::__construct();
#$this->load->helper('url');
$this->load->model('student_model');
$this->load->library('form_validation');
$this->load->library('session');
}
//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()
{
$udata['name'] = $this->input->post('name');
$udata['roll'] = $this->input->post('roll');
$udata['department'] = $this->input->post('department');
$udata['email'] = $this->input->post('email');
$udata['mobile'] = $this->input->post('mobile');
$this->form_validation->set_rules('roll', 'Roll', 'callback_check_duplicate_roll');
if($this->form_validation->run() == TRUE)
{
$res = $this->student_model->insert_student($udata);
if($res)
{
header('location:'.base_url()."index.php/student/".$this->index());
}
}
else
{
$this->session->set_flashdata('warning', 'Data already exists');
redirect('/student/index/', 'refresh');
}
}
// My callback function
public function check_duplicate_roll($post_roll)
{
return $this->student_model->checkDuplicateRoll($post_roll);
}
}
?>
我的视图页面-student\u view.php
:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My first site in CI</title>
</head>
<body>
<h2>Student Information</h2>
<form method="post" action="<?php echo base_url();?>index.php/student/insert_student_db">
<table width="800" border="0">
<tr>
<th width="213" align="right" scope="row">Name:</th>
<td width="161"><input type="text" name="name" size="60" /></td>
</tr>
<tr>
<th align="right" scope="row">Roll:</th>
<td><input type="text" name="roll" size="60" /></td>
</tr>
<tr>
<th align="right" scope="row">Department:</th>
<td>
<select name="department">
<?php
$sql = mysql_query("SELECT dept_name FROM department");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"{$row['dept_name']}\">" . $row['dept_name'] . "</option>";
}
?></select>
</td>
</tr>
<tr>
<th align="right" scope="row">Email:</th>
<td><input type="text" name="email" size="60" /></td>
</tr>
<tr>
<th align="right" scope="row">Mobile:</th>
<td><input type="text" name="mobile" size="60" /></td>
</tr>
<tr>
<th align="right" scope="row"> </th>
<td><input type="submit" name="submit" value="Send" /></td>
</tr>
</table>
</form>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Name</th>
<th scope="col">Roll</th>
<th scope="col">Department</th>
<th scope="col">Email</th>
<th scope="col">Mobile</th>
</tr>
<?php foreach ($student_list as $std_key){ ?>
<tr>
<td><?php echo $std_key->name; ?></td>
<td><?php echo $std_key->roll; ?></td>
<td><?php echo $std_key->department; ?></td>
<td><?php echo $std_key->email; ?></td>
<td><?php echo $std_key->mobile; ?></td>
</tr>
<?php }?>
</table>
</body>
</html>
我在CI中的第一个站点
学生信息
电邮:
流动电话:
名称
滚
部门
电子邮件
可移动的
在localhost中运行应用程序并尝试插入数据后,重复的滚动数据不会插入到我的student
表中,这很好,我的页面被重定向到student\u view.php
。但是闪光信息没有显示出来。有什么问题?这是我放错flashdata的问题吗?还是别的什么
请注意,这是我的问题的最终、完全修改的版本。自从我第一次发布这个问题以来,我已经编辑了4次,由于所有这些编辑,它看起来很难看。因此,我决定根据我的代码的最新更新版本修改我的问题。请原谅这个问题的性质和我无法解决这个问题,因为我对CodeIgniter绝对是个新手。您的帮助将帮助我理解CI,并鼓励我继续这一旅程 在您当前的代码中,验证在哪里都找不到。首先在控制器中初始化库:
$this->load->library('form_validation');
然后当一切正常时,制定规则,等等:
if($this->form_validation->run() !== false) {
$res = $this->student_model->insert_student($udata);
}
有关更多信息:
编辑:
由于您有自己的验证,因此也可以使用该自定义验证:
$this->form_validation->set_rules('roll', 'Roll', 'callback_checkDuplicateRoll');
在您的定制功能中:
public function checkDuplicateRoll($post_roll)
{
$this->db->where('roll', $roll);
$query = $this->db->get('student');
$count_row = $query->num_rows();
if ($count_row > 0)
{
$this->form_validation->set_message('checkDuplicateRoll', 'This roll number already exists. Please enter a new roll.');
return FALSE;
}
else
{
return TRUE;
}
}
student.php
public function checkDuplicateRoll($roll) {
return $this->student_model->checkDuplicateRoll($roll);
}
如果在验证规则中定义回调函数,则验证程序将调用设置验证规则的类中的函数。在这种情况下,学生控制器
编辑
Controller student.php:
public function insert_student_db() {
// check if form is posted and assing variable to post data
if (false !== $udata = $this->input->post()) {
//unset the submit button
unset($udata['submit']);
$this->form_validation->set_rules('roll', 'Roll', 'callback_check_duplicate_roll');
$this->form_validation->set_message('check_duplicate_roll', 'Role already exists');
if ($this->form_validation->run()) {
$this->student_model->insert_student($udata);
redirect('index.php/student');
}
else {
$this->load->helper('form');
$this->load->view('studentForm');
}
}
}
//for some reason a callback function needs to be public
public function check_duplicate_roll($roll) {
return $this->student_model->checkDuplicateRoll($roll) == false;
}
Model student_Model.php
public function ckeckDuplicateRoll($roll) {
$this->db->select('roll');
$this->db->where('roll', $roll);
$result = $this->db->get('student');
return (bool) $result->num_rows();
}
CodeIgniter中的视图
事实上,这里讨论的是MVC,但是视图不应该包含任何编程逻辑,除了一些回声(在迭代中是可选的)、一些小计算和一些条件语句。
它是与模型交互并将获得的数据解析到视图的控制器。
在CI中,它非常简单:
public function index() {
$data = array('title' => 'My first CI site',
'header' => 'First attempt',
'departments' => $this->m_departments->getCollection()
);
$this->load->view('student_view', $data);
}
CI通过extract(核心php函数)运行$data,因此键成为包含值的变量。您可以在视图中使用这些变量:
// departments as example
<select name="department">
<?php
foreach($departments as $row) {
echo '<option value="' . $row['dept_name'] . '">' . $row['dept_name'] . '</option>';
}
?>
</select>
//以部门为例
非常感谢。但现在出现了一个新问题。我还在把重复的卷号插入数据库。表示验证不起作用。我的代码还有问题吗?@CrescentMoon这是自定义验证,请检查我的revisiob@CrescentMoon是的,我又修改了,我改了,反正我想你现在明白了,不走运,伙计。根据您的指示更改了我的代码,仍然插入了重复的卷。表单验证的回调函数必须在controllerI中定义。我没有studentform
。请在“我的编辑”中检查我的视图文件。再次感谢你。我举的例子是为了让你走上正确的方向。请检查Ghost答案中的链接,了解如何使用表单验证和表单帮助器。(作为一个友好的建议)浏览一下整个文档,我认为您可能想了解更多关于MVC模式的信息。非常感谢@Gervs。好吧,我当然会这么做,因为我是CodeIgniter的绝对初学者。但我想获得在CI工作的“实践”经验,这就是为什么我想自己构建一些东西,而不是阅读整个CI用户指南。而且,虽然我知道MVC,但我需要更多地调整我对MVC的了解。因此,你的建议是非常有用和有价值的。你不必阅读完整的文档,但是如果你使用了一些功能,你可以阅读该部分。这会给你省去很多麻烦。我已经用一个简单的例子扩展了我的答案:如何使用视图。非常感谢您花这么多时间向我解释这一点。:)你已经设定好了,但你永远也得不到。
// departments as example
<select name="department">
<?php
foreach($departments as $row) {
echo '<option value="' . $row['dept_name'] . '">' . $row['dept_name'] . '</option>';
}
?>
</select>