Get the last record id inserted to db and pass to redirect url
my url is employeesview.php?showdetail=&id=
so id mus go after &id=
thanks
Question could be improved (e.g. what DB to be used, why do you think you have to provide the ID as HTTP GET argument, etc.)
But the answer could be easy. Just guessing you are using a mysql db (because php beginners often use mysql).. in this case please check auto_increment attribute for your ID column in sql. It allows that PHP automatically uses the next ID for your new records, and you don't need to provide the ID for inserting a new row.
See https://www.w3schools.com/sql/sql_autoincrement.asp
Related
I understand the lastInsertId or mysqli_insert_id function returns the last inserted id of the insert or update query but say you had two intending users filling out a form in different locations at the same time and user2's id was the last at the time the mysqli_insert_id on user1's script was being run instead of user1's id. will the id returned be user2's id or user1's id? I know the script will run in micro or milliseconds and its possible the scenario might not ever occur but is it possible that the returned id in a case where two intending users post form data at the same time can be different from the actual id that should be returned.
I hope the question is pretty clear. I am also very new to programming so pardon me.I would also like to thank all the contributors on stack exchange, I have learnt alot in a very short while.
Nothing to worry about here.
They all call the underlying MySQL functionality.
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.
You can use LAST_INSERT_ID().
It will get you the latest row inserted in a table i.e the last row for you.
I'm trying to add a row to a table, and then immediately get its unique ID, which is simply an autoincremented column.
Googling tells me PHP can do this through the mysql_insert_id() method, which
Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
Is the "previous query" limited in scope to this specific connection, or is it possible that if somehow another row is inserted before I call it, it will return the wrong ID?
Are there any better suggestions on how to get or set a unique ID for a row? I'd make the unique ID on the client-side, but there are multiple clients, so that's not really possible.
Thanks.
Yes, it's limited to connection, so it will return a reliable value.
Try to use mysqli-functions or PDO, MySQL will be deceprated.
In a customer CMS the customer can update their personal information, like change their address and first/last name. I use a mysql UPDATE query for that.
Problem with working like this is, is that people can change their information. E.g. change their name from john doe to asdfdas. I would like to SAVE their old information.
What are my options? What is the best way to do this?
Assuming the user has a unique ID you could have an old_user_information table and when you do an update also do a new entry into that table. The table would have an autogenerated ID as well as the unique user ID and the rest of that users past information.
A user could have multiple rows in this table but only one row in the real Users table.
Edit: If I were you I would write a stored procedure that does both of these things so that it is easier to manage if things change.
You can make table that contains something like this
`yourTableID, field, value, date`
and update this with a trigger. You write an update trigger that adds the old value if it is changed. Look at the manual here to find out more about triggers.
If you don't want to use triggers you could obviously do the same in your logic: just update the history table with the old value. But this needs some trickery to find out if you need to update it, but nothing to complicated.
For easy retrieval what happened you might want to add something like "oldvalue" AND "newvalue", but the latter isn't really needed, as it is either in the next update as 'old' value, or it is the current value.
Create new columns for the data being updated column would be prior_to_update_column
On update, move the old info into prior_to_update_column
If the user updates again, append to the prior_to_update_column seperated by , (to look like an array).
This should keep all the previous info the user updates
add an additional field name version
and use Insert instead of update
あの答えのとうりに、やってない。
UPDATE table name SET column=column+'new value' WHERE condition
I am building a web application, which can be used by multiple users simultaneously. They can add data at the same time.
I have a table named doctor_main as follows
Screenshot of DB http://img687.imageshack.us/img687/7033/testxqz.png
Once a record about a doctor is added, I want the id of the inserted record(which is an auto increment field) to be returned.
I know i can use methods like LAST_INSERT_ID() and mysql_insert_id. But i don't know how it behaves.
I need the exact id of the record which is inserted by that particular user.
Sometimes, if two users are trying to insert a record, the id which is returned shouldn't get exchanged.
To achieve this what kind of mysql function should i use ?
There's a whole page in the manual dedicated to this subject:
How to Get the Unique ID for the Last Inserted Row
Here's a quote from that page that should help answer your question:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
mysql_insert_id() returns exactly the id of the last inserted record, so if you just echo mysql_insert_id() you'll get the id of the very last inserted row.
According to the docs, the mysql_insert_id will return to you the exact id of the insert that you done before.
LAST_INSERT_ID() operates per-connection; multiple users will use multiple connections, so there will never be an exchange of IDs between users.
LAST_INSERT_ID() and mysql_insert_id works fine. Each client will receive the last inserted ID for the last statement that client executed.
If you are suspicious on the mechanism of last_insert_id, then you may assign the id by hand not by auto_increment feature of MySQL.
I have a website which generates each visitor a referral link (ex. http://mysite.com/?ref=12345678). The actual referral id (12345678) is a unique 8 character ID (using uniqid() ).
I then just add the ID to the end of http://example.com/?ref=....
I am trying to find a script which can connect to my MySQL database, check if the id exists, and if it doesn't, enter it into the table.
If it does exist it shouldn't do anything.
I am guessing that I need to implement a cookie to check if the id exists, so I don't really need help with that. I'm just confused to how to make the script I mentioned above.
I'm trying to make the table look like this:
Unique ID
---------
3af456yT
Sa32xs21
9af456yT
8a78Fs21
1wsd4Fav
7f3Xv5Bd
Here is a great place to start: http://php.net/manual/en/book.mysql.php. The documentation will provide the information you need to connect to a MySQL database via PHP and to insert data. Specifically, you will need to use the mysql_query() function.
You can use the "UPDATE INTO" syntax to ensure that you do not create duplicate rows. Please see http://dev.mysql.com/doc/refman/5.0/en/update.html for more information.