Welcome Guest, Not a member yet? Register   Sign In
Can't write data into database
#1

[eluser]penta997[/eluser]
Hi. I have a register form. I Have to write data into 3 tables. First table users, 2nd address and 3rd client. When function insert is running i have 2 errors:
A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: mysql/mysql_driver.php

Line Number: 535

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'jakas', 'jakas', '23', '2', '26-315', '2134324')' at line 1

INSERT INTO `address` (`Country_COUNTRY_Id`, `State_STATE_ID`, `USERS_USR_ID`, `ADDR_CityName`, `ADDR_StreetName`, `ADDR_StreetNr`, `ADDR_HomeNr`, `ADDR_ZIP`, `ADDR_Fax`) VALUES ('17', '4', , 'jakas', 'jakas', '23', '2', '26-315', '2134324')

This is my model code
Code:
function create_User()
       {

          

           $USR_ID = $this->db->query("select USR_ID from users where USR_Nick='".$this->input->post('username')."'");
          
        
          
           $ADDR_ID = $this->db->query("select ADDR_ID from address where ADDR_StreetName='".$this->input->post('ulica')."'");
          
          
           $users = array(

               'USR_Nick' => $this->input->post('username'),
               'USR_Pass' => md5($this->input->post('pass')),
               'USR_CreateDate' => date('Y-m-d H:i:s')
           );



           $address = array(
               'Country_COUNTRY_Id' => $this->input->post('country'),
               'State_STATE_ID' => $this->input->post('state'),
               'USERS_USR_ID' => $USR_ID,
               'ADDR_CityName' => $this->input->post('ulica'),
               'ADDR_StreetName' => $this->input->post('ulica'),
               'ADDR_StreetNr' => $this->input->post('nr_ulicy'),
               'ADDR_HomeNr' => $this->input->post('nr_domu'),
               'ADDR_ZIP' => $this->input->post('kod_pocztowy'),
               'ADDR_Fax' => $this->input->post('fax')

           );


           $client = array(
               'Address_ADDR_ID' => $ADDR_ID,
               'CLI_Email' => $this->input->post('email'),
               'CLI_Name' => $this->input->post('imie'),
               'CLI_LastName' => $this->input->post('nazwisko'),
               'CLI_NIP' => $this->input->post('nip'),
               'CLI_Regon' => $this->input->post('regon'),
               'CLI_Pesel' => $this->input->post('pesel'),
               'CLI_Phone' => $this->input->post('telefon')
           );


          
          
           $us = $this->db->insert('users', $users);
            $add = $this->db->insert('address', $address);
           $cli = $this->db->insert('client',$client);
          



           $data = array('us' => $us, 'cli' => $cli, 'add' => $add);

           return $data;

       }
#2

[eluser]cideveloper[/eluser]
You are not getting the results from your query. Only the results object. The below should work.

Code:
$USR_ID = $this->db->query("select USR_ID from users where USR_Nick='".$this->input->post('username')."'")->row()->USR_ID;
          
        
          
$ADDR_ID = $this->db->query("select ADDR_ID from address where ADDR_StreetName='".$this->input->post('ulica')."'")->row()->ADDR_ID;
#3

[eluser]penta997[/eluser]
it still doesn't works. Now i have an errors:
Trying to get property of non-object and Column 'USERS_USR_ID' cannot be null.
#4

[eluser]cideveloper[/eluser]
I just looked at your code again and am wondering what you are attempting to do here. Please explain what this code is supposed to do, because what I can figure out does not make sense.

if $this->input->post('username') is not in the database it will return nothing. Then you can not insert that into table address most likely because you have a not null constraint on the field USERS_USR_ID.

if $this->input->post('username') is in the database you will have a bigger problem. You will be creating a new user in the users table but the USERS_USR_ID in the address table will be from the previously found user.


I think what you want to do is this:

1) insert user table
2) get last inserted id from user table ($this->db->insert_id())
3) insert into address table with USERS_USR_ID coming from (2)
4) get last inserted id from address table ($this->db->insert_id())
5) insert into client table with Address_ADDR_ID coming from (4)

If I am wrong then please advise as to what you are trying to do.
#5

[eluser]penta997[/eluser]
as I say, I have a register form. And this is my table structure:

table users:

USR_ID
USR_Nick
USR_Pass
USR_CreateDate


table address

ADDR_ID
COUNTRY_COUNTRY_Id
STATE_State_Id
USERS_USR_ID
ADDR_CityName
ADDR_StreetName
ADDR_StreetNr
ADDR_HomeNr
ADDR_ZIP
ADDR_Fax



table client

CLI_ID
Address_ADDR_ID
CLI_Email
CLI_NAme
CLI_LastName
CLI_NIP
CLI_Regon
CLI_Phone
CLI_Pesel

and i trying to insert data from form into database. And i have a problem with ID's, so I get the value from form for example: USR_Name and getting his Id to insert it to address table, becouse there I need this value.
#6

[eluser]cideveloper[/eluser]
Is this what you are looking for

Code:
function create_User(){

    $users = array(
        'USR_Nick' => $this->input->post('username'),
        'USR_Pass' => md5($this->input->post('pass')),
        'USR_CreateDate' => date('Y-m-d H:i:s')
    );
    $us = $this->db->insert('users', $users);
    $USR_ID = $this->db->insert_id();

    $address = array(
        'Country_COUNTRY_Id' => $this->input->post('country'),
        'State_STATE_ID' => $this->input->post('state'),
        'USERS_USR_ID' => $USR_ID,
        'ADDR_CityName' => $this->input->post('ulica'),
        'ADDR_StreetName' => $this->input->post('ulica'),
        'ADDR_StreetNr' => $this->input->post('nr_ulicy'),
        'ADDR_HomeNr' => $this->input->post('nr_domu'),
        'ADDR_ZIP' => $this->input->post('kod_pocztowy'),
        'ADDR_Fax' => $this->input->post('fax')
    );
    $add = $this->db->insert('address', $address);
    $ADDR_ID = $this->db->insert_id();

    $client = array(
        'Address_ADDR_ID' => $ADDR_ID,
        'CLI_Email' => $this->input->post('email'),
        'CLI_Name' => $this->input->post('imie'),
        'CLI_LastName' => $this->input->post('nazwisko'),
        'CLI_NIP' => $this->input->post('nip'),
        'CLI_Regon' => $this->input->post('regon'),
        'CLI_Pesel' => $this->input->post('pesel'),
        'CLI_Phone' => $this->input->post('telefon')
    );
    $cli = $this->db->insert('client',$client);

    $data = array('us' => $us, 'cli' => $cli, 'add' => $add);
    return $data;

}
#7

[eluser]penta997[/eluser]
Yes, thanks. I forgot about order Smile




Theme © iAndrew 2016 - Forum software by © MyBB