ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

CodeIgniter using MySQL

April 09, 2011 8:43am

Subscribe [4]
  • #16 / Apr 11, 2011 8:10am

    MaxEisley

    21 posts

    Full DB is bigger but there is no more used adresa ID so it isn’t important to show it. I am from Czech Republic and everything have to translate to Eng 😊

  • #17 / Apr 11, 2011 8:13am

    mihaibaboi

    39 posts

    I see. There’s no point i showing the whole DB if it’s not relevant. I’ll post a working example as soon as I can, and you can compare to what you have to figure out what’s wrong.

  • #18 / Apr 11, 2011 8:37am

    toopay

    1583 posts

    There something wrong with your database! Try run this SQL query in phpmyadmin or mysql daemon, to correct the foreign key relationship :

    ALTER TABLE `customer` ADD CONSTRAINT `FK_customer_address` FOREIGN KEY (`addressID`) REFERENCES `address` (`addressID`);

    For more information, look here

  • #19 / Apr 11, 2011 9:46am

    MaxEisley

    21 posts

    Ok i deleted FK keys cause i think its done badly.

    Now i have another problem with last_insert_id();

    This is my data_model:

    function createaddress ($town, $street, $CP, $PSC) {
    $data = array (
    'Mesto' => $town,
    'Ulice' => $street,
    'CP' => $CP,
    'PSC' => $PSC
    );

    $this->db->insert('adresa', $data);
    }
     
      function createcustomer ($phone, $email, $name, $surname) {
    $data = array (
    'Telefon' => $phone,
    'Email' => $email,
    'Jmeno' => $name,
    'Prijmeni' => $surname,
    'adresaID' => 'last_insert_id()'
    );

    $this->db->insert('zakaznik', $data);
    }

    And this is controller:
    $this->data_model->createaddress($this->input->post(‘Town’), $this->input->post(‘Street’), $this->input->post(‘StreetN’), $this->input->post(‘ZIP’));

    $this->data_model->createcustomer($this->input->post(‘Phone’), $this->input->post(‘Email’), $this->input->post(‘FirstN’), $this->input->post(‘SecondN’));

    In one step (function, send form) i want to add all info about customer, address is in another table then customer corresponding with FK (which is not set but I know about that).

    last_insert_id() doesnt work because adresaID (FK) has 0 and not ID address from table address.

    Where is problem or how can i do that better ?

  • #20 / Apr 11, 2011 9:54am

    InsiteFX

    6819 posts

    See if this will work for you.

    function createaddress ($town, $street, $CP, $PSC) {
        $data = array (
        'Mesto' => $town,
        'Ulice' => $street,
        'CP'    => $CP,
        'PSC'   => $PSC
        );
        
        $this->db->insert('adresa', $data);
        return $this-db->last_insert_id();
      }
     
    // assign the return value from above to a string variable then pass it to the method below!
      function createcustomer ($phone, $email, $name, $surname $last_insert_id) {
        $data = array (
        'Telefon'  => $phone,
        'Email'    => $email,
        'Jmeno'    => $name,
        'Prijmeni' => $surname,
        'adresaID' => $last_insert_id
        );
      
        $this->db->insert('zakaznik', $data);
       }

    and please use code tags when posting code!

    // remove spaces at end!
    [code ]
    [/code ]

    InsiteFX

  • #21 / Apr 11, 2011 10:17am

    toopay

    1583 posts

    @MaxEisley

    Is everything ok, when you had FK? Then, you should FIX IT first (not deleted it!).

  • #22 / Apr 11, 2011 10:39am

    MaxEisley

    21 posts

    InsiteFX: I get this error.

    Fatal error: Call to undefined method CI_DB_mysql_driver::last_insert_id() in C:\xampplite\htdocs\www\DBS2\application\models\data_model.php on line 26

  • #23 / Apr 11, 2011 10:41am

    MaxEisley

    21 posts

    @toopay: I will not use FK cause when i insert 1 row, all tables are bad and i cannot delete nothing, i can only delete full database.

    Now i am working with FK, its small DB and i know what is FK and i dont need any mysql features for this.

    I have problem written above, cannot get last_insert_id();

  • #24 / Apr 11, 2011 11:06am

    toopay

    1583 posts

    function createaddress ($town, $street, $CP, $PSC) 
    {
        $data = array (
           'Mesto' => $town,
           'Ulice' => $street,
           'CP'    => $CP,
           'PSC'   => $PSC
        );
        $this->db->insert('adresa', $data);  
        return $this->db->insert_id();
    }
      
    function createcustomer ($phone, $email, $name, $surname, $address_id) 
    {
        $data = array (
           'Telefon'  => $phone,
           'Email'    => $email,
           'Jmeno'    => $name,
           'Prijmeni' => $surname,
           'adresaID' => $address_id
        );
        $this->db->insert('zakaznik', $data);
    }

    And in controller:

    $address_id = $this->data_model->createaddress($this->input->post('Town'), $this->input->post('Street'), $this->input->post('StreetN'), $this->input->post('ZIP'));
    
    $this->data_model->createcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'),$address_id);
  • #25 / Apr 11, 2011 11:24am

    MaxEisley

    21 posts

    toopay: thanks, it works, i have all ok but i have used bad function mysql_insert_id() and not just insert_id()

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases