私は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);
}
}
?>