我创建了一个可以很好地将数据插入数据库的表格。
我正在尝试更新记录,但是我的代码似乎有点问题。
当我提交更新按钮时,它没有显示任何错误,但是当我在数据库中看到这些值时,它们并没有得到更新。
请问有人可以帮助我解决我的代码中的哪些问题吗?请为我提供帮助。
控制器:
<?php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Form_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Form_model->displayrecordsById($id);
$this->load->view('update_records',$result);
if($this->input->post('update'))
{
$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]|is_unique[form.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');
if ($this->form_validation->run() == TRUE)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if($this->upload->do_upload('filename'))
{
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
$fi = $this->upload->data('file_name');
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
}
}
}
}
}
?>
型号:
<?php
class Form_model extends CI_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."'");
}
}
?>
显示记录:
<!DOCTYPE html>
<html>
<head>
<title>Display Records</title>
</head>
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr style="background:#CCC">
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Username</th>
<th>E-mail</th>
<th>Image</th>
<th>Update</th>
</tr>
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->first_name."</td>";
echo "<td>".$row->last_name."</td>";
echo "<td>".$row->username."</td>";
echo "<td>".$row->email."</td>";
echo '<td><img src="' . base_url('uploads/' . $row->filename) . '" alt="image" width="100px" height="100px"></td>';
echo "<td><a href='updatedata?id=".$row->ID."'>Update</a></td>";
echo "</tr>";
$i++;
}
?>
</table>
</body>
</html>
更新记录:
<!DOCTYPE html>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">First name </td>
<td width="329"><input type="text" name="fname" value="<?php echo $row->first_name; ?>"/></td>
</tr>
<tr>
<td>Last name </td>
<td><input type="text" name="lname" value="<?php echo $row->last_name; ?>"/></td>
</tr>
<tr>
<td>Username </td>
<td><input type="text" name="username" value="<?php echo $row->username; ?>"/></td>
</tr>
<tr>
<td>E-mail </td>
<td><input type="text" name="email" value="<?php echo $row->email; ?>"/></td>
</tr>
<tr>
<td>Image</td>
<td><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=""></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>
答案 0 :(得分:0)
查看
echo "<td><a href='updatedata?id=".$row->ID."'>Update</a></td>";
您要在url中传递值
控制器
public function updatedata($id)
{
echo $id;
}
提示:
如果您使用的是<form method="post"
,则始终以$this->input->post('value');
的身份获取数据,并尝试获取方法get
请注意: 不要将自定义php与codeigniter混合使用,尝试阅读有关codeigniter语法的文档