Louie Heaton
Louie Heaton

Reputation: 25

Inserting an Array from HTML form into MYSQL Database

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.

Upvotes: 1

Views: 146

Answers (1)

Henry
Henry

Reputation: 1270

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

Upvotes: 1

Related Questions