View Full Version : Updating MySQL dB from HTML table values
07-13-2011, 11:45 PM
The table is generated with a loop like so:
# Python code embedded in a .psp file
cursor = db.cursor()
cursor.execute("SELECT from_name, from_address, project, orb_proj, app, tier, environment, network, security_zone, datacenter, server_type, num_servers, date, status FROM requests")
rows = cursor.fetchall()
for row in rows:
new_row = """
... (line repeated for each column)
""" % row[0:15]
Any ideas? Maybe this whole thing should be rewritten in something else. :(
07-14-2011, 12:14 AM
07-14-2011, 12:53 AM
Okay, well, that "server side script" is the big question mark for me. For example, how would I handle the fact that all the form elements (text boxes and drop downs) have the same name? They, and the rows that contain them, are generated with the above loop (one line would be <td><input ... value="%s"> ... </td>). Any easy way around this problem?
07-14-2011, 01:04 AM
You can assign array names to form controls like <input name="whatever" type="text" /> which can so a server side script (at least PHP (http://docs.php.net/manual/en/faq.html.php#faq.html.arrays), and I assume other languages, too) can work with it. Or, if you have some kind of loop I suppose you can pass the index, too, so each form control would have its own number like
<input type="text" name="whatever1" />
<input type="text" name="whatever2" />
07-14-2011, 01:14 AM
Yeah, that did occur to me, but I was hoping for something a bit cleaner as far as linking each row back to its respective SQL table row.
As it is now, the best, possible solution I can come up with is to use each row's index (either from the 'for' loop or the row's 'id' value stored in the dB) to name each form element element_id. Then pass the values to a python script using POST. Maybe I could make each row have its own form, so the user doesn't have to re-submit the entire table, including values that haven't even changed. Just thinking aloud here...
07-14-2011, 01:31 AM
Maybe I could make each row have its own form, so the user doesn't have to re-submit the entire table, including values that haven't even changed. Just thinking aloud here...
Ummm...you can only submit *ONE* <form> per page. So if you made each row its own form, the user could only update one row per load of that page. After each update, he/she would have to wait for the server to reload all the rows and then go (again) change one row.
I think you need to move this to a Python forum and get suggestions from Python users on how best to accomplish this.
One difficulty with using same-named form fields, even in PHP where they use stuff like <input name="address" /> to imply an array of fields, is that *UNCHECKED* checkboxes do *NOT* appear at all in the data sent by the browser to the server. So if your rows use checkboxes you can be utterly hosed.
In general, I think it is better to do something like
<input name="name_773" value="Joe"/>
<input name="address_773" value="111 Elm St." />
<input name="city_773" value="Amityville" />
<input name="name_817" value="Harry"/>
That is, you append the *ID* of the record in the data base to each field name. Then it's easy to find and process all related fields in your server-side code.
I'm 90% sure you can do all of this in pure Python code. You can certainly do it in pure ASP, JSP, ASP.NET, PHP, and CGI (Perl) code. So, again, try posting in a Python forum.
07-14-2011, 01:52 AM
OK, thank you, I appreciate the input and ideas.