1

I am looking to add a function that will get the largest number from a specific column in a table and add to it before doing an INSERT query. (I cant have it be auto increment as several entries need to have the same value this is controlled through an if statement) however it isnt doing this and isn't increasing it by 1 based off the highest value.

$max = "SELECT MAX(LocationID) FROM boss";
$result = mysqli_query($connection, $max);
$locID = $result+1;

$query = "INSERT INTO boss (ID, Name, Type, Location, LocationID, Difficulty) VALUES ('0', '$boss', '$type', '$loc', '$locID', '$diff')";
Kai Jones
  • 145
  • 4
  • 14

2 Answers2

1

You don't need to use two queries for this, you can do it in the INSERT query.

$query = "INSERT INTO boss (ID, Name, Type, Location, LocationID, Difficulty)
    SELECT '0', '$boss', '$type', '$loc', MAX(locationID)+1, '$diff'
    FROM boss";
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You forgot to fetch the result

$max = "SELECT MAX(LocationID) as m FROM boss";
$result = mysqli_query($connection, $max);
$result = mysqli_fetch_array($result);
$locID = $result[0]+1;
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24