Ryflex Posted May 20, 2011 Share Posted May 20, 2011 Hi all, I'm starting an new project where I'm making an Incident Management System and right from the start I don't know what to do. Could anyone get me on my way. I need to make a script where if I open a new incident the new page opens and automatically gets a new Incident number. This number is offcourse 1 number higher every time you make a incident. I thought about make a column with autoincremetn and then query the highest number. Problem with that is when you open a page at multiple computers at the same time they get the same number. Does anyone have an idea? Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/ Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 The auto-incremented ID is only generated as you actually insert the row, and so no two users could end up with the same ID. Edit Just be clear, you don't need to actually query the table to find the current highest ID. MySQL will automatically generate and insert the number as you insert the row. You don't even need to include it within the columns you list in the insert statement. Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217962 Share on other sites More sharing options...
Ryflex Posted May 20, 2011 Author Share Posted May 20, 2011 Hi MrAdam, Thanks for the quick reaction! Well in my idea when you open the new incident page you create the new row and query it. That is because when I open the page I want the Incident number to automaticaly show. Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217963 Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 So you want to create a new, blank incident as soon as they click create? The problem I can foresee there is that the user may leave the page, have clicked create by accident, etc. You'll end up with blank records that sooner or later you're going to have to purge. Of course, the severity of that depends upon the size of the organisation this is for, or whatever you plan to do with it. Anyway it's perfectly possible to do what you want, but you will need to insert the blank row (which means you'll need to allow null values for every column) to guarantee the user gets the incident number you display. Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217964 Share on other sites More sharing options...
Ryflex Posted May 20, 2011 Author Share Posted May 20, 2011 Hi MrAdam, I wil make another script which will run everyday and delete blank records. My big question with the solution I have won't 2 users end up with the same number because the clicked create at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217965 Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 Nope. MySQL uses table-level locking during an insert, to prevent issues like that. Even if they both fired at the very same millisecond, only one insert would be performed at a time whilst the other waited. Afterwards you can use the mysql_insert_id function to retrieve the ID that was created. Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217966 Share on other sites More sharing options...
Ryflex Posted May 20, 2011 Author Share Posted May 20, 2011 Thanks!!!! Quote Link to comment https://forums.phpfreaks.com/topic/236953-incident-management-system/#findComment-1217967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.