Updating multiple rows from two different tables with one form

Hi,

I have been teaching myself MySQL and PHP over the past few months and I have been trying to figure out how to update multiple rows over two different tables with one button click.

I can pull the data from the tables and display them using a foreach loop in my template, but I don’t know how I would code it so I can make changes to the different rows at once. I know how to update each entry one at a time, but this approach is too tedious, as I want to make changes then click one button to update everything. I am coding with a controller and template, putting my information in a while loop in the controller before using a foreach to display in the template.

If anyone can offer any assistance or good advice on how to tackle this, I would greatly appreciate it.

The stuff I showed will be produced by your template. It’s basically wrapping the html table you are already generating with a html form element.

Then you will need to adjust your post processor. Somewhere you already have code like: $something = $_POST[‘something’]; You will need to modify that code to use the posted arrays.

You might be better off looking for a step by step tutorial to walk you through things in detail. There a number of step you need to take to get something like this working. Not difficult once you have done it a few times but plenty of gotchas along the way.

I apologize for my novice-ness, but I’m slightly confused. If I’m using a foreach loop in my template, how do I post back arrays as you describe? Where does the table example you shared go? Into my controller or do I recode my template? Again, I’m sorry for being confused. I’m really new at this.

Basically you need to post back arrays for each column of data to be updated, turn them back into rows then update your database accordingly. Kind of hard to explain but start with something like:


<form>
<table>
<tr><td>
  <input type="hidden" name="person_ids[1]" value="1" />
  <input type="text" name="person_fname[1]" value="First" />
 <input type="text" name="person_lname[1]" value="Last" />
</td></tr>
<tr><td>
  <input type="hidden" name="person_ids[2]" value="2" />
  <input type="text" name="person_fname[2]" value="First" />
 <input type="text" name="person_lname[2]" value="Last" />
</td></tr>
<tr><input type="submit" />
</table>
</form>

You will end up with three arrays in $_POST. Loop though the id array, pull the values out of the other two arrays then do a SQL update. Once you get it working for multiple rows in one table then adding more tables is pretty easy.