• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQL Problem

#1
[eluser]Isuru[/eluser]
I am creating this registration system and when I submit data, and view data on Mysql table I get 0 in the email field.

Here is codes.

Model
Code:
<?php

class User_model extends CI_Model{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function register($username,$name, $email, $password){
        $sha1_password = sha1($password);
        //$query_str = 'INSERT INTO users(username, password, email, name) VALUES(?,?,?,?)';
        $data = array(
            'username' => $username,
            'name' => $name,
            'email' => $email,
            'password' => $sha1_password

        );
        //$this->db->query($query_str, array($username, $sha1_password, $name, $email));
        $this->db->insert('users', $data);
    }

}

Controller
Code:
<?php

class User extends CI_Controller{

    function index(){
        echo "Hello It's Working!";
    }

    public function __construct(){
        parent::__construct();
        $this->view_data['base_url'] = base_url();
        $this->load->model('User_model');
    }

    function register(){

        $this->form_validation->set_rules('username', 'Username');
        $this->form_validation->set_rules('name', 'Name');
        $this->form_validation->set_rules('email', 'E-mail');
        $this->form_validation->set_rules('password');
        $this->form_validation->set_rules('password_conf', 'Password Confirm');


        if($this->form_validation->run() == FALSE){
            //hasn't been run or there are validation errors
            $this->load->view('register', $this->view_data);
        }else{
            //Everything is great!
            $username = $this->input->post('username');
            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $password = $this->input->post('password');



            
            $this->User_model->register($username,$name, $email, $password);
        }
    }

}

View

Code:
<html>
<head><title>Registration Form</title>
<style type="text/css">
    li{list-style:none;}
</style>
</head>

<head>
    <h1>User Registration</h1>
    <p>Please fill in the details below</p>

    &lt;?php
    echo form_open($base_url, '/index.php/user/register');

    $username = array(
        'name' => 'username',
        'id' => 'username',
        'value' => ''
    );

    $name = array(
        'name' => 'name',
        'id' => 'name',
        'value' => ''
    );

    $email = array(
        'email' => 'email',
        'id' => 'id',
        'value' => ''
    );

    $password = array(
        'password' => 'password',
        'id' => 'id',
        'value' => ''
    );

    $password_conf = array(
        'name' => 'password_conf',
        'id' => 'password_conf',
        'value' => ''
    );

    



    ?&gt;


    <ul>
        <li><label>Username</label><div>&lt;?php echo form_input($username); ?&gt;</div></li>
        <li><label>Name</label><div>&lt;?php echo form_input($name); ?&gt;</div></li>
        <li><label>E-mail</label><div>&lt;?php echo form_input($email); ?&gt;</div></li>
        <li><label>Password</label><div>&lt;?php echo form_password($password); ?&gt;</div></li>
        <li><label>Confirm Password</label><div>&lt;?php echo form_password($password_conf); ?&gt;</div></li>
        <li><div>&lt;?php echo form_submit(array('name' => 'register'), 'Register'); ?&gt;</div></li>
        <li><div>&lt;?php echo validation_errors(); ?&gt;</div></li>
    </ul>



    &lt;?php echo form_close(); ?&gt;
&lt;/head&gt;
&lt;/html&gt;

#2
[eluser]InsiteFX[/eluser]
For one this is wrong! Controller/Method
Code:
form_open($base_url, '/index.php/user/register');

// should be:
form_open('user/register');

Then these are also wrong!
Code:
$email = array(
        'email' => 'email',
        'id' => 'id',
        'value' => ''
    );

    $password = array(
        'password' => 'password',
        'id' => 'id',
        'value' => ''
    );

// should be:
    $email = array(
        'email' => 'email',
        'id' => 'email',
        'value' => ''
    );

    $password = array(
        'password' => 'password',
        'id' => 'password',
        'value' => ''
    );

InsiteFX

#3
[eluser]davidbehler[/eluser]
@Inside: I think what you mean is this:
Code:
// should be:
    $email = array(
        'name' => 'email',
        'id' => 'email',
        'value' => ''
    );

    $password = array(
        'name' => 'password',
        'id' => 'password',
        'value' => ''
    );

About your initial problem:
Due to your error in your html which results in no input with the name "email", the following will always return FALSE:
Code:
$this->input->post('email');
If the requested field (in this case "email") is not set, then the function returns FALSE. That means your variable $email will always be FALSE and FALSE is converted to 0 when trying set as a value for insert/update or in general when used as a string.

You might wanna add some "required" rules to your form validation to make sure all the important fields are actually present and set!

#4
[eluser]InsiteFX[/eluser]
Yep!

Also he can use.
Code:
var_dump($data);
To see what values he is getting!

InsiteFX


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.