Welcome Guest, Not a member yet? Register   Sign In
Weird problem trying to create/update a table with Model function
#1

[eluser]BoyBlue[/eluser]
I've been trying over the last couple days to get the following code to work but with no luck...I've now reached a point where I just need a fresh set of eyes because it's probably me just missing a period or colon or something.

Controller:
Code:
function confirm_email( $password )
    {
        if (  $password )
        {
            $password = $this->uri->segment(3, 0);
            $temp_password = $this->uri->segment(4, 0);
            $table_name = $this->uri->segment(5, 0);
            $email = $this->uri->segment(6, 0);
            
            //Replace Temp Email and Temp_password with 0 and put Password into cell
            $new_member_data = array(

            /*Place Email Into Email cell*/
            'email' => $email,

            /*Place Password Into Password cell*/
            'password' => $password,

            /*Turn temp_email to 0*/
            'temp_email' => 0,

            /*Turn confirm_password to 0*/
            'confirm_password' => 0,

            /*Turn temp_password to 0*/
            'temp_password' => 0

            );

            //Update the Database by placing password into position and changing Temp Password and Confirm Password
            $this->load->model('model_members');
            $this->model_members->update_member($new_member_data);
            
            //Create Member Table with table_name stored in Member table on specific members row
            $this->model_members->create_member_table($table_name);

            //Get the Game data from Game Table
            /*$game_data['new_table_game_data'] =*/
                        $this->model_members->get_game_data($table_name);

                
            /********Now Load the Page***************/
            $this->create_member_success_page();
        }
    }

Model:
Code:
function update_member($new_member_data)
    {
        //Test to see if there is a match with passwords
        $this->db->select('confirm_password');//Table item(s). If all then remove this line
        $this->db->where('temp_password', $this->uri->segment(4));
        
        $query = $this->db->get('members');//name of table in database

        //If password match then update the DB
        if ($query->num_rows > 0)
        {
            //Update in Database
THIS SEEMS TO BE LINE WHERE THE DATA IS BEING PULLED FROM TO CREATE ERROR
            $this->db->select( 'temp_email, temp_password, confirm_password, password' );//Table item(s). If all then remove this line

            $this->db->where('temp_password', $this->uri->segment(4));//This is the element that loads individual rows to be displayed in the the view file
    
            $this->db->update('members', $new_member_data);//users is name of table in database
            
            
        }

    }
    
    function create_member_table($table_name)
    {
        //Use CI's Database Forge to create new table
        $this->load->dbforge();
        
        // Set up Data for inserting into new table
            // gives PRIMARY KEY to `whatever_id` (`whatever_id`)
        $this->dbforge->add_key('member_rating_id', TRUE);

        $fields = array
        (
            'member_rating_id' => array(
                                     'type' => 'INT',
                                     'constraint' => 11,
                                     'unsigned' => TRUE,
                                     'auto_increment' => TRUE
                              ),

            'game_id' => array(
                                     'type' => 'INT',
                                     'constraint' => '10',
                              ),

        );        
                

        
        //Uses the Newly Generated Table Name to create new Table
        //first load the fields
        $this->dbforge->add_field($fields);
        //Then create the table and use the Unique Member Table ID variable as the name
        // 2nd variable 'TRUE' gives CREATE TABLE IF NOT EXISTS table_name
        $this->dbforge->create_table( $table_name, TRUE);

    }


    function get_game_data($table_name)
    {
        $this->db->select('game_id, base_number');//Table item(s). If all then remove this line
        
THIS IS LINE 153
       $game_data = $this->db->get('games');//name of table in database

        if ($game_data->num_rows > 0)
        {

        $this->db->select('game_id, base_number');//Table item(s). If all then remove this line
        
        $this->db->insert($table_name, $game_data);//name of table in database

        }

    }

Error Message:
Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'temp_email' in 'field list'

SELECT `temp_email`, `temp_password`, `confirm_password`, `password`, `game_id`, `base_number` FROM (`games`)

Filename: /Applications/MAMP/htdocs/beta_new/models/model_members.php

Line Number: 153
#2

[eluser]InsiteFX[/eluser]
‘temp_email’ show your database table

InsiteFX
#3

[eluser]BoyBlue[/eluser]
Hello InsiteFX. Thanks so much for responding. I've seen you all over these boards since starting with CI and feel very lucky to have gotten a response from you--I know that you really know your stuff.

Here is the code for the '$table_name' Table I'm trying to create(I deleted some of it for brevity, but realize I deleted one line of relevant code for troubleshooting...sorry)

