What is wrong with this code??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kendalleley
Forum Newbie
Posts: 2
Joined: Fri Jun 18, 2010 6:01 am

What is wrong with this code??

Post by kendalleley »

I am trying to get the following code to work. I think that it is a simple mistkae, but I am unable to find it. WHat I am trying to accomplish is to populate a form with data from a mysql table and then insert that date back into a mysql table.

Here is the code to poulate the form

Code: Select all

<?php
 $copy =0;
//Initialize counter variables

$index = 0;
$index_count = 0;

echo "<form method=post action='/associations/copy_girls_cross_country.php'>\n";
/*
Assuming we already have retrieved the records from the database into an array setting 
$myrow = mysql_fetch_array().  The do...while loop assigns a value to the $xstr variable 
by taking the name and concatenating the value of $index to the end starting with 0.  So 
the first time through the loop $SubmissionIDStr would have a value of SubmissionID0 the 
next time through it would be SubmissionID1 and so forth.
*/

do {

	$hometeamStr = hometeam.$index;
	$visitteamStr = visitteam.$index;
	$monthStr = month.$index;
	$dayStr = day.$index;
	$yearStr = year.$index;
	$hourStr = hour.$index;
	$minutesStr = minutes.$index;
	$ampmStr = ampm.$index;
	$team_id_numStr = team_id_num.$index;
	$assoc_id_numStr = assoc_id_num.$index;
	$user_id_numStr = user_id_num.$index;
	$sport_id_numStr = sport_id_num.$index;
	$cc_courseStr = cc_course.$index;

//This section would print the values onto the screen one record per row

printf("<input type=hidden name=%s value='%s'><input type=hidden name=%s value='%s'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value=%s size='5'><input type=hidden name=%s value='%s' size ='5'>", 
$hometeamStr, $row_boys_master["hometeam"], $visitteamStr, $row_boys_master["visitteam"], $monthStr, $row_boys_master["month"], $dayStr, $row_boys_master["day"], $yearStr, $row_boys_master["year"], $hourStr, $row_boys_master["hour"], $minutesStr, $row_boys_master["minutes"], $ampmStr, $row_boys_master["ampm"], $team_id_numStr, $row_boys_master["team_id_num"], $assoc_id_numStr, $row_boys_master["assoc_id_num"], $user_id_numStr, $row_boys_master['user_id_num'], $sport_id_numStr, '104', $cc_courseStr, $row_boys_master["cc_course"]);


//Increase counter values by 1 for each loop

$index++;
$index_count++;

} while ($row_boys_master = mysql_fetch_array($boys_master));

// I also had to create an index count to keep track of the total number of rows.
$copy=1;

echo "<INPUT TYPE=hidden NAME=counter VALUE=$index_count>\n";
echo "<INPUT TYPE=hidden NAME=copy VALUE=$copy>\n";

echo "<INPUT TYPE=submit value='Copy Schedule'></form>\n";
?>  


I have loaded the page, and everything appears to be populated correctly including the copy variable equaling one.

At the top of the page I have the following code to insert the date into a table.

Code: Select all

<?php
//This loops through all the records that have been displayed on the page.

for ($index = 0; $index <= $counter; $index++) {

    
    /*
    This part sets a variable with the names we created in the first section.  
    We start with 0 and go until the number saved in the $index_count variable.
    */ 
    
    $varhometeam = 'hometeam'.$index;
    $varvisitteam = 'visitteam'.$index;
    $varmonth = 'month'.$index;
    $varday = 'day'.$index;
    $varyear = 'year'.$index;
    $varhour = 'hour'.$index;
    $varminutes = 'minutes'.$index;
    $varampm = 'ampm'.$index;
    $varteam_id_num = 'team_id_num'.$index;
    $varassoc_id_num = 'assoc_id_num'.$index;
	$varuser_id_num = 'user_id_num'.$index;
 	$varsport_id_num = 'sport_id_num'.$index;
	$varcc_course = 'cc_course'.$index;
   
    /*
    This is the variable variable section.  We take the value that was assigned 
    to each name variable.  For example the first time through the loop we are 
    at the record assigned with SubmissionID0.  The value given to SubmissionID0 
    is set from the first section.  We access this value by taking the variable 
    variable of what SubmissionID0 is.
    */

    $hometeamvalue = $$varhometeam;
    $visitteamvalue = $$varvisitteam;
    $monthvalue = $$varmonth;
    $dayvalue = $$varday;
    $yearvalue = $$varyear;
    $hourvalue = $$varhour;
    $minutesvalue = $$varminutes;
    $ampmvalue = $$varampm;
    $team_id_numvalue = $$varteam_id_num;
    $assoc_id_numvalue = $$varassoc_id_num;
    $user_id_numvalue = $$varuser_id_num;
    $sport_id_numvalue = $$varsport_id_num;
	$cc_coursevalue = $$varcc_course;


    //Update the database
if ($copy=='1') {
    $sql = "insert into  schedules_2_teams_tbl (hometeam, visitteam, month, day, year, hour, minutes, ampm, team_id_num, assoc_id_num, user_id_num, sport_id_num, cc_course) values ('$hometeamvalue', '$visitteamvalue', '$monthvalue', '$dayvalue', '$yearvalue', '$hourvalue', '$minutesvalue', '$ampmvalue', '$team_id_numvalue', '$assoc_id_numvalue', '$user_id_numvalue', '$sport_id_numvalue','$cc_coursevalue')";
    $result = mysql_query($sql);
    $insertGoTo = "menu.php";
	header(sprintf("Location: %s", $insertGoTo));
}
}
?>
The problem appears to be that the if statement never gets executed. When I submit the page, it simply reloads the same page and does not go forward to the menu.php page. Please help!!
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: What is wrong with this code??

Post by hypedupdawg »

In the for loop, you use the variable $counter in the argument:

Code: Select all

for ($index  = 0;  $index <= $counter; $index++)
What is $counter? It has not been initialised, it is not incremented anywhere. Check that this is the argument you want to use. Maybe you mean one of your $index_count variables?
kendalleley
Forum Newbie
Posts: 2
Joined: Fri Jun 18, 2010 6:01 am

Re: What is wrong with this code??

Post by kendalleley »

In the code to populate the form about the 4th line from the bottom reads

Code: Select all

echo "<INPUT TYPE=hidden NAME=counter VALUE=$index_count>\n";
Shouldn't that be passed as $counter. When I look at the course code after the page is loaded, it is set to the number of rows that I want to copy.

As you can probalby tell I am far from an expert when it comes to code, but I copied the code from another page I have and it works perfectly to do the same task in a different table.
Post Reply