PHP Codeigniter Insert does not insert



我是PHP的新手,正在学习将数据插入数据库的教程。语法运行良好,只是在表单中键入数据不会将任何数据插入数据库。我已经创建了这个表,并检查了insert查询在MySQL中是否正常工作。

控制器:studCredController

class studCredController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
//load Model
$this->load->model('studCredModeller');
}
//Register students
public function registerStud() {
//load registration view form
$this->load->view('Home');
//Check the submit button
if($this->input->post('save'))
{
$username = $this -> input -> post('username');
$email = $this -> input -> post('email');
$password = $this -> input -> post('password');
$admin_no = $this -> input -> post('adminNo');
$phone = $this -> input -> post('phone');
//call method of studCredModeller and pass variable as parameter
$this -> studCredModeller -> saveRecords($username, $email, $password, $admin_no, $phone);
echo "Records Saved Successfully";
}
}
}

型号:studCredModeller

class studCredModeller extends CI_Model
{
public function saveRecords($username, $email, $password, $adminNo, $phone);
{
$query="insert into locka.stud_login values('$username','$email','$password','$admin_no','$phone', ' ')";
$this->db->query($query);
}
}

查看:主页

<form method="post">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Username</label>   
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Admin No</label>
<input type="text" class="form-control" name="adminNo" placeholder="AdminNo">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Phone number</label>
<input type="number" class="form-control" name="phone" placeholder="Phone">
</div>
</div>
</div>
<input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">
<p class="lead-footer">* We'll contact you by phone & email later</p>
</form>

感谢您查看代码。几天来,我一直在尝试不同的教程。

如果使用Query Builder,尤其是insert((方法,您会发现这要容易得多。

修改控制器对提交作出反应的部分

//Check the submit button
if($this->input->post('save'))
{
$data['username'] = $this->input->post('username');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
$data['admin_no'] = $this->input->post('adminNo');
$data['phone'] = $this->input->post('phone');
//call method of studCredModeller and pass variable as parameter
$result = $this->studCredModeller->saveRecords($data);
echo $result === TRUE ? "Records Saved Successfully" : "Records did not save";
}

修改模型方法

public function saveRecords($data);
{
//returns true if insert works, otherwise false
return $this->db->insert('stud_login', $data);
}

您的插入语句不正确。请参阅:https://www.w3schools.com/sql/sql_insert.asp

您缺少列名

除非:

如果要为表的所有列添加值,则不需要需要在SQL查询中指定列名但是,请确保值的顺序与中的列的顺序相同表

如果您正在指定所有列,并且它们是按顺序排列的,我建议打开database.php中的db_debug以查看发生了什么。

尽管我甚至不会麻烦自己进行查询,我会使用查询生成器,它会自动转义输入数据:

$data = array(
'username' => $username,
...
);
$this->db->insert('tablename', $data);

出于显而易见的原因,不要存储明文密码或使用md5或sha1对密码进行散列。还有其他更好的密码哈希算法。

相关内容

  • 没有找到相关文章

最新更新