0

I use the following code to access the id for the last inserted item but can only get 0 for relatedAccountID. May I ask if anyone can point out the issue? Many Thanks.

It continues showing 0 with executing the code

Thanks for the support from the post: How to get the id for the last inserted item in SQL Php admin

<?php

class Connection{
    static $conn = null;

    static public function connect(){
        if ($conn) {
            return $conn;
        }
        $link = new PDO("mysql:host=localhost;dbname=ham", "root", "");
        $link -> exec("set names utf8");
        self::$conn = $link;
        return $link;
    }
}

static public function mdlAddAccount($tableOne, $dataOne, $tableTwo, $dataTwo){
    $stmtTwo = Connection::connect() ->prepare("INSERT INTO $tableTwo(name, planLevel) VALUES (:name, :planLevel)");
    $stmtTwo -> bindParam(":name", $dataTwo["name"], PDO::PARAM_STR);
    $stmtTwo -> bindParam(":planLevel", $dataTwo["planLevel"], PDO::PARAM_STR);
    if ($stmtTwo->execute()) {
        $last_id = Connection::connect() -> lastInsertId();
        $stmtOne = Connection::connect()->prepare("INSERT INTO $tableOne(name, user, password, profile, status, relatedAccountID) VALUES (:name, :user, :password, :profile, :status, :relatedAccountID)");
        $stmtOne -> bindParam(":name", $dataOne["name"], PDO::PARAM_STR);
        $stmtOne -> bindParam(":user", $dataOne["user"], PDO::PARAM_STR);
        $stmtOne -> bindParam(":password", $dataOne["password"], PDO::PARAM_STR);
        $stmtOne -> bindParam(":profile", $dataOne["profile"], PDO::PARAM_STR);
        $stmtOne -> bindParam(":status", $dataOne["status"], PDO::PARAM_STR);
        $stmtOne -> bindParam(":relatedAccountID", $last_id, PDO::PARAM_INT);
        if ($stmtOne->execute()) {
              return 'ok';
       } else {
           return 'error';
       }
    } else {
        return 'error';
    }
    $stmtTwo -> close();
    $stmtTwo = null;
    $stmtOne -> close();
    $stmtOne = null;
}

enter image description here

Barmar
  • 741,623
  • 53
  • 500
  • 612
Eric
  • 5
  • 1

1 Answers1

0
if ($conn) {
    return $conn;
}

should be

if (self::$conn) {
    return self::$conn;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612