From MODEL
Code:
function create_member_table($table_name)
    {
        //Use CI's Database Forge to create new table
        $this->load->dbforge();
        
        // Set up Data for inserting into new table
            // gives PRIMARY KEY to `whatever_id` (`whatever_id`)
        $this->dbforge->add_key('member_rating_id', TRUE);

        $fields = array
        (
            'member_rating_id' => array(
                                     'type' => 'INT',
                                     'constraint' => 11,
                                     'unsigned' => TRUE,
                                     'auto_increment' => TRUE
                              ),

            'game_id' => array(
                                     'type' => 'INT',
                                     'constraint' => '10',
                              ),

            'rated' => array(
                                     'type' => 'INT',
                                     'constraint' => '10',
                              ),

            'base_number' => array(
                                     'type' => 'INT',
                                     'constraint' => '10',
                              ),

        );        
                

        
        //Uses the Newly Generated Table Name to create new Table
        //first load the fields
        $this->dbforge->add_field($fields);
        //Then create the table and use the Unique Member Table ID variable as the name
        // 2nd variable 'TRUE' gives CREATE TABLE IF NOT EXISTS table_name
        $this->dbforge->create_table( $table_name, TRUE);

    }

Any errors you can see?
#4

[eluser]BoyBlue[/eluser]
Also, because I'm trying to create the table in this function, I don't have the code from the actual table(sql). I could create it manually and then give you the code if that would be helpful...

Let me know and thanks so much for your time.
#5

[eluser]micflan[/eluser]
It doesn't look like you're creating the temp_email column. Does that column exist in the games table? The error suggests that it doesn't.
#6

[eluser]BoyBlue[/eluser]
Thanks for posting Micflan...

Your post helped me realize that I made a mistake in my response to SiteFX. The "Game" Table is actually the "$table_name" Table.

But, you are correct in that the "$table_name" table does not have a field in it called "temp_email" or "temp_password" etc. And, if you notice with the model, after I've updated the "members" table with the info for "temp_email" and "temp_password" then the next function actually creates the "$table_name" table successfully(with the fields of "member_rating_id" and "rated" etc) all there.

The problem is with the next part(function get_game_data($table_name)) where I then try to grab only 2 columns of info from the "games" table("game_id" and "base_number")...But, for some reason the function is adding additional fields that it's trying to get("temp_email" "temp_password" etc...that is being called in the previous function where I'm inserting(successfully) those fields into the table.

So, what I can't figure out is why the info from the $new_member_data array fields are "bleeding" over into the next function where I'm trying to grab info in a completely separate function that doesn't share any variables with the previous function...

This has really got me perplexed and I'm sure it's something really simple I'm missing...

Please Help if you can.
#7

[eluser]InsiteFX[/eluser]
Check your Model this line.
Code:
$this->db->where('temp_password', $this->uri->segment(4));

echo $this->uri->segment(4));
See if you are getting your uri segments?

InsiteFX
#8

[eluser]BoyBlue[/eluser]
Ok, so I double checked and I'm definitely getting the URI segments. In fact, the entire time I've been working on this code, the function "update_member" is working great--all that stuff is put into the proper place.
Also, the function "create_member_table" is also working great--Even with getting the error page, when I go check the database there is a table that has been created exactly as I've coded it to do...

So, the problem comes when the code gets to "function get_game_data" and tries to read the "games" table info in preparation to insert it into the newly created member table. For some reason, even though just before line 153, I'm only specifying for the function to get only the "game_id" and "base_number", it is also adding into that trying to read all the items called for in the "SELECT statement" in the function "update_member" ("temp_email", "temp_password" "confirm_password", "password")--In fact, something I noticed is that my Line 153 Error includes all of these items plus the 2 other items I'm requesting in the SELECT statement just above line 153--I made sure this was the case by deleting the "password" in the SELECT statement in the "update_memeber" function and sure enough, the Error Page didn't include the "password" field but did include all the others plus the "game_id" and "base_number"...then when I put "password" back into the SELECT statement in the "update_member" function, then the error page put it back in there...

What I can't wrap my brain around is why the function "get_game_data" is including the SELECT calls from the "update_member" function when I haven't even used any similar variables that would cause this data to persist to another function. I don't get it. I even did troubleshooting where I tried to do the function "get_game_data" on a completely new model page all by itself, and it still tried to grab the "temp_email" and other stuff from the SELECT update_member function call.

This is very frustrating to say the least.

Thanks so much for being willing to help...
#9

[eluser]InsiteFX[/eluser]
This line is wrong! Your missing the () on the end of num_rows...
Code:
if ($query->num_rows > 0)

// should be!
if ($query->num_rows() > 0)

InsiteFX
#10

[eluser]BoyBlue[/eluser]
Ok, so I changed those. Thanks for that.

Everything however acted the exact same way...
The Data was placed into the members table fine.
The New Member Table was created fine.

When trying to check the new member table in the "browse" view I get the same error:
Quote:MySQL returned an empty result set (i.e. zero rows).

Obviously due to the same error as I got before...
Quote:A Database Error Occurred
Error Number: 1054

Unknown column 'temp_email' in 'field list'

SELECT `temp_email`, `temp_password`, `confirm_password`, `password`, `game_id`, `base_number` FROM (`games`)

Filename: /Applications/MAMP/htdocs/beta_new/models/model_members.php

Line Number: 148

Is there perhaps another way for me to approach executing this read/insert code for the function "get_game_data($table_name)"?




Theme © iAndrew 2016 - Forum software by © MyBB