www.webdeveloper.com
Results 1 to 11 of 11

Thread: Any one Help me Please

  1. #1
    Join Date
    Sep 2013
    Posts
    18

    Any one Help me Please

    I'm new to PHP,

    How to insert data into a table using foreign key!

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    If this is truly a foreign key then that is a pointer to somewhere else, so you are not accessing the record in the current table properly.

    Perhaps you could explain better.

  3. #3
    Join Date
    Sep 2013
    Posts
    18
    emp_table is table1, emp_settings is table2... table 1 having primary key...

    by using that primary key i want to insert data in table 2

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    The only safe way to have that work is to have a one-to-one relationship between those two tables. AND if that is true, then why are they separate tables? It appears to me that your database is not fully normalized.

    OTOH - you s/b using table 2's primary key to do updates to table 2. Period.

    If, in the case that it is ok to do this, then it's simply a matter of reading t1, getting the fk from it and then updating t2. No? It's not a php problem, it's an logic problem.

  5. #5
    Join Date
    Sep 2013
    Posts
    18
    that prob is belongs to not only these two tables....

    in my database having 17 tables....!(attendance, vacation, shifts, schedules..etc...)
    emp_id is foreign key for all tables...

    by using emp_id only retrieve the data...

  6. #6
    Join Date
    Sep 2013
    Posts
    221
    Try out with the below example:

    insert into emp_settings (`order no`,fid)
    values(1,(select id from emp_table where name='abc' LIMIT 1));

    Hope this helps.
    strad solutionswww.stradsolutions.com

  7. #7
    Join Date
    Apr 2013
    Posts
    13
    You use MySQL INSERT and select syntax?

  8. #8
    Join Date
    Sep 2013
    Posts
    18
    this code will work in only selected record only naaaa!

  9. #9
    Join Date
    Sep 2013
    Location
    Houston, Texas.
    Posts
    26


    I think you need to go http://www.w3schools.com/php/

    There is alots of tutorials and hope it will helpful to you.


    Milina

  10. #10
    Join Date
    Sep 2013
    Posts
    221
    yes, i have used both select and insert. I have used select query inside insert query for your selected records.This is because you have to use primary key to insert data in table 2 as you mentioned it before.
    strad solutionswww.stradsolutions.com

  11. #11
    Join Date
    Sep 2013
    Posts
    18
    mysql_query($sql);
    $sql = 'select last_insert_id() as lastId';
    $result = mysql_query($sql);
    while($row = @mysql_fetch_assoc($result)){
    $lastId = $row['lastId'];
    }

    it is working.....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles