我是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);
}
}
?>
答案 0 :(得分:0)
修复mysqli_real_escape_string($ str); //除了一个给定的两个参数 修复是让你的代码工作