$_POST array problems

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
wabu
Forum Newbie
Posts: 1
Joined: Fri Mar 21, 2008 6:07 am

$_POST array problems

Post by wabu »

Hello

I am trying to develop in php a website where friends can predict football/soccer scores and save them to a mysql database.

My problem is getting all the variables out of the $_POST array when passing from one page to another.

I have my first page called 'prophecy' where people can enter the scores in a form, method 'post'. I can retrieve the data on the confirmation page called 'score_confirm', but when I get them to press the save button I use another variable called action which is supposed to trigger an if clause that writes back to the database but it doesn't work and I can't see where I'm going wrong.

Its a multidimensional array as they are entering more than one score along with a hidden game id.

Here is the code from my first page 'prophecy':

Code: Select all

<php?
                // temporarily set game week
                $week = '1';
                
                echo "<p>Score Prophecy for Week $week:<br><br>";
                
                // 1. Get week's game details from game table.
 
                $sql_game = "SELECT game_id, game_team1, game_team2, game_date, game_time from game WHERE game_week='$week' ORDER BY game_date, game_time";
                $sql_game_result = @mysql_query($sql_game,$connection) or die("Error");
          
            echo "<form action=\"score_confirm.php\" method=\"post\">"; 
         
            while ( $game = mysql_fetch_array($sql_game_result))
        
            {echo "$game[game_team1] v $game[game_team2]  <span class=\"info\">$game[game_time]</span> Score: <input name=\"$game[game_id][]\" type=\"text\" size=4 maxlength=5><br><br> ";
            }
              
          ?>
          <input type="submit" value="Save Scores" /> </form>

I use the game_id as the array key for the predicted score which is stored as the value. Here is the code for the result page 'score_confirm:

Code: Select all

<?
        // temporarily set game week
            $week = '1';
 
            // Request the variable that determins what actions are carried out by the page.
                $action = $_REQUEST['action'];
 
$sql_game = "SELECT game_id, game_team1, game_team2, game_date, game_time from game WHERE game_week='$week' ORDER BY game_date, game_time";
                $sql_game_result = @mysql_query($sql_game,$connection) or die("Error");
 
                //Show games listed
 
                while ( $game = mysql_fetch_array($sql_game_result))
 
                {echo "$game[game_team1] v $game[game_team2]  <span class=\"info\">$game[game_time]</span><br><br>";
                }
 
    if ($action == "verify")
        {
            //1. Get scores submitted
 
                    foreach ($_POST as $key => $value)
                        {
                        foreach ($_POST[$key] as $value)
                        {echo "$value null<br><br>";
                        $key++;
                        }
                        }
 
        echo "<form action=\"prophecy.php\"><input type=\"submit\" value=\"Back\" /> </form></p> ";
 
        }
 
    if ($action == "")
    {
 
        echo "<br><p><strong>Score Prophecy for Week $week:</strong></p>" ;
 
            echo "<form action=\"score_confirm.php\">";
            echo "<input type=\"hidden\" name=\"action\" value=\"verify\">";
 
          // 1. Get scores submitted
 
                    foreach ($_POST as $key => $value)
                        {
                        foreach ($_POST[$key] as $value)
                        {echo "$value verify<br><br>";
                        $key++;
                        }
                        }
 
            echo "<input type=\"submit\" value=\"Save Scores\" /> </form></p> ";
 
        }
 
        
 
        echo "action= $action ";
 
        ?>
When I set the action to verify it should show the games to be inserted in db (I have left out my insert script as I know that bit works), but it doesn't, I think its because of the $action variable but don't know how else to do it.

Any help would be much appreciated, I am very new to php and have struggled to find info on how to reference multidimensional arrays out of $_POST.

Thanks

Wabu
Post Reply