Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is you are trying to insert into the salaries table, but you aren't specifying what employee the salary record is for. You would need to provide a value for the <code>emp_no</code> column that is in the <code>employees</code> table.</p> <p>ETA:</p> <p>Glad you posted more code - what you should do is modify your <code>add_emp</code> function so that it returns the id of the employee that was created (I'm assuming here that <code>emp_no</code> is and auto-increment column in your database). Like so:</p> <pre><code>function add_emp($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate) { $data = array( 'first_name' =&gt; $firstname, 'last_name' =&gt; $lastname, 'gender' =&gt; $gender, 'birth_date' =&gt; $date_of_birth, 'hire_date' =&gt; $hiredate); $this-&gt;db-&gt;trans_start(); $this-&gt;db-&gt;insert('employees', $data); $this-&gt;db-&gt;trans_complete(); if ($this-&gt;db-&gt;trans_status() === FALSE) { //$msg = "Adding the new employee failed."; //return $msg; return -1; // indicates failure } else { //$msg = "Successfully Added Employee."; //return $msg; return $this-&gt;db-&gt;insert_id(); } } </code></pre> <p>Note that the code that calls <code>add_emp</code> will need to change a bit to look at the return value as an integer instead of a message now, but now you have the id of the employee and you can use that in your add_salary function instead of having to pass in all that other data, like so:</p> <pre><code>function add_salary($emp_no,$salary, $salary_from_date,$salary_to_date) { $salarystuff = array('emp_no' =&gt; $emp_no, 'salary' =&gt; $salary, 'from_date' =&gt; $salary_from_date, 'to_date' =&gt; $salary_to_date); $this-&gt;db-&gt;insert('salaries', $salarystuff); } </code></pre> <p>So the code to add an employee and their salary would be something like this:</p> <pre><code>$emp_no = add_emp([Your parameters]); if ( $emp_no &gt; 0 ) { add_salary($emp_no, [other parameters]); } else { // Show some error message that the employee creation failed. } </code></pre> <p>EDIT AGAIN:</p> <p>If you ignore my advice above, the simple answer to why you are getting an error is because <code>$selected_employee</code> is a result set and not an actual employee number. You would need to change that code like so:</p> <pre><code>$selected_employee = $this-&gt;db-&gt;get(); $result = $selected_employee-&gt;result(); $emp_no = $result[0]-&gt;emp_no; $salarystuff = array('emp_no' =&gt; $emp_no, 'salary' =&gt; $salary, 'from_date' =&gt; $salary_from_date, 'to_date' =&gt; $salary_to_date); $this-&gt;db-&gt;insert('salaries', $salarystuff); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload