Inserting values using a loop (PHP/MySQL)

Hi Everyone,

I have a php script that uses a loop to insert data into a mysql table. The first page has a list of items followed by text fields that are created from tables values with a ‘while’ loop that increments var $i each time. Text field names are ‘task_$i’ and ‘hours_$i’. These I can get to post no problem. The php in the form action goes like so:

$get_tasks = "SELECT task_name FROM chris_tasks;";
$result = mysql_query($get_tasks);
$num_rows = mysql_num_rows($result);

for($i = 0; $i < $num_rows; $i++){
     $task = $HTTP_POST_VARS['task'.$i];
     $task_hours = $HTTP_POST_VARS['hours_'.$i];
     print "Task: $task<br>Hours: $task_hours<br>";//this prints fine for each loop
     $add_tasks = "INSERT INTO $tablename (task_name, task_hours) VALUES ('$task', '$task_hours');";
     print "Query $i: $add_tasks"; //this prints a correct, unique query each time
     $tasks_result = mysql_query($add_tasks); //this line is only successful for the first loop
          print "Success<br>";
          print "Failure<br>";

With the script as shown (minus spelling errors - I retyped it) it works up until trying to do the $add_tasks query for a second time (or more). All the data is posted and printed for the first loop and all successive loops, but the query only works once.

I found something similar on Google answers that has the same structure, but it doesn’t seem to work for me.

Can anyone see a problem? I’ve tried adding .$i to the query and the result but then it screws up the posted data (only the first item posts - all others are empty).