I am working on a web application project, and there is a rather large html form that needs to have its data stored in a table. The form and the insert are already done, but my client wants to be able to load the saved data back into the HTML form and be able to change it, again, this is not a problem, but I came across a question, when are you going to do the update, it would be advisable to just save insert request and then delete the old line if it was editing?
Basically, what already happens when the form is submitted, all the data is put into the table using INSERT, I also have a flag called edit, which contains the identifier of the primary key, if the data is for the field being updated. I can handle the update function in two ways:
a) Create an actual update request with all fields / datasets and use if / else to decide whether to run the update or insert the request.
b) Paste each time, but add one row to DELETE WHERE row = editID after a successful paste.
Since deleting will only happen if INSERT was successful, I do not risk deleting the data without inserting, thereby losing the data, but since INSERT / DELETE are two queries, it would be less efficient than just using if / else to solve Should I run insert or update?
There is a second table that uses the auto-increment id as a foreign key, but this table needs to be updated every time the form is submitted, so if I delete a row in table A, I will also delete the related rows from table b. This seems like bad programming practice, so I'm leaning towards option a) anyway, but it's very tempting to just use the one line option. DELETE will look like this. Is this really bad programming practice? Beyond the conventions, are there any reasons why this is "never do it!". type of code?
if ($insertFormResults) { $formId = mysql_insert_id(); echo "Your form was saved successfully."; if(isset($_POST['edit'])){ $query = "DELETE FROM registerForm WHERE id='$_POST[edit]'"; $result = mysql_query($query); } }