Page 1 of 1

Insert SQL Statement from a POST array

Posted: Thu Mar 12, 2009 7:03 am
by mattywarr
Hi Guys,

I've been racking my brains for days trying to work out this problem to no avail. I wonder if someone here can identify where i'm going wrong.

I've got 2 functions. One of them produces a list of Players from a team for the user to select from. Its designed to record statistics so it produces a list for each stat, depending on how many there are (Goals for example)

This produces a form field.

Then i submit the form and have another function whichs process and inserts the array into the database.

it works for one type, but not the other.

Here is the code.

RESULT PLAYERS FUNCTION

Code: Select all

function resultplayers($stat,$statnum,$teamid,$game) 
{
 
$num=1;
 
//$statnum is the number of goals submitted fromthe previous form, so it produces a drop down list for each goal scored.
 
    while ($num <= $statnum)    
    {
     $selectname="$stat";
     ?><select name="<? echo $selectname; ?>[<? echo $num; ?>]" class='loginboxes'><?
     echo "<option value=0>Unknown</option>\n";
    $q_teamname = mysql_query("
    SELECT teamname from dbTeams where id=$teamid and game='$game'") or die ("Cannot Load Team ID");
 
            if ($name = mysql_fetch_row($q_teamname)) 
            { 
            $teamnamepell = $name[0]; 
            } 
            else    
            { 
            echo "Cannot Load Team Name"; 
            } 
 
    $q_getplayers = mysql_query("
    select 
    dbPlayers.id, 
    dbPlayers.name,
    dbTeams.teamname 
    from dbPlayers 
    INNER JOIN dbTeams ON dbPlayers.pellukclubteam = dbTeams.teamname 
    where dbTeams.id = $teamid and dbPlayers.game='$game' order by dbPlayers.name ;") or die ("Bad Query Player Load");
       while ($getplayers = mysql_fetch_array($q_getplayers, MYSQL_NUM)) 
   {
            
        
            echo "<option value=$getplayers[0]>$getplayers[1]</option>\n";
            
           }
 
         echo "</select><br>\n";
 
    
    $num = $num+1;
    }
}
This generates the following HTML. The option values are player ids

Code: Select all

<select name="homegoal[1]" class='loginboxes'><option value=0>Unknown</option>
<option value=45617>Andres Guardado</option>
<option value=45315>Baraja</option>
 
<option value=45283>Benjani</option>
<option value=44732>Dean Ashton</option>
<option value=46409>Dede</option>
<option value=45108>Dickson Etuhu</option>
<option value=44973>Didier Zokora</option>
<option value=44864>Habib Beye</option>
<option value=44751>Javier Pedro Saviola</option>
<option value=44818>Juan Sebastian Veron</option>
<option value=45222>Juanito</option>
 
<option value=45204>Kolo Toure</option>
<option value=45188>Manuel Almunia</option>
<option value=46365>Massimo Oddo</option>
<option value=46626>Naldo</option>
<option value=44981>Pascal Chimbonda</option>
<option value=44771>Ricardo</option>
<option value=44778>Ricardo Carvalho</option>
<option value=45292>Roberto Carlos Abbondanzieri</option>
<option value=45484>Salomon Kalou</option>
 
<option value=45455>Scott McDonald</option>
<option value=45056>Tim Cahill</option>
<option value=46462>Tranquillo Barnetta</option>
</select>
 
<select name="homeyellow[1]" class='loginboxes'><option value=0>Unknown</option>
<option value=45617>Andres Guardado</option>
<option value=45315>Baraja</option>
<option value=45283>Benjani</option>
<option value=44732>Dean Ashton</option>
<option value=46409>Dede</option>
<option value=45108>Dickson Etuhu</option>
<option value=44973>Didier Zokora</option>
 
<option value=44864>Habib Beye</option>
<option value=44751>Javier Pedro Saviola</option>
<option value=44818>Juan Sebastian Veron</option>
<option value=45222>Juanito</option>
<option value=45204>Kolo Toure</option>
<option value=45188>Manuel Almunia</option>
<option value=46365>Massimo Oddo</option>
<option value=46626>Naldo</option>
<option value=44981>Pascal Chimbonda</option>
 
<option value=44771>Ricardo</option>
<option value=44778>Ricardo Carvalho</option>
<option value=45292>Roberto Carlos Abbondanzieri</option>
<option value=45484>Salomon Kalou</option>
<option value=45455>Scott McDonald</option>
<option value=45056>Tim Cahill</option>
<option value=46462>Tranquillo Barnetta</option>
</select>
 
 
I POST this to the next page, and while the function WORKS for the select array called "Homegoal" it does NOT work for "HomeYellow"

Here is the function

Code: Select all

 
function insertplayerstat($teamid,$fixtureid,$homeoraway,$type,$game,$statmax,$statname) 
{
echo $_POST['awayred[1]'];
 
 
$num=1;
 
while ($num <= $statmax)
 
{ 
$goal='N';
$assist='N';
$yellow='N';
$red='N';
$dispoints=0;
 
if ($type=='G') { $goal='Y'; } 
if ($type=='A') { $assist='Y'; } 
if ($type=='Y') { $yellow='Y'; $dispoints=1; } 
if ($type=='R') { $red='Y'; $dispoints=3; } 
 
 
echo $_POST['homeyellow'];
 
if ($goal=='Y' && $homeoraway=='H') { $playerid=$_POST['homegoal'][$num]; }
if ($goal=='Y' && $homeoraway=='A') { $playerid=$_POST['awaygoal'][$num]; }
if ($assist=='Y' && $homeoraway=='H') { $playerid=$_POST['homeassist'][$num]; }
if ($assist=='Y' && $homeoraway=='A') { $playerid=$_POST['awayassist'][$num]; }
 
if ($yellow=='Y' && $homeoraway=='H') { $playerid=$_POST['homeyellow'][$num]; }
 
 
$insert = "INSERT INTO dbPlayerStats VALUES 
(0,$teamid,$fixtureid,'$homeoraway',$playerid,'$goal','$assist','$yellow','$red',$dispoints,1,0,'$game')";
 
        if ($result = mysql_query($insert))
        {
         echo "Insert OK<br><br>";
        }
        else
        {
         echo "Insert Failed $insert<br><br>";
        }
$num=$num+1;
    }
 
}
 
The problem is, the Insert query won;t run because it can;t obtain the $playerid value from the $_POST['homeyellow'][$num];

It DOES work using $_POST['homegoal'][$num];

So I am completely stumped!

ANY help or ideas would be appreciated.

Re: Insert SQL Statement from a POST array

Posted: Thu Mar 12, 2009 3:17 pm
by sparrrow
Have you tried echoing $_POST['homeyellow'][1] or doing a print_r($_POST['homeyellow']) ? You could also print_r($_POST) if the first two attempts are blank, to see if it is coming through there.

I don't know what the rest of your code looks like, but also make sure you are containing the form element inside the form tags correctly. I've made a mistake before of adding a new field to a form, but adding it AFTER the </form>, which caused your exact problem.