Riggsfolly already warned you about generating your own IDs.
It is MUCH safer to let the database generate UNIQUE ID's.
(Almost) Every table I create gets a primary key that has some form of SERIAL in it.
For example: For Postgres I use SERIAL:
CREATE TABLE registration (
registrationid SERIAL PRIMARY KEY,
project_name VARCHAR(100),
whatever VARCHAR(1000)
)
You seem to use MYSQL: Look up AUTO_INCREMENT.
Now, if you insert into that table, you do it without the registrationid:
INSERT INTO registration (project_name , whatever ) VALUES ('test2', 'hi there');
If this is the first insert in the table registrationid will automagically become 1.
Next insert is will be 2. etc etc.
Now suppose you did this 1000 times, you next one will then be 1001.
When you delete a few, that doesn't matter, the next will be 1002.
Now: If you want the query for the highest registrationid AND some project_name, do it like you did, but make sure you get the MAX() of the ones that have your desired project_name:
SELECT *
FROM registration
WHERE registrationid= (SELECT MAX(registrationid) FROM registration WHERE (project_name='test2') ) ;
2 last observations:
- Follow Riggsfolly's advice and do not generate your own uniqueness, but let the database do this using appropriate syntax when defining your table (SERIAL for Postgres, AUTO_INCREMENT for MySQL, etc).
- In my example the inserts are naive. It is better to use PDO for databinding when working with strings to avoid SQL-injection.