Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Unknown column 'Array' in WHERE clause –  Timothy Coetzee Oct 10 '13 at 11:55
    
Which one is line 37? –  Amal Murali Oct 10 '13 at 11:56
    
show the var_dump of implode(',', $player_ids) –  Sebas Oct 10 '13 at 11:56
    
which is line 37? –  Yogesh Suthar Oct 10 '13 at 11:56
3  
"WHERE fixture_id = $opponents_id". $opponents_id is an array, too. –  bitWorking Oct 10 '13 at 11:59

4 Answers 4

up vote 3 down vote accepted

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/

share|improve this answer
    
Thank you sooo much for the help –  Timothy Coetzee Oct 10 '13 at 12:03
    
No worries. Good luck on your project. –  cillosis Oct 10 '13 at 12:05
    
can you please have a quick look at the code for your solution. There is a syntax error. I cant spot it... –  Timothy Coetzee Oct 10 '13 at 12:09
    
Sorry, forgot to add the right parenthesis wrapped around the query string. Fixed it. –  cillosis Oct 10 '13 at 12:17

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()); 
share|improve this answer

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.

share|improve this answer

you did not specified Array properly in sql query.

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

share|improve this answer
    
Do you have any suggestions on how to specify it properly? –  James A Mohler Dec 28 '13 at 5:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.