I'm creating a registration form using the codeigniter.
I have successfully completed the inserting part and now i'm trying to update my records using same view page, which i had used for inserting the values in to datbase.
However, I am getting errors:
A PHP Error was encountered Severity: Notice
Message: Undefined variable: fn
Filename: views/myform.php
Line Number: 18
Can any one help me in fixing this issue?
I am not able to fetch the values in the form.
Controller:
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();
}
}
model
//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."'");
}
view
<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>
In your view use $fn like:
`$data[0]->$fn`
This is because result['data'] is what your are passing to the view. Your data comes from your model as an array but since your querying by Id you can use [0] in this situation. Assuming your Ids are unique.
If you have a many different records per Id you will have to use a foreach loop to get all of them.
You will have to do this with $fn, $ln etc..
Also you should read this: https://www.codeigniter.com/userguide3/database/queries.html
Your queries are not safe if they are going to be handling user input data.
If you have only one set of data to pass to view, i.e only one set like this:
$result['data']=$this->Form_model->displayrecordsById($id);
You can change array to :
$data=$this->Form_model->displayrecordsById($id);
and pass like this:
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform',$data);
}
Then You will be able to access value directly in view using the $fn name.