php - 使用 codeigniter 在同一视图页面中编辑和添加
问题描述
我正在使用 codeigniter 创建一个注册表单。
我已经成功完成了插入部分,现在我正在尝试使用相同的视图页面更新我的记录,我曾使用该页面将值插入到数据库中。
但是,我收到错误:
遇到 PHP 错误 严重性:通知
消息:未定义变量:fn
文件名:views/myform.php
行号:18
任何人都可以帮助我解决这个问题吗?
我无法获取表单中的值。
控制器:
public function updatedata($id)
{
//$id=$this->input->get('id');
$result['data']=$this->Form_model->displayrecordsById($id);
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform',$result);
}
else
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
if($this->upload->do_upload('filename'))
{
$fi= $this->upload->data('file_name');
$file = ("uploads/".$result['data'][0]->filename);
//delete the existing file if needed
if (file_exists($file)) {
unlink($file);
}
}
else
{
$fi= $result['data'][0]->filename;
}
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
//exit();
}
}
模型
//get values
function displayrecordsById($id)
{
$query=$this->db->query("select * from form where ID='".$id."'");
return $query->result();
}
//update record
function updaterecords($fn,$ln,$un,$em,$fi,$id)
{
$query=$this->db->query("update form SET first_name='$fn',last_name='$ln',username='$un',email='$em',filename='$fi' where ID='".$id."'");
}
看法
<body>
<?php echo form_open_multipart('form'); ?>
<div class="container">
<h2 style="color:#ff1a1a">Registration Form</h2><br>
<h5 style="color:#ff1a1a">First Name</h5>
<input type="text" class="form-control" name="fname" value="<?php echo set_value("first_name", $fn); ?>" size="50">
<span class="text-danger"><?php echo form_error("fname");?></span>
<h5 style="color:#ff1a1a">Last Name</h5>
<input type="text" class="form-control" name="lname" value="<?php echo set_value("last_name", $ln); ?>" size="50">
<span class="text-danger"><?php echo form_error("lname");?></span>
<h5 style="color:#ff1a1a">Username</h5>
<input type="text" class="form-control" name="username" value="<?php echo set_value("username", $un); ?>" size="50">
<span class="text-danger"><?php echo form_error("username");?></span>
<h5 style="color:#ff1a1a">Email Address</h5>
<input type="text" class="form-control" name="email" value="<?php echo set_value("email", $em); ?>" size="50">
<span class="text-danger"><?php echo form_error("email");?></span>
<h5 style="color:#ff1a1a">Password</h5>
<input type="password" class="form-control" name="password" value="" size="50">
<span class="text-danger"><?php echo form_error("password");?></span>
<h5 style="color:#ff1a1a">Password Confirm</h5>
<input type="password" class="form-control" name="passconf" value="" size="50">
<span class="text-danger"><?php echo form_error("passconf");?></span><br>
<h5 style="color:#ff1a1a">Upload image</h5>
<input type="file" name="filename" size="20">
<img src="<?php echo base_url('uploads/' . $row->filename);?>" class="img-responsive" alt="image" width="100px" height="100px"><br><input type="file" name="filename" value="">
<span class="text-danger"><?php echo form_error("filename");?></span><br>
<div><input type="submit" value="Submit" class="btn btn-success"></div>
</div>
</form>
</body>
解决方案
在您看来,使用 $fn 之类的:
`$data[0]->$fn`
这是因为 result[' data '] 是您传递给视图的内容。您的数据作为数组来自您的模型,但由于您通过 Id 查询,因此您可以在这种情况下使用 [0]。假设您的 ID 是唯一的。
如果每个 ID 有许多不同的记录,则必须使用 foreach 循环来获取所有记录。
您必须使用 $fn、$ln 等来执行此操作。
您还应该阅读以下内容:https ://www.codeigniter.com/userguide3/database/queries.html
如果要处理用户输入数据,您的查询是不安全的。
推荐阅读
- android - 没有可用的缓冲区空间(达到最大连接数?):绑定
- python-3.x - 在 Python 3 中使用 For If 语句删除字典键
- java - 在java中如何捕获从子线程抛出的异常,然后从父线程再次抛出它
- sdl - 应用程序中的插值导致精灵抖动
- javascript - (Angular) 在 Bootstrap 模式中访问 Typescript 对象
- php - PHP regexp 如何在 preg_match 中获取所有匹配项
- java - java8按自定义值分组自定义地图值
- angular5 - PrimeNg TurboTable colgroup 动态 colspan
- css - 如何创建完整的背景图像
- vba - VBA 脚本导致 Excel 在 15 次循环后没有响应