2

My page displays the name of players of a certain sports team using drop down menus. The coach can log in to pick his team and select the opposition which his team will play against. When user has selected the team and opposition he clicks submit and the isset function is triggered.

Now I capture the values from the drop down menus and upload it to the correct table in the DB. Everything is pretty straight forward however when I click submit I get the error in the tittle. Any help would be appreciated

if ( isset($_POST['submit']) ) {
    $player_ids = array_map('intval', $_REQUEST['players']);
    $opponents_id = $_REQUEST['players'];

    var_dump($player_ids);
    var_dump($opponents_id);

    $query = 'SELECT `name`, `position` 
        FROM `player_info` 
        WHERE `player_id` IN (' . implode(',', $player_ids) . ')';

    $return_names = mysql_query($query) or die(mysql_error());

         while ( $row = mysql_fetch_assoc($return_names) ) 
        {
            $selected[] = $row['name'];
            $position[] = $row['position'];
        }

    $query = ("SELECT `fixture_id` 
                FROM `fixtures`     
                WHERE `fixture_id` = $opponents_id") 
                or die (mysql_error()); 

    $result = mysql_query($query) or die(mysql_error());

                while ($row = mysql_fetch_array($query))
                {
                    $fixture_id[] = $row['fixture_id']; 

                }
                        for ($i=0; sizeof($selected) > $i; $i++){
                             $sql = mysql_query("INSERT INTO `team` (`selection_id`, `fixture_id`, `player_position`,`player_name`) 
                                                VALUES ('$fixture_id[$i]','$position[$i]','$selected[$i]')") 
                                                or die(mysql_error());
                                echo $selected[$i]; 
                                echo $position[$i];
                                echo $fixture_id[$i];
                                echo'<br>';


}       

enter image description here

enter image description here

10
  • Unknown column 'Array' in WHERE clause Commented Oct 10, 2013 at 11:55
  • Which one is line 37? Commented Oct 10, 2013 at 11:56
  • show the var_dump of implode(',', $player_ids) Commented Oct 10, 2013 at 11:56
  • Don't you have a missing value in your insert? You want to populate 4 columns in the INSERT, but you provide only 3 values - value for SELECTION_ID is missing. Commented Oct 10, 2013 at 11:57
  • 3
    "WHERE fixture_id = $opponents_id". $opponents_id is an array, too. Commented Oct 10, 2013 at 11:59

4 Answers 4

3

The Unknown column 'Array' in 'where clause' error means literally what it says -- you tried to put an array value into your where clause.

In this line:

$query = ("SELECT `fixture_id` 
            FROM `fixtures`     
            WHERE `fixture_id` = $opponents_id") 
            or die (mysql_error());

You are using the $opponents_id variable which your var_dump shows is an array containing five values. So, you need to use the IN clause and list them out (just like you did for $player_ids):

$query = ("SELECT `fixture_id` 
            FROM `fixtures`     
            WHERE `fixture_id` IN (" . implode(",", $opponents_id) . ");") 
            or die (mysql_error());

Note: Hopefully you are aware of the tiring subject of the mysql_* family of functions. These are being deprecated and are insecure. I wrote about it a while back: http://www.jorble.com/2012/06/you-are-vulnerable-for-sql-injection/

Sign up to request clarification or add additional context in comments.

3 Comments

No worries. Good luck on your project.
can you please have a quick look at the code for your solution. There is a syntax error. I cant spot it...
Sorry, forgot to add the right parenthesis wrapped around the query string. Fixed it.
1

bodi0 is right, your $opponents_id is an array , if it must be an array so do some things like that

$opponents_id_text=implode(',',$opponents_id);
$query = ("SELECT `fixture_id` 
                FROM `fixtures`     
                WHERE `fixture_id` in ($opponents_id_text)") 
                or die (mysql_error()); 

Comments

0

The problem is in your second SQL query:

WHERE `fixture_id` = $opponents_id" - 

Here the $opponents_id is array (it is converted automatically to array when you assign the value to it from the $_REQUEST, because the HTML component select sends the options as array). Just implode() it also, like you did for $player_ids.

Comments

-1

you did not specified Array properly in sql query.

when we use Arrays we have to specify array number in query

1 Comment

Do you have any suggestions on how to specify it properly?

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.