|
Posted by Rik on 09/28/59 11:46
number1.email@gmail.com wrote:
> I have a question regarding incrementing records in a mySQL Database
> Table. For example, if the User Table has a UserID field as the
> primary index, I could always Select the MAX(userid) field, increment
> it, and add a new user this way.
Why trouble yourself with php-code which is handled by the database itself?
Primary key should be an autoincrement field (integer), for reference to new
primary key if further operations on the database are needed, use
mysql_insert_id($resource). Inserts can be done without setting the primary
key to an explicit value, this is handled by MySQL.
If you're looking for a function updating existing records look into the
"REPLACE" posssibility of MySQL.
> Problem is, what if someone also
> requests this same userid before I "insert" my record?
Why would thid happen? It seems to me this is not a database problem but a
code problem. If a request from a client requires a certain primary field,
it should be a result from earlier code, and exist. If a request from a user
asks for a non-existent ID, it would be an illegal request if you'd code
correctly, and as such an exception which wouldn't need catching, as it's a
client try for vulnerabilities, not a legel request.
> This would
> cause a problem as it is not unique... In Oracle you can "Select
> <val> from dual", which is automatically updated...so no person gets
> the same number. Does mySQL have something similar, like a counter
> field? How do I go about this?
autoincrement your primary key field.
use REPLACE instead of INSERT.
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|