数据库中的值未插入codeIgniter?
Posted
技术标签:
【中文标题】数据库中的值未插入codeIgniter?【英文标题】:values in database not inserted in codeIgniter? 【发布时间】:2015-10-07 18:24:54 【问题描述】:我是 php 框架的新手,我创建了一个简单的表单,想将它的值插入到数据库中,但没有这样做,不知道我错在哪里!!!
每次我得到 mysql 返回一个空结果集(即零行)。 我正在使用 CodeIgniter 3 和 mysqli 驱动程序这是我的控制器
<?php
//display_errors(on);
error_reporting(E_ALL);
class Register extends CI_Controller
public function index ()
//Validations
$rules=array (
"username"=> array(
"field"=>"username",
"label"=>"Username",
"rules"=>"required|max_length[20]|min_length[5]|callback_username_is_taken"
),
"password"=> array(
"field"=>"password",
"label"=>"Password",
"rules"=>"required|max_length[20]|min_length[6]"
),
"pass_conf"=> array(
"field"=>"pass_conf",
"label"=>"Password",
"rules"=>"required|matches[password]"
),
"email"=> array(
"field"=>"email",
"label"=>"Email",
"rules"=>"required|valid_email|callback_is_taken"
)
);
//set the rules
$this->form_validation->set_rules($rules);
//Override the message
$this->form_validation->set_message('required','The %s field is empty');
// check to see if form has been submitted
if ($this->form_validation->run()!=true)
$this->load->helper('url');
$this->load->view('register'); //Display page
$this->load->view('footer');
else
echo "Donkey Butt";
$form=array();
$form['username']=$this->input->post("username");
$form['password']=md5($this->input->post("password"));
$form['email']=$this->input->post("email");
if(self::createUser($form['username'],$form['password'],$form['email'])==true)
//Created User Successfully
echo "Success";
//$this->load->view("success",$data);
else
echo "problem in subbmitting the form";
public function username_is_taken($input)
$query="SELECT * FROM `tbl_usrs` WHERE `username`=? ";
$arg=array ($input);
$exec=$this->db->query($query,$arg) or die(mysqli_error());
echo $query;
if($exec->num_rows() >0)
$this->form_validation->set_message('username_is_taken','Sorry the UserName <b> $input </b> is already taken');
return false;
else
return true;
public function email_is_taken($input)
$query="SELECT * FROM `tbl_usrs` WHERE `email`=? ";
$arg=array ($input);
$exec=$this->db->query($query,$arg) or die(mysqli_error());
echo $query;
if($exec->num_rows() >0)
$this->form_validation->set_message('email_is_taken','Sorry the email <b> $input </b> is already taken');
return false;
else
return true;
public function createUser($user,$pass,$email)
$query="
INSERT INTO `tbl_usrs` (`username`,`password`,`email`,`ip`)
VALUES (?,?,?,?)
";
$arg=array (self::protect($user),self::protect($password),$email,$_SERVER['REMOTE_ADDR']);
if($this->db->query($query,$arg)==true)
echo $query;
return true; // if added to database
else
return false;
public function protect($str)
return mysqli_real_escape_string($str);
?>
【问题讨论】:
请给一些调试代码的参考 这是我得到的错误 错误号:1048 列“用户名”不能为空 INSERT INTOtbl_usrs
(username
,password
,email
,ip
) 值 ( NULL,NULL,'anmol@gmail.com','::1') 文件名:C:/xampp/htdocs/koopanda/application/controllers/Register.php 行号:132
你必须有其他错误......我可以清楚地看到mysqli_real_escape_string只有参数你应该把mysqli_链接作为第一个参数
非常感谢,我没有注意到这一点,也没有认真对待
【参考方案1】:
修复 mysqli_real_escape_string($str); //两个参数除外,只有一个给定 修复是让你的代码工作
【讨论】:
以上是关于数据库中的值未插入codeIgniter?的主要内容,如果未能解决你的问题,请参考以下文章
使用Codeigniter将jQuery datepicker + timepicker中的值插入MySQL datetime数据类型?