Welcome Guest, Not a member yet? Register   Sign In
Insert help needed!
#1

[eluser]debow[/eluser]
Hello all, I'm really new to CI and I'm trying to take my old insert statement which was working in reg php and convert it over to CI format and not having much luck. Below is the existing sql.

I have this from my old project. It works. I have the first part of inserting a new athlete working. I'm just having issue with the 2nd sql statement trying to insert info from 2 tables into one.
Code:
$today = date('Y');
    $sql = "INSERT INTO athlete SET firstname='$firstname', lastname='$lastname', gender='$gender'";
    $result = mysql_query($sql);
    
    $last_id = mysql_insert_id();
            
    $sql = "INSERT INTO event_results (athlete_id,gender,event,date) SELECT $last_id,athlete.gender,events.event,$today
    FROM athlete,events
    WHERE athlete.id = $last_id";
    
    $result = mysql_query($sql);
    
        if ($result)
        {    
            return true;
        } else
        {
            return false;
        }
   }


This is the controller section of CI code I have that is working.
Code:
function add()
    {        
        // Validate form
        $this->form_validation->set_rules('firstname', 'firstname', 'trim|required');
        $this->form_validation->set_rules('lastname', 'lastname', 'trim|required');
        $this->form_validation->set_rules('gender', 'type', 'trim|required');
        
        if($this->form_validation->run())
        {
            // Validation passes
            $athId = $this->athlete_model->AddAthlete($_POST);
            
            if($athId)
            {
                $this->session->set_flashdata('flashConfirm', 'The athlete has been successfully added.');
                redirect('athletes');
            }
            else
            {
                $this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
                redirect('athletes');
            }
        }
            $data['main_content'] = 'athletes/athletes_add_form';
            $this->load->view('includes/template', $data);
        
    }

This is the model section of CI code.

Code:
function AddAthlete($options = array())
    {
        // required values
        if(!$this->_required(
            array('firstname', 'lastname', 'gender'),
            $options)
        ) return false;
        
        $options = $this->_default(array('athStatus' => 'active'), $options); //declares all athletes active by default
        
        $this->db->insert('athlete', $options);
        
        return $this->db->insert_id(); //This will return the id the athlete was given
        
    }

At this point I'm having issues trying to get the second half of the existing code working in CI. I need help converting this to CI code.
Code:
$today = date('Y');
$last_id = mysql_insert_id();
            
    $sql = "INSERT INTO event_results (athlete_id,gender,event,date) SELECT $last_id,athlete.gender,events.event,$today
    FROM athlete,events
    WHERE athlete.id = $last_id";
    
    $result = mysql_query($sql);
    
        if ($result)
        {    
            return true;
        } else
        {
            return false;
        }

Thanks for any help!


Messages In This Thread
Insert help needed! - by El Forum - 09-28-2010, 11:56 AM
Insert help needed! - by El Forum - 09-28-2010, 01:38 PM
Insert help needed! - by El Forum - 09-28-2010, 01:46 PM
Insert help needed! - by El Forum - 09-28-2010, 02:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB