kenfx
asked on
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['eventD ate']))."< /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...");
}
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['eventD
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'].
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'].
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'].
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'].
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']
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");
}
Is there a specific problem you are encountering or question that you have? You have posted your intentions, but not a question.
ASKER
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?
What happens? Do you receive a syntax error? Does it fail silently? What exactly is not working?
ASKER
If fails silently. Doesn't write to the database, doesn't do anything. I've attached the actual php file.
editEvent.txt
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());
(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());
ASKER
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.
ASKER
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...
$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_0'].'\',
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");
$tString1 = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_1'].'\',
mysql_query($tString1) or die("Unable to update showtimes in [eventDates] database...");
etc...
ASKER
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...
$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_0'].'\',
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");
$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_1'].'\',
mysql_query($tString) or die("Unable to update showtimes in [eventDates] database...");
$tString = 'UPDATE `majestic`.`eventDates` SET `time_1` = \''.$_POST['time1_2'].'\',
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'];
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'];
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That was it!! You rule.
Thanks a lot!
Thanks a lot!
ASKER
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!