我是codeigniter的新人 . 我需要在数据库中插入数据,我在名称Home.php上创建一个控制器,下面是该代码;当我正在运行时,我收到错误消息:未定义属性:Home :: $ Yes.Kindly help拜托,先谢谢
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index()
{
$this->load->database();
$this->load->model('yes');
}
public function savedata()
{
$this->load->view('demo');
if($this->input->post('submit'))
{
$name=$this->input->post('name');
$email=$this->input->post('email');
$content=$this->input->post('content');
$this->Yes->saverecords($name,$email,$content);
echo "Records Saved Successfully";
}
}
}
这里是名称的视图 - demo.php
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data"
id="User">
<h2 align="center">Student Marks</h2>
<h3>Name:</h3>
<input type="text" name="name" />
<h3>Email ID</h3>
<input type="text" name="email" />
<h3>Content </h3>
<input type="text" name="content" />
<button type="submit" value="submit" name="submit" >Submit</button>
<button type="reset" value="Reset" >Reset</button>
</form>
</body>
</html>
这是Yes.php的模型页面
<?php
class Yes extends CI_Models
{
function saverecords($name,$email,$content)
{
$query="insert into news values('$name','$email','$mobile')";
$this->db->query($query);
}
}
2 回答
首先,您必须在控制器中加载模型 .
In Model
Hope this will help you
在控制器加载
database
和model
这样的__construct
中,您只是将其添加到index
方法Second
在
savedata()
方法中$this->load->view('demo');
是您的表单然后表单url是好的但如果不是表单操作应重定向到savedata
方法,因为您正在访问其中的post值第三,你的模型应该是这样的: