?
Solved

PHP $_POST Loop with Variables

Posted on 2008-11-18
14
Medium Priority
?
1,277 Views
Last Modified: 2013-12-13
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...");
}

0
Comment
Question by:kenfx
  • 8
  • 6
14 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 22986757
Is there a specific problem you are encountering or question that you have? You have posted your intentions, but not a question.
0
 

Author Comment

by:kenfx
ID: 22986884
The second loop to update the records is NOT WORKING. Is there a better way to handle this procedure?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22986898
>> is NOT WORKING

What happens? Do you receive a syntax error? Does it fail silently? What exactly is not working?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:kenfx
ID: 22987218
If fails silently. Doesn't write to the database, doesn't do anything. I've attached the actual php file.
editEvent.txt
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22987904
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
 

Author Comment

by:kenfx
ID: 22988131
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
 

Author Comment

by:kenfx
ID: 22988159
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
 

Author Comment

by:kenfx
ID: 22988197
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
 
LVL 24

Expert Comment

by:glcummins
ID: 22988267
>> 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
 

Author Comment

by:kenfx
ID: 22988420
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
 
LVL 24

Accepted Solution

by:
glcummins earned 2000 total points
ID: 22988535
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
 

Author Comment

by:kenfx
ID: 22988638
That was it!! You rule.

Thanks a lot!
0
 

Author Closing Comment

by:kenfx
ID: 31517926
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
 
LVL 24

Expert Comment

by:glcummins
ID: 22988708
Glad to help. Enjoy!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question