Can't write data into database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Can't write data into database (/showthread.php?tid=37652) |
Can't write data into database - El Forum - 01-16-2011 [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() Can't write data into database - El Forum - 01-16-2011 [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; Can't write data into database - El Forum - 01-16-2011 [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. Can't write data into database - El Forum - 01-16-2011 [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. Can't write data into database - El Forum - 01-16-2011 [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. Can't write data into database - El Forum - 01-16-2011 [eluser]cideveloper[/eluser] Is this what you are looking for Code: function create_User(){ Can't write data into database - El Forum - 01-16-2011 [eluser]penta997[/eluser] Yes, thanks. I forgot about order |