Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add foreign key value in insert query - mysql - CodeIgniter
    primarykey
    data
    text
    <p>I have the following insert query:</p> <pre><code>$salarystuff = array('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>The salary table has columns: emp_no| salary| from_date| to_date (I'm using the database available from dev.mysql.com.</p> <p>But it gives me an error 1452 saying foreign key constraint. How do I reference to the key value in the other table to be able to insert into this table?</p> <p>This is the error message:</p> <blockquote> <p>Cannot add or update a child row: a foreign key constraint fails (<code>employees</code>.<code>salaries</code>, CONSTRAINT <code>salaries_ibfk_1</code> FOREIGN KEY (<code>emp_no</code>) REFERENCES <code>employees</code> (<code>emp_no</code>) ON DELETE CASCADE)</p> <p>INSERT INTO <code>salaries</code> (<code>salary</code>, <code>from_date</code>, <code>to_date</code>) VALUES ('1000000', '2012-12-27', '2013-01-16')</p> </blockquote> <p>Thank you</p> <p>edit: I'm trying the following</p> <p>First I create the record in the employees table using this function:</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; } else { $msg = "Successfully Added Employee."; return $msg; } } </code></pre> <p>and then in another function I add the salary:</p> <pre><code>function add_salary($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate,$salary, $salary_from_date,$salary_to_date) { $this-&gt;db-&gt;select('emp_no'); $this-&gt;db-&gt;from('employees'); $this-&gt;db-&gt;where('first_name', $firstname); $this-&gt;db-&gt;where('last_name', $lastname); $this-&gt;db-&gt;where('gender', $gender); $this-&gt;db-&gt;where('hire_date', $hiredate); $this-&gt;db-&gt;where('birth_date', $date_of_birth); $this-&gt;db-&gt;limit(1); $selected_employee = $this-&gt;db-&gt;get(); $salarystuff = array('emp_no' =&gt; $selected_employee, '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>both of which are in the model. Then in the controller I call both functions:</p> <p>$employee_insert = $this->user->add_emp($firstname,$lastname,$gender,$date_of_birth,</p> <pre><code>$jobtitle,$dept, $hiredate); $salarythings = $this-&gt;user-&gt;add_salary($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate,$salary, $salary_from_date,$salary_to_date); </code></pre> <p>But I get an error because there is no value in the emp_no field...I think it might be the line: </p> <pre><code>$selected_employee = $this-&gt;db-&gt;get(); $salarystuff = array('emp_no' =&gt; $selected_employee, </code></pre> <p>My error message is: </p> <blockquote> <p>Object of class CI_DB_mysql_result could not be converted to string</p> </blockquote>
    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. This table or related slice is empty.
    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