Welcome Guest, Not a member yet? Register   Sign In
Active Record "insert" is not working properly
#1

[eluser]Daksh Mehta[/eluser]
Hello friends,

I am new to CI and developing my first application.
I am using active record "insert" to insert a member into database from registration page, but its not working properly.

The problem with active record is that , whenever i am using the insert(), its adding one extra record in it with values 0 for all fields.

Here is my controller:
Code:
<?php
class Register extends CI_Controller {
    public static $step;
    public $data;
    public function __construct(){
        parent::__construct();    
        $this->step = 1;

    }
    public function index(){
                $this->data['name_username'] = "username";
        $this->data['name_password'] = "password";
        $this->data['name_repassword'] = "password2";
        $this->data['name_email'] = "email";
        $this->data['name_submitBtn'] = "regBtn";
        $this->data['title'] = "Register";
        $this->template->render($this->data);
    }
    public function process(){
        $this->load->helper('security');
        $username = $this->input->post("username");
        $passwd = do_hash($this->input->post("password"));
        $email = $this->input->post("email");    
        $sql = array(
        'username' => $username,
        'password' => $passwd,
        'email' => $email
        );
        $this->db->insert('members', $sql)

        $this->data['title'] = "Registration done!";
        $this->data['msg'] = "A private key of your account has been sent to your email address. Please, enter it in below form to confirm your account.";
            $this->template->render($this->data);
    }
}
?>
So,
the table will look like:
ID username password email
1 dax abc [email protected]
2 0 0 0

where, the record with ID 2 is automatically added without execution any kind of query.

My view is located in /views/default/register/process.php
Code:
{header}
<h1>{title}</h1>
<p>
{msg}
</p>
{footer}

Please, let me know why its happening with solution.
#2

[eluser]waynhall[/eluser]
I'd recommending using a model, not a controller, to perform database inserts. But I'm puzzled as to why you're getting the extra record.
#3

[eluser]waynhall[/eluser]
I would try running a test view outside of the template parser. Create a new view that doesn't use the template. Just use &lt;?php ?&gt; tags, to determine if the template has something to do with this.
#4

[eluser]InsiteFX[/eluser]
Maybe you should do a echo var_dump to see what is being put
into your $sql array

InsiteFX
#5

[eluser]Daksh Mehta[/eluser]
Hello,

thanks for your help but its still not working, i can't understand why its not working.

here is my new process() function:
Code:
public function process(){
        $this->load->helper('security');
        $username = $this->input->post("username");
        $passwd = do_hash($this->input->post("password"));
        $email = $this->input->post("email");    
        $this->load->model('members');
        $this->members->register($username, $passwd, $email);  
        $this->data['title'] = "Registration done!";
        $this->data['msg'] = "A private key of your account has been sent to your email address. Please, enter it in below form to confirm your account.";
            $this->template->render($this->data);
    }

and here is my members.php model used in the above function:
Code:
&lt;?php
class Members extends CI_Model {
    public function register($username, $password, $email){
        $sqlq = "INSERT INTO members VALUES(?, ?, ?, ?)";
        $this->db->query($sqlq, array(NULL, $username, $password, $email));    
    }
}
?&gt;

Its still inserting 2 rows..
I am not understanding why its not working. Actually, i have created one library for retrieving and saving setting of portals into the database, which is as follow:
Code:
&lt;?php
class Setting {
    private $CI, $value;
    public function __construct(){
        $this->CI = & get_instance();
        $this->CI->load->database();
    }
    public function add($option, $value){
            $data = $this->get(array($option));
            if(isset($data[$option])){
                    $this->update($option, $value);
            }
            else {
                $sql = "INSERT INTO config VALUES(?, ?)";
                $this->CI->db->query($sql, array($option, $value));
            }
    }
    public function update($option, $value){
        $sql = "UPDATE config SET value=? WHERE ukey=?";
        $this->CI->db->query($sql, array($value, $option));    
    }
    public function _get($option = ""){
        $sql = "SELECT * FROM config WHERE ukey = ?";
        $result = $this->CI->db->query($sql, array($option));    
        $this->value = $result->row_array();
        return $this->value['value'];        
    }
    public function get($options = array(), $parse = FALSE){
        $data = array();
        $i = 0;
        foreach($options as $option){
            if($parse == FALSE)
                $data[$option] = $this->_get($option);
            else
                $data[$i++][$option] = $this->_get($option);    
        }
        return $data;
    }
}
?&gt;

When we are accessing the add() function, its working fine and inserting only one row.
Please, if possible experiment it practically and let me know what you say about it.

Again thanks to all, looking forward for help!
#6

[eluser]waynhall[/eluser]
Is it possible you are using javascript that is broken on the client side sending the null values via ajax just before the page submits?
#7

[eluser]Daksh Mehta[/eluser]
yes, i am using Ajax to validate username if its available or not. that's it .
And i am beginner, so i can judge it.
Here is my js file:
Code:
user_validated = 0;
passwd_validated = 0;
function user_available(){
    $.ajax({
          type: "POST",
         url: "http://localhost:8888/ptcan/index.php/ajax/username_available/"+$("#username").val(),
          success: function(msg){
            if(msg == 1){
                $("#username_status").html("<font class=\"succ\">"+$("#username").val()+" is available!</font>");
                user_validated = 1;
                 reg_validate();
            }
            else {
                $("#username_status").html("<font class=\"err\">"+$("#username").val()+" is not available, try again!</font>");
                user_validated = 0;    
                 reg_validate();
            }
        }
     })
    
}
function pass_check(){
    if(($("#password").val() != $("#password2").val())){
                $("#password_error").html("<span class=\"err\">Password do not matches!</span>");
                passwd_validated = 0;
    }
    else {
        $("#password_error").html("<span class=\"succ\">Password matched!</span>");
        passwd_validated = 1;
    
    }
    reg_validate();
}
function reg_validate(){
    if((user_validated == 1) && (passwd_validated == 1))
        $("#regBtn").css("display", "block");
    else
        $("#regBtn").css("display", "none");
}


Here is my ajax controller:
Code:
&lt;?php
class Ajax extends CI_Controller {
    public function __construct(){
        parent::__construct();    
    }
    public function username_available($username = ""){
        if($username != ""){
            $q = $this->db->query("SELECT username FROM members WHERE username = ?", array($username));
            if($q->num_rows() == 0){
                echo "1";
            }
            else {
                echo "0";
            }
        }
        else {
            echo "<span class=\"err\">Username is required!</span>";    
        }
    }
}
?&gt;
#8

[eluser]waynhall[/eluser]
First of all, use the Web Developer Toolbar to disable javascript on the page.

Does that fix the double insert problem?
#9

[eluser]appleboy[/eluser]
double insert problem?

Please referrer it

http://ellislab.com/forums/viewthread/195128/
#10

[eluser]Daksh Mehta[/eluser]
[quote author="appleboy" date="1312378191"]double insert problem?

Please referrer it

http://ellislab.com/forums/viewthread/195128/[/quote]

Thanks. But its still not helping me..


Also, i have checked with Opera and Chrome, where its working perfectly without any problem. and inserting only one row.

I have already tried to use return TRUE; in my model and disabled js on browser and even disabled all addon.

still not working.

hope i can resolve it as soon as possible..




Theme © iAndrew 2016 - Forum software by © MyBB