PHP $_POST Loop with Variables

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...");
}

kenfxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

glcumminsCommented:
Is there a specific problem you are encountering or question that you have? You have posted your intentions, but not a question.
0
kenfxAuthor Commented:
The second loop to update the records is NOT WORKING. Is there a better way to handle this procedure?
0
glcumminsCommented:
>> is NOT WORKING

What happens? Do you receive a syntax error? Does it fail silently? What exactly is not working?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kenfxAuthor Commented:
If fails silently. Doesn't write to the database, doesn't do anything. I've attached the actual php file.
editEvent.txt
0
glcumminsCommented:
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());
0
kenfxAuthor 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.
0
kenfxAuthor 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...
0
kenfxAuthor 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...
0
glcumminsCommented:
>> 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'];
0
kenfxAuthor 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.

0
glcumminsCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kenfxAuthor Commented:
That was it!! You rule.

Thanks a lot!
0
kenfxAuthor 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!
0
glcumminsCommented:
Glad to help. Enjoy!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.