I'm working on a back-end functionality for a client. They want to be able to add new records and edit old records through a very basic view; I'm slowly getting somewhere but I'm stuck now. I'm afraid I didn't think my approach through from the start.
This is what it looks like at the moment:
The 'Add' fieldset slides out from under a button which isn't showing atm. It does exactly what it says, add new records to the table. This works fine, I have succeeded in making the view dynamic (fields change per table, etc) and the insert statements work fine aswell. The problem, however, lies in the edit functionality.
When I press the 'Edit' button next to a record, the values of that row get loaded in the form above. The customer can then edit the values and submit them.. Or should be able to.
I'm stuck here. Mostly the update statement makes me want to bang my head against a wall. With the table showing, it isn't exactly hard to write the query:
$this->db->where('guestid' => $guestid)->update($table)
The primary key here is guestid
. I should use this id to know which row I want to update. However, at the moment, I don't have a way to tell my form which row I'm going to update. I could make a hidden input field and put the ID in there.. But! I use this view, like I mentioned before, for different tables. Not every table has a guestid, etc. I could of course write a huge switch statement or if-structure to determine which table and thus which id should go into the hidden field, but I'm looking for an easier way.
This is mostly giving me headaches because some tables have a combined PK (E.G.: my table event_pass
has a combined PK of eventID
and passID
). I'm unsure how to link this to the form and how to tell my application 'look, update these values on this row with this PK'.
I have all the table metadata in an array, so I know the PK, etc.:
[eventID] => Array
(
[Field] => eventID
[Type] => int(10) unsigned
[Null] => NO
[Key] => PRI
[Default] =>
[Extra] =>
)
[passID] => Array
(
[Field] => passID
[Type] => int(10) unsigned
[Null] => NO
[Key] => PRI
[Default] =>
[Extra] =>
)
etc..
Any help with my thought process would be appreciated here. I'm not looking for full code snippet, just some thoughts, suggestions on how to implement this and make my life a little bit easier. I can provide more screenshots of how other tables might look, and I can of course provide code if necessary. Th开发者_如何学编程anks in advance.
You need to get the primary key from the table with a SHOW INDEX FROM <table>
query (see this) and send the primary key data to the page - as you suggested, a hidden input could be the way to do this, it's certainly nice and simple and should do exactly what you expect it to in every browser.
Then all you need to do is read that data when the request is submitted and build your where clause for the UPDATE
query accordingly...
精彩评论