2
$S = "INSERT INTO ". TBD ." (NODE, AV, BV) VALUES ('15', '$name', '$email')";
$Q = $CONN->query($S);
$M = $Q->insert_id;

$M returns NULL not 0


The above script runs the query fine, but will not return the unique ID generated. The table, definitely has a auto increment and is a primary key. I have used the script elsewhere and works fine.

So I have no idea why its returning NULL now.

alex
  • 5,516
  • 2
  • 36
  • 60
Andy Webb
  • 121
  • 1
  • 9
  • If your problem is solved, please mark the helping answer with the green check on the left. – alex Mar 06 '14 at 16:12

2 Answers2

3

I think you are calling insert_id wrong. Try this:

$S = "INSERT INTO ". TBD ." (NODE, AV, BV) VALUES ('15', '$name', '$email')";
$Q = $CONN->query($S);
$M = $CONN->insert_id;
alex
  • 5,516
  • 2
  • 36
  • 60
1

You need to extract the insert_id from the connection object and not the result set.

Your $Q variable is a mysqli result object so you'll want to extract the inserted id like this:

$CONN->insert_id;
Lix
  • 47,311
  • 12
  • 103
  • 131