gpt4 book ai didi

php - 为我的 CodeIgniter 应用程序中的现有数据添加验证不起作用

转载 作者:行者123 更新时间:2023-11-29 05:20:49 27 4
gpt4 key购买 nike

我有一个 CodeIgniter 网络应用程序,它获取学生信息并将它们保存在一个名为 student 的表中。我的表结构是这样的:

   name             varchar(30)
roll varchar(10)
department varchar(10)
email varchar(50)

mobile varchar(15)

在我的应用程序中,我想实现以下行为:

插入数据时,如果滚动数据已经存在,则验证将出现在滚动文本字段旁边,显示此滚动编号已存在。请输入新的卷。这意味着,对于滚动数据的重复条目,将出现验证,并且不会插入该数据。

我的 Controller 是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_model.php,请注意我有一个检查重复滚动数据的方法:

    <?php
class Student_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}

//To retrieve all students
public function get_all_students()
{
$query = $this->db->get('student');
return $query->result();
}

//Checking for duplicate roll
public function checkDuplicateRoll($post_roll)
{
$this->db->where('roll', $roll);
$query = $this->db->get('student');
$count_row = $query->num_rows();
if ($count_row > 0)
{
return FALSE;
}
else
{
return TRUE;
}
}


//To add a new student to the database
public function insert_student($data)
{
return $this->db->insert('student', $data);

}

}
?>

我的 View 页面 - student_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">&nbsp;</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>

在我在本地主机上运行我的应用程序并尝试插入数据后,重复的滚动数据没有插入到我的 student 表中,这很好,我的页面被重定向到 student_view.php。但是 flash 消息没有显示。问题是什么?是我flashdata错位的问题吗?还是别的?

请注意,这是我的问题的最终完全修改版本。自从我第一次发布这个问题以来,我已经编辑了 4 次,并且由于所有这些编辑,它看​​起来很难看。所以我决定根据我的代码的最新更新版本来修改我的问题。请原谅这个问题的性质以及我无法找出问题所在,因为我是 CodeIgniter 的绝对新手。您的帮助将帮助我了解 CI 并鼓励我继续这段旅程。

最佳答案

在您当前的代码中,无法找到验证。首先在 Controller 中初始化库:

$this->load->library('form_validation');

然后当一切正常时,设置规则等:

if($this->form_validation->run() !== false) {
$res = $this->student_model->insert_student($udata);
}

更多信息:https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

编辑:

由于您有自己的验证,您也可以使用该自定义验证:

$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;
}
}

关于php - 为我的 CodeIgniter 应用程序中的现有数据添加验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26083262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com