1 person found this helpful
Getting the next increment number is very bad practise, when that number is given it doesn't assume you will use it, for instance it returns 100, in the meantime someone else has appended a new record. guess what the 100 you have is no longer the next increment number, The only way to get your identifier is to append a blank record then return its id value, if for some reason you no longer require that 'empty' record you can always delete it.
I know what you mean but for this particular app only 1 person will be using it. This version is for my client to create and ammend links. My users themselves who will have a similar app will only be able to do things such as favourite links and edit the data etc.
Perhaps you're right though, maybe it would be best in a single php function to add a new blank record, get the max linkid i.e. the linkid of this new record and then delete this record all in the same php function so the value is exact, then the value in my app would be this value +1 (the next auto increment). Is that what you meant and would that value be exact in this case?
Cheers for the advice.
1 person found this helpful
The idea is you create the blank record and once you have the id you insert the id into your array then update the record, no insert needed, as you already own that record.
Sorry I'm a bit confused. Do you mean to say that I should create multiple blank records which will act as record slots so to speak which the user can insert data into and when they remove an item the record will just be made blank but still be kept in the table and I could have some sor of filter function in my application which makes it ignore the blank records when displaying data. The problem with this is that I want the user to be able to add an infinite amount of records.
Do you mean to say that I should add a blank record at the end of my table which will serve to provide the highest id value for my app? But the problem with this is if a user adds another record it will increment past this blank record and have the new highest id value so the blank record I made would be invalid for this purpose then.
Sorry for the confusion.
step 1. the client wants to add a new record. and sends a simple request for a new record
step 2. the server adds a new record to the database (its now blank) and returns its ID number
step 3. the client now has a proper ID number and uses it to add a new object to the array
step 4. the client armed with a record ID and new data for that 'empty record' does a record update not an insert.
step 5. you now have the array and the database in sync.
Gotchya. So what I'd basically be doing is reserving a slot for the link by adding in only the linkid and leaving the rest of the fields for that record blank whereas my auto-increment method would have just been basically finding out what the next slot is but leaving it up for grabs to other users. This wouldn't really matter in this app with there only being one user and one app but you're right it is bad practice and I can apply the method you've outlined to my user app in which case there would be multiple users and multiple instances of the same app i.e. a chance of such data overlaps occurring.
Great idea David. Cheers.
I'm just trying to figure out a way to get all that to happen in one smooth click of the add button. In my php code I have a line which adds in a new record I suppose I'll have to edit it so that before this happens the record with just the linkid value and the rest of the fields blank is added followed by a bit of code which outputs this new linkid as xml. Then I could retrieve this value in a result handler in my app and in the result handler would be the code which inserts the user inputted data in my app into the empty fields of this new link via http request closely followed by the code which creates the new array collection item containing this new link id value and the same user inputted data so they are both in sync as you say. That sound about right?
Actually I think I've got an even simpler solution that wouldn't need the blank record:-
- When the user clicks the add button it sends the http request to add the new link to the database. The link will auto increment.
- Following this I could get my php file to output as xml the linkid of this record which would be caught by a result handler in my app which would save this data as an object proxy.
- The result of this result handler could contain the additem code which would add this same data that has just been added to the database from the object proxy to my array collection.
The problem here is how do I output the values of a record that has just been added? I am awful with php :-(
Here's how I insert a new record via php:-
$query = "INSERT INTO links(categoryid, label, icon, url) VALUES('$param_categoryid', '$param_label', '$param_icon', '$param_url')";
Obviously the linkid doesn't need to be entered as it is created automatically by the database table via auto increment but what I'd need it to do directly following this insert is output, as xml, the linkid, categoryid, label, icon and url of THAT item that has just been added. But how exactly do you get these values of an an item that has JUST been added via php code?