1

So, I have my code here but when I go to insert it, it only inserts the last value of the array. The form starts after //CREATE THE MATRIX OF MATCHUPS in the document.

    echo "<pre>";

// DISTRIBUTE TEAMS INTO CONTESTS
$fixtures = mysqli_query($link, "SELECT teamname FROM tourn_teams WHERE groupname='Group 1'");

$teams = array();
// THE TEAMS
while($row = mysqli_fetch_assoc($fixtures))
{ 

  $teams[] = $row['teamname'];

}

// HOW MANY WEEKS
$weeks = 3;

// MAKE ENOUGH ARRAY ELEMENTS FOR THE DISTRIBUTION
$array = array_merge($teams, $teams);


// POPULATE THE MATCHES ARRAY
$matches = array();
while ($weeks)
{
    foreach ($teams as $ptr => $team)
    {
        // FIND THE INDEX INTO THE DISTRIBUTION ARRAY
        $linkt = $ptr + $weeks;

        // SELECT THE HOME AND AWAY TEAMS
        $home = $team;
        $away = $array[$linkt];
        $matches[$team][$weeks] = array('home' => $home, 'away' => $away);
    }

    // NEXT WEEK
    $weeks--;
}


// SORT THE MATCHES SENSIBLY SO WEEK ONE COMES FIRST
foreach ($matches as $team => $contests)
{
    ksort($contests);
    $matches[$team] = $contests;
}


// ACTIVATE THIS TO SEE WHAT THE $matches ARRAY LOOKS LIKE
// print_r($matches);


// CREATE THE TABLE OF MATCHUPS
$out = NULL;
$out .= "<table>";
$out .= PHP_EOL;


// CREATE THE HEADERS FOR EACH WEEK
$weeknums = end($matches);

$out .= "<tr>";
$out .= '<th> Team </th>';
$out .= '<th> v </th>';
$out .= "<th> Team </th>";
$out .= '</tr>';
$out .= PHP_EOL;
// CREATE THE MATRIX OF MATCHUPS
foreach ($matches as $team => $contests)
{
    $out .= "<form class='form-horizontal' action='".$_SERVER['PHP_SELF']."'d method='post'><tr><td><input type='text' name='teamone' value='$team' readonly></td>";
    $out .= "<td> <b>v</b></td>";
    foreach ($contests as $week => $matchup)
    {
        $out .= "<td> <input type='text' name='teamtwo' value='{$matchup["away"]}' readonly> </td>";
    }
    $out .= "</tr>";
    $out .= PHP_EOL;
}
$out .= "<input class='btn btn-primary' type='submit' name='submit'></form></table>";
$out .= PHP_EOL;

$sql = "INSERT INTO tourn_fixtures (teamone, teamtwo) VALUES ('".$_POST['teamone']."', '".$_POST['teamtwo']."')";

if(mysqli_query($link, $sql)){
    echo "Records inserted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

echo "</pre>";
echo $out;

?>

Output

Database

So as you can see, the output of the array in a table works and outputs correctly in the table and generates fixtures but when I then go and try and input them into the database only the last value of the array gets inputed whereas I'm trying to get all of the fixtures inserted in separate rows.

2
  • try implode(',',$_POST['teamone']) Commented Jan 1, 2020 at 10:53
  • @Gulshan Thanks but not working. Correct me if I'm incorrect but this is how I'm using it. $sql = "INSERT INTO tourn_fixtures (teamone, teamtwo) VALUES ('".implode(',',$_POST['teamone'])."', '".$_POST['teamtwo']."')"; Commented Jan 1, 2020 at 11:37

1 Answer 1

1

I wrote a function for this. Feel free to use and adapt it. It is part of a class that also handles the query reqeust. So you need to change where $this->query($query); is.Make sure the array is safe to insert before you insert it. Never insert raw post data.

public function insert_array( $tabel = '', $arr = array() ) {
    // make sure its an array
    $arr_velden = (array) $arr;

    // write array to table
    $query = "
        INSERT INTO
            $tabel (
                " . implode(',', array_keys($arr_velden)) . "
            )
        VALUES (
                '" . implode("','", array_values($arr_velden)) . "'
            )";
    return $this->query($query);

} // end function
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Henry, Thanks for response. My first time working with arrays so a bit confused on how to implement it. public function insert_array( $tabel = 'tourn_fixtures', $arr = array() ) { // make sure its an array $arr_velden = (array) $arr; // write array to table $query = " INSERT INTO $tabel ( " . implode(',', array_keys($arr_velden)) . " ) VALUES ( " . implode("','". $_POST['teamone'] . " )"; return $this->query($query); } // end function This is what I've wrote
I'm guessing its wrong as page isn't working, not too sure how I would fix this...
The keys must reflect the database fields exactly, forget to mention that. You use the keys from $arr_velden and the values from $_POST['teamone'] and is this an array or just one value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.