简体   繁体   中英

PDO inserting last insert id into two tables

I'm attempting to insert the same insertId into a couple of link tables, however I've discovered that apparently the insert id gets reset to zero after executing the first method. These methods are separate as they get reused in other places. They are in separate classes.

There are essentially three processes being called, but I'm mainly concerned with these two updates, one of which cancels the other out.

if($resource->createResource()){

    $author->resourceAuthor();
    $subject->resourceSubject();

}

The two methods are essentially identical to each other. Each one inserts data into a separate link table (many to many).

The first one is this:

function resourceAuthor()
{

    $query = "INSERT INTO authors_has_resource (resource_id, author_id) VALUES (?,?)";

    $last_id = $this->conn->lastInsertId();

    $stmt = $this->conn->prepare($query);


    if(!empty($this->id)) {
        foreach($_POST['author_id'] as $id) {


            $stmt->execute(array(
                $last_id,
                $id
            ));
        }
    }
}

and the second one is this:

function resourceSubject()
{

    $query = "INSERT INTO resource_has_subjects (resource_id, subject_id) VALUES (?,?)";

    $last_id = $this->conn->lastInsertId();

    $stmt = $this->conn->prepare($query);


    if(!empty($this->id)) {
        foreach($_POST['subject_id'] as $id) {


            $stmt->execute(array(
                $last_id,
                $id
            ));
        }
    }
}

Which ever one I run first, works fine, but the second one just enters the resource_id as 0 (the other rows, ie, author_id or subject_id in their respective tables go in fine, but of course they end up with duplicate keys, as of course 0 is not the primary key, at least in this case)

Is there a simple way of getting around this? I've tried creating the insert id as a variable in the initial script, it does not seem to have an effect.

Edit

I have attempted something like this:

if($resource->createResource()){

    $last_id = $db->lastInsertId();
    print_r($last_id);
    $resource->resourceSubjectAuthor($last_id);


}

And then creating a new method

function resourceSubjectAuthor($last_id)
{

    $query = "INSERT INTO authors_has_resource (resource_id, author_id) VALUES (?,?)";


    $stmt = $this->conn->prepare($query);


    $query2 = "INSERT INTO resource_has_subjects (resource_id, subject_id) VALUES (?,?)";


    $stmt2 = $this->conn->prepare($query2);
    if(!empty($this->id)) {
        foreach($_POST['author_id'] as $id) {


            $stmt->execute(array(
                $last_id,
                $id
            ));
        }

        foreach($_POST['subject_id'] as $ids) {


            $stmt2->execute(array(
                $last_id,
                $ids
            ));
        }
    }       


}

The last id is being created correctly, but then nothing gets executed

Ok, I managed to solve this. The problem was directly related to the insert id being reset after one execution. I just needed to define the insert id prior to both methods being called, and then pass that variable to the methods prior to their executions.

if($resource->createResource()){

    $last_id = $db->lastInsertId();
    $author->resourceAuthor($last_id);
    $subject->resourceSubject($last_id);
}

and then in both methods, just pass the variable instead of declaring it like so:

function resourceSubject($last_id)
{

    $query = "INSERT INTO resource_has_subjects (resource_id, subject_id) VALUES (?,?)";


    $stmt = $this->conn->prepare($query);


    if(!empty($this->id)) {
        foreach($_POST['subject_id'] as $id) {


            $stmt->execute(array(
                $last_id,
                $id
            ));
        }
    }
}

and similarly:

function resourceAuthor($last_id)
{

    $query = "INSERT INTO authors_has_resource (resource_id, author_id) VALUES (?,?)";
    $stmt = $this->conn->prepare($query);


    if(!empty($this->id)) {
        foreach($_POST['author_id'] as $id) {


            $stmt->execute(array(
                $last_id,
                $id
            ));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM