Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:10:33+00:00 2026-06-08T22:10:33+00:00

I’m having some trouble with a form designed to insert fixtures into a database

  • 0

I’m having some trouble with a form designed to insert fixtures into a database for a sports league.

The previous page contains a form which defines through _POST “area”/”division”/”season” and contains an iterated (to ten) loop of a group of fixture fields, which are defined as having arrayed names like “hometeam[$i]”, “awayteam[$i]” and so forth.

On the page which processes the input of the fixtures, I need to pull each individual iteration of the form – i.e. “hometeam[1]” “awayteam[1]” and so on – from within the $_POST array and insert that into the MYSQL database before moving onto the next one. I’m totally in over my head with foreach and if clauses so I thought it best to ask here.

<?php
$a=1;
while($a<11){
foreach($_POST as $key){
if(is_array($key))
{foreach($key as $value1=>$value2){
    if($value1=$a){
    if($_POST='hometeam')$home_id=$value2;
    else if($_POST='awayteam')$away_id=$value2;
    else if($_POST='day')$day=$value2;
    else if($_POST='month')$month=$value2;
    else if($_POST='year')$year=$value2;
    else if($_POST='hour')$hour=$value2;
    else if($_POST='mins')$mins=$value2;}
    $date = ($year . "-" . $month . "-" . $day);
    $time = (($hour) . ":" . ($mins) . ":00");
    $enter_query = "INSERT INTO matches (home_id, away_id, date, time, league_id) VALUES ('$home_id', '$away_id', '$date', '$time', '$league_id'";
    if($hour != "00"){
$enter_result = mysql_query($enter_query);
}}}

}
$a++;
}
?>

If anyone can point at where I’m blatantly going wrong here, I’d be immensely grateful. I’m sure it’s not meant to be this hard but I’m flustered and can’t see past what I’ve typed, and I’m not sure my knowledge is accurate as-is.

Running a print_r on the array gives out the following structure:

Array ( [area] => 1 [season] => 2 [division] => 1 [hometeam]
=> Array ( [1] => 17 [2] => 2 [3] => 12 [4] => 17 [5] => 17
[6] => 17 [7] => 17 [8] => 17 [9] => 17 [10] => 17 ) [awayteam]
=> Array ( [1] => 6 [2] => 4 [3] => 10 [4] => 17 [5]

where the same 10fold structure continues with arrays indexed as hometeam, awayteam, day, month, year, hour, mins.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:10:35+00:00Added an answer on June 8, 2026 at 10:10 pm

    $_POST is an associative array so its values are accessed through key names, for example $_POST['hometeam']. This would contain an array of each of the hometeam[] values.

    The second thing I see is that you need to remember that the = operator is for assigning a value, and == is for comparison. So if ($value = $a) would assign $a to $value and return the result of that, not a logical test.

    You should considered replacing the big chain of if/else with a select block. But that’s optional and I’ve left that as is from your example.

    My final suggestion is that whist you don’t have to have whitespace, it does make it a lot easier to review code when it’s not working properly.

    I haven’t tested this code but this is how I would rework what you have.

    <?php
        $a=1;
        while ($a<11) {
            foreach ($_POST as $key => $value) {
                if (isarray($value)) {
                    foreach ($value as $value1 => $value2) {
                        if ($value1 == $a){
                            if ($key == 'hometeam') { $home_id = $value2; }
                            else if($key == 'awayteam') { $away_id = $value2; }
                            else if($key == 'day') { $day = $value2; }
                            else if($key == 'month') { $month = $value2; }
                            else if($key == 'year') { $year = $value2; }
                            else if($key == 'hour') { $hour = $value2; }
                            else if($key == 'mins'){ $mins = $value2;}
                            $date = ($year . "-" . $month . "-" . $day);
                            $time = (($hour) . ":" . ($mins) . ":00");
                            $enter_query = "INSERT INTO matches (home_id, away_id, date, time, league_id) VALUES ('$home_id', '$away_id', '$date', '$time', '$league_id')";
                            if ($hour != "00"){
                                $enter_result = mysql_query($enter_query);
                            }
                    }
                }
    
            }
            $a++;
        }
    ?>
    

    I hope this gets you on the right track at least.

    EDIT: I’ve refactored this down a bit. It doesn’t do any checking of what is returned on the form (which is a security issue) but that’s outside the scope of this question..

    <?php
        $a=1;
        while ($a<11) {
    
            $date = ($_POST['day'][$a] "-" . $_POST['month'][$a] . "-" . $_POST['day'][$a]);
            $time = ($_POST['hour'][$a] . ":" . $_POST['mins'][$a] . ":00");
            $hometeam = $_POST['hometeam'][$a];
            $awayteam = $_POST['awayteam'][$a];
            $enter_query = "INSERT INTO matches (home_id, away_id, date, time, league_id) VALUES ('$hometeam', '$awayteam', '$date', '$time', '$league_id')";
            if ($_POST['hour'][$a] != "00"){
                $enter_result = mysql_query($enter_query);
            }
            $a++;
        }
    ?>
    

    I can’t see where league_id comes from. If it is a part of $_POST you can reference it in the same way (if it is not an array then don’t use the [$a]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.