2

I've created this to insert to multiple tables, and I want to insert the same ID to both table. ID is auto-incremented in test table and I want test_category table to take the same ID.

I tried it but don't know where I am doing it wrong and getting an error

Fatal error: Call to undefined method mysqli_stmt::insert_id() in C:\wamp64\www\Android\include\DbOperations.php on line 27"

Check this pic for detailed error report

[https://www.dropbox.com/s/nuij2o2ac3xp8mz/Untitled4.png?dl=0]

My php

public function testReg($name, $pin, $a, $b, $ho, $ll, $c, $d){
    $stmt = $this->con->prepare("INSERT INTO `test` (`name`, `pin`) VALUES (?, ?)");
    $stmt->bind_Param("ss",$name,$pin);
    if(!$stmt->execute()){
        return 2;
    }
    $stmttst = $this->con->prepare("INSERT INTO `test_category` (`pid`, `name`, `a`, `b`, `ho`, `ll`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
    $stmttst->bind_Param("isssssss",$stmt->insert_id(),$name,$a,$b,$ho,$ll,$c,$d);
    if ($stmttst->execute()){
        return 1;
    }else{
        return 2;
    }   
}
1

3 Answers 3

1

The "mysqli::$insert_id" is an attribute of mysqli object and your trying on $stmt Try Following code will resolve your issue.

public function testReg($name, $pin, $a, $b, $ho, $ll, $c, $d){
        $stmt = $this->con->prepare("INSERT INTO `test` (`name`, `pin`) VALUES (?, ?);");
        $stmt->bind_Param("ss",$name,$pin);
        if(!$stmt->execute()){
            return 2;
        }
        $stmttst = $this->con->prepare("INSERT INTO `test_category` (`pid`, `name`, `a`, `b`, `ho`, `ll`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
        $stmttst->bind_Param("isssssss",$this->con->insert_id(),$name,$a,$b,$ho,$ll,$c,$d);
        if ($stmttst->execute()){
            return 1;
        }else{
        return 2;
        }

   }

As of Documents mysqli::$insert_id -- mysqli_insert_id — Returns the auto generated id used in the latest query

So instead of using $this->con->insert_id() you can use

$var = $this->con;
mysqli_insert_id($var);
Sign up to request clarification or add additional context in comments.

2 Comments

it's[mysqli_insert_id($this->con)] working giving an eeror also "Strict standards: Only variables should be passed by reference in C:\wamp64\www\Android\include\DbOperations.php on line 27"
got it... just need to call it using variable. Thanks anyways.
1

Try putting your last inserted id in a variable and then pass it to your 2nd query.

public function testReg($name, $pin, $a, $b, $ho, $ll, $c, $d){
$stmt = $this->con->prepare("INSERT INTO `test` (`name`, `pin`) VALUES (?, ?);");
$stmt->bind_Param("ss",$name,$pin);
if(!$stmt->execute()){
   return 2;
  }else{
        //ADDED ESLE AND STORE ID IN VAR
     $lastid=$stmt->insert_id();
  }


$stmttst = $this->con->prepare("INSERT INTO `test_category` (`pid`, `name`, `a`, `b`, `ho`, `ll`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
$stmttst->bind_Param("isssssss",$lastid,$name,$a,$b,$ho,$ll,$c,$d);
// CHANGED BIND PARAM for last id

if ($stmttst->execute()){
    return 1;
}else{
   return 2;
}

}

Also, you shold specify if you are using mysqli or PDO as most people assume you are using PDO as it is most commonly used.

http://php.net/mysqli_multi_query might also help you in this case. Or try creating a second connectionto the BD, not just a 2nd statement.

1 Comment

further more you could nest your 2nd call inside the else so if the 1st query fails, the 2nd won't run.
0

You are probably looking for PDO::lastInsertId. It returns the ID of the last inserted row. Therefore you can use this in your second query as it will hold the ID of the first. More info here: http://php.net/manual/en/pdo.lastinsertid.php

1 Comment

I don't understand about how to implement it... can you show me editing my php file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.