php - PDO last insert id Alternative?

305

I am moving data from one table to another via aINSERT INTO table1 SELECT * FROM table2 query. The data being moved contains information about employees (first name, last name...etc) as well as the path to that employee's resume. I'm now trying to split that information up into two different tables, one table for the employee info, and one table for the document (resume) info, linking the two by putting the employee ID in the document table. Both the employee ID and the document ID will be auto incremented PK values.

I understand that I can put these queries into a for loop and move one row at a time, grabbing thelast insert id of the employee table before adding the document info to the document table in order to link the two. I am curious if there is a way to do this in one query, being able to take multiple rows from the original table, split up the info to be inserted into two new/different tables and and use the auto-generated id in the employee table as a value in the document table....hope this makes sense!

747

Answer

Solution:

Sorry if I get this wrong but do you want to execute this query once with your current DB Tables? And I guess both tables have the same amount of rows(and in order of each other)?

If you split those up you will get:

Employee table for example:

 - employee_id(auto_increment)
 - employee_firstname
 - employee_lastname
 - employee_document_id
 - +whatever you want etc

Document table for example:

 - document_id(auto_increment)
 - document_name
 - document_path
 - document_employee_id
 - +whatever you want etc.

If this is what you mean, than I think the following would work:

1: Setup PDO(The editor didn't work for me that's why ">")

    <?php

    $config['db'] = array(
        'host' => 'host',
        'username' => 'username',
        'password' => 'password',
        'dbname' => 'dbname'
    );

    $db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

    ?>

2: Setup insert queries


<?php
$select_query = "SELECT * FROM table1"; 
//$db is PDO example name
$select_all = $db->prepare($select_query);
$select_all->execute();
$count = $select_all->rowCount();
for(var i = 0; i =< $count; ++i) { 
    $insert_query1 = "INSERT INTO table1 (employee_firstname,
    employee_lastname, employee_document_id)
    VALUES(employee_firstnameValue, employee_lastnameValue,'"i"'";

    $insert_query2 = "INSERT INTO table2 (document_name, document_path,
    employee_id) VALUES(document_nameValue, document_pathValue, '"i"')"

    $insert_table1 = $db->prepare($insert_query1);
    $insert_table1->execute()

    $insert_table2 = $db->prepare($insert_query2);
    $insert_table2->execute()
}
?>

I think the above will work because you get an auto_increment starting at 1, en de ++i will occur every time. So the employee_document_id and the document_employee_id will both get ++i(which is 1) just like the auto_increment is(also 1)

But maybe this is to much thought.. Or not going to work in your model

Side notes:

  • Working with parameters is recommended in the query.
  • This is just a loose describing method which came up in my mind(maybe you can pick something up from here..)

EDIT: Another solution is to use a query like "SELECT MAX", but this is unsafe.

People are also looking for solutions to the problem: php - How to rename uploaded file before saving it into a directory?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.