Jump to content

Using foreach to update multiple records


NiallFH

Recommended Posts

What I'm trying to do seems very basic, but having not done it before I think I've become quite lost along the way.

 

I have a record that has a list of goals, each has its own ID and also a number next to it which is the minute the goal is scored.

 

I have written an html form which posts information the code detailed below.

 

Here is the only input box from my form:


<input type=\"hidden\" name=\"goal_id\" value=\"$gsdata[id]\">
<input type=\"text\" name=\"minute\" size=\"2\" value=\"$gsdata[minute]\">

It is displayed using a mysql_fetch_array to display a list of all goals associated to one match, so naturally there could, 2, 3, 4, or any number of records displayed as input boxes shown above.  When I click the Submit button, it posts to the page shown below:

$goalid = $_POST['goal_id'];

foreach($_POST['minute'] as $goaltime) 
{ 

 mysql_query("
 UPDATE 
  pro_goals 
 SET
  GoalMinute = '$goaltime'
 WHERE 
  GoalID = '$goalid'
 ",$connection);   

} 

Can anyone offer any help?

you will want to use a CASE on the insert, Barand should be able to knock you up something elegant if he sees the post, but have a look here in the interim :

http://stuporglue.org/update-multiple-rows-at-once-with-different-values-in-mysql/

@NiallFH, since you started a new thread after this, one using array names in your form, so that you would even have arrays in the form processing code, this thread is no longer active.

 

just because your code for an issue evolves, don't start new threads for the same problem, continue in the same thread, or at least have the consideration to go back to earlier threads and post in them that you no longer need help with the stated issue.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.