PHP SQL查询错误 - WHERE子句中的未知数组列

时间:2021-10-21 11:49:10

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.

我的页面使用下拉菜单显示某个运动队的球员姓名。教练可以登录选择他的球队并选择他的球队将要对抗的对手。当用户选择了团队并且反对时,他点击提交并触发了isset功能。

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

现在我从下拉菜单中捕获值并将其上传到DB中的正确表。一切都很直接但是当我点击提交时,我得到了错误。任何帮助,将不胜感激

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>';


}       

PHP SQL查询错误 -  WHERE子句中的未知数组列

PHP SQL查询错误 -  WHERE子句中的未知数组列

4 个解决方案

#1


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.

'where子句'错误中的未知列'Array'意味着字面意思 - 您试图将数组值放入where子句中。

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):

您正在使用$ opponents_id变量,var_dump显示的变量是包含五个值的数组。因此,您需要使用IN子句并将其列出(就像您对$ 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/

注意:希望您了解mysql_ *系列函数的累人主题。这些都被弃用并且不安全。我曾经写过这篇文章:http://www.jorble.com/2012/06/you-are-vulnerable-for-sql-injection/

#2


1  

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

bodi0是对的,你的$ opponents_id是一个数组,如果它必须是一个数组,所以做一些这样的事情

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

#3


0  

The problem is in your second SQL query:

问题出在你的第二个SQL查询中:

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.

这里$ opponents_id是数组(当你从$ _REQUEST为它分配值时,它会自动转换为数组,因为HTML组件选择将选项作为数组发送)。只是implode()它也像你为$ player_ids所做的那样。

#4


-1  

you did not specified Array properly in sql query.

你没有在sql查询中正确指定数组。

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

当我们使用Arrays时,我们必须在查询中指定数组编号

#1


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.

'where子句'错误中的未知列'Array'意味着字面意思 - 您试图将数组值放入where子句中。

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):

您正在使用$ opponents_id变量,var_dump显示的变量是包含五个值的数组。因此,您需要使用IN子句并将其列出(就像您对$ 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/

注意:希望您了解mysql_ *系列函数的累人主题。这些都被弃用并且不安全。我曾经写过这篇文章:http://www.jorble.com/2012/06/you-are-vulnerable-for-sql-injection/

#2


1  

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

bodi0是对的,你的$ opponents_id是一个数组,如果它必须是一个数组,所以做一些这样的事情

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

#3


0  

The problem is in your second SQL query:

问题出在你的第二个SQL查询中:

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.

这里$ opponents_id是数组(当你从$ _REQUEST为它分配值时,它会自动转换为数组,因为HTML组件选择将选项作为数组发送)。只是implode()它也像你为$ player_ids所做的那样。

#4


-1  

you did not specified Array properly in sql query.

你没有在sql查询中正确指定数组。

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

当我们使用Arrays时,我们必须在查询中指定数组编号