PHP $_POST Loop with Variables

kenfx
kenfx used Ask the Experts™
on
I'm writing a form that needs to update multiple mysql records at one time. The fields are created dynamically using a for loop. I want to be able to catch the $_POST values to update the records.


Here is the for loop that creates the form:

  for ($i=0; $i<$dRows; $i++) {
  $drow = mysql_fetch_array($dResult);
  echo "<tr>";
  echo "<td width=\"150\"><div align=\"right\">".date('D - M j',strtotime($drow['eventDate']))."</div></td>";
  echo "<td width=\"110\"><div align=\"center\"><input name=\"time1_".$i."\" id=\"time1_".$i."\" type=\"text\" class=\"formText\" size=\"10\" maxlength=\"10\"  value=\"".$drow['time_1']."\" onchange=\"upperCase(this.id)\" /></div></td>";
  echo "<td width=\"110\"><div align=\"center\"><input name=\"time2_".$i."\" id=\"time2_".$i."\" type=\"text\" class=\"formText\" size=\"10\" maxlength=\"10\"  value=\"".$drow['time_2']."\" onchange=\"upperCase(this.id)\" /></div></td>";
  echo "<td width=\"110\"><div align=\"center\"><input name=\"time3_".$i."\" id=\"time3_".$i."\" type=\"text\" class=\"formText\" size=\"10\" maxlength=\"10\"  value=\"".$drow['time_3']."\" onchange=\"upperCase(this.id)\" /></div></td>";
  echo "<td width=\"110\"><div align=\"center\"><input name=\"time4_".$i."\" id=\"time4_".$i."\" type=\"text\" class=\"formText\" size=\"10\" maxlength=\"10\"  value=\"".$drow['time_4']."\" onchange=\"upperCase(this.id)\" /></div></td>";
  echo "<td width=\"90\"><div align=\"center\"><input name=\"stid_".$i."\" id=\"stid_".$i."\" type=\"hidden\" class=\"formText\" size=\"10\" maxlength=\"10\"  value=\"".$drow['id']."\" /></div></td>";
  echo "</tr>";
  }

I was thinking I could do something like this to update each individual record. The 'stid_0, stid_1, etc' identifies each record:

for ($i=0; $i<$stRows; $i++) {
$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_']['$i'].'\', `time_2` = \''.$_POST['time2_']['$i'].'\', `time_3` = \''.$_POST['time3_']['$i'].'\', `time_4` = \''.$_POST['time4_']['$i'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_']['$i'].'\'  LIMIT 1;';
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");
}

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Is there a specific problem you are encountering or question that you have? You have posted your intentions, but not a question.

Author

Commented:
The second loop to update the records is NOT WORKING. Is there a better way to handle this procedure?
>> is NOT WORKING

What happens? Do you receive a syntax error? Does it fail silently? What exactly is not working?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
If fails silently. Doesn't write to the database, doesn't do anything. I've attached the actual php file.
editEvent.txt
Well, the first thing I see from the code you posted is that $stRows is not defined. Therefore, the loop is interpreted as:

(for ($i=0; $i<0; $i++)
{
   ...
}

The loop starts at the exit condition, and therefore never runs.

Second, I see that you are not displaying any valid MySQL error data. You should make a modification such as the following so that, when you do get the loop to run, you will know exactly why the query fails if it fails:

mysql_query($tString) or die("Unable to update showtimes in [eventDates] database. The MySQL error was: " . mysql_error());

Author

Commented:
The $stRows is passed as a hidden input when the form is submitted. When I add > print_r($_POST) < the value is passed without any problems.

Author

Commented:
This is what I'm trying to replicate as a loop:

$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_0'].'\', `time_2` = \''.$_POST['time2_0'].'\', `time_3` = \''.$_POST['time3_0'].'\', `time_4` = \''.$_POST['time4_0'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_0'].'\' LIMIT 1;';
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");


$tString1 = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_1'].'\', `time_2` = \''.$_POST['time2_1'].'\', `time_3` = \''.$_POST['time3_1'].'\', `time_4` = \''.$_POST['time4_1'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_1'].'\' LIMIT 1;';
mysql_query($tString1) or die("Unable to update showtimes in [eventDates] database...");


etc...

Author

Commented:
Sorry, I miss typed. It should look like this:

$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_0'].'\', `time_2` = \''.$_POST['time2_0'].'\', `time_3` = \''.$_POST['time3_0'].'\', `time_4` = \''.$_POST['time4_0'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_0'].'\' LIMIT 1;';
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");

$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_1'].'\', `time_2` = \''.$_POST['time2_1'].'\', `time_3` = \''.$_POST['time3_1'].'\', `time_4` = \''.$_POST['time4_1'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_1'].'\' LIMIT 1;';
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");

$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_2'].'\', `time_2` = \''.$_POST['time2_2'].'\', `time_3` = \''.$_POST['time3_2'].'\', `time_4` = \''.$_POST['time4_2'].'\' WHERE `eventDates`.`id` = \''.$_POST['stid_2'].'\' LIMIT 1;';
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");

etc...
>> The $stRows is passed as a hidden input when the form is submitted

Unfortunately, it is not. Recent versions of PHP (assuming that you have a recent version) do not automatically create variables based on GET or POST parameters. This is referred to as global registration of variables, and is controlled by the register_globals setting in your php.ini file. In all versions of PHP since 4.2 have shipped with this set to off by default. You will need to explicitly create the variable, like this:

$stRows = $_POST['stRows'];

Author

Commented:
Thanks for the info.

For testing purposes, I've explicitly stated a value ij te for statement:

for ($i=0; $i<10; $i++) {

Again, it acts as though it is doing something, but it doesn't.

If I use the code as in the comment above (11.18.2008 at 02:02PM CST), it works fine. Problem is, sometimes there can be as many as 40 or so showtimes being updated at one time.

Ah, I see:

$_POST['time2_0'] is not equivalent to $_POST['time2_']['$i']

I think you mean something like this:

$_POST['time2_'.$i];

Note the concatenation operator (.) and lack of extra brackets.

Author

Commented:
That was it!! You rule.

Thanks a lot!

Author

Commented:
I've been working on this for almost two days. I knew it could be done, I just couldn't figure out the concatenation. Again, thanks!
Glad to help. Enjoy!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial