1

hi i have a problem about adding data into two tables, heres the situation, i want to add a student with his username and password in users table and his personal info like name and age into users_profiles,, heres my code:

function add_user($username,$password,$fname,$lname,$sex,$address,$city,$country,$role) { $this->db->trans_start();

    $user = array(
    'usrName'=>$username,
    'usrPassword'=>sha1($password),
    'roleID'=>$role
    );

    $this->db->insert('users', $user);

    $this->db->query('SELECT usrID FROM users WHERE usrName=$username');

    $usrID = $this->db->get(); //i know this is wrong thats why i need help

    $user_profile = array(
    'usrpFirstName'=>$fname,
    'usrpLastName'=>$lname,
    'usrpSex'=>$sex,
    'usrpAddress'=>$address,
    'usrpCity'=>$city,
    'usrpState'=>$country,
    'usrID'=>**$usrID** // this is the foreign key from users table
    );

    $this->db->insert('users_profiles', $user_profile);

    $this->db->trans_complete();
}

1 Answer 1

2

You do not need to use get() when you use the query() method. Instead store the result from the query() method and use result() or row() to access the data.

So your code should be something like:

$result = $this->db->query("SELECT usrID FROM users WHERE usrName=$username");

$row = $result->row();

$user_profile = array(
  'usrpFirstName'=>$fname,
  'usrpLastName'=>$lname,
  'usrpSex'=>$sex,
  'usrpAddress'=>$address,
  'usrpCity'=>$city,
  'usrpState'=>$country,
  'usrID'=>$row->usrID
);
Sign up to request clarification or add additional context in comments.

6 Comments

it returns an error. can you please help.... A Database Error Occurred Error Number: 1054 Unknown column '$username' in 'where clause' SELECT usrID FROM users WHERE usrName=$username
also with the code above it also returns error Fatal error: Call to undefined method CI_DB_mysql_driver::row() in C:\wamp\www\pgoadmin\application\models\user_model.php on line 54
the $row = $this->db->row(); returns an error Fatal error: Call to undefined method CI_DB_mysql_driver::row()
the code is working but after inserting data from users table ,, the next query cant read the username=$username even it is already inserted in users table
Unknown column 'sample' in 'where clause' SELECT usrID FROM users WHERE usrName=sample,,, but in phpmyadmin usrName sample is already inserted
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.