PHP错误:数组到字符串的转换

时间:2021-10-20 16:27:34

I have a PHP file named with Set of HTML codes, this code is to show several ID of location to be selected for the next process (Form is GET).

我有一个PHP文件,它是以HTML代码的集合命名的,这个代码是为了显示要为下一个进程选择的位置的几个ID(表单是GET)。

$sqloc = mysql_query("SELECT loc_id FROM location");
while ($row = mysql_fetch_array($sqloc)){

echo "<tr><td>
    <label><input type=\"checkbox\" name=\"chk_loc[]\" value=". $row['loc_id'] ." />
</td><td>" . $row['loc_id'] . "</td></tr></label>"; }

Then in other PHP file i use this code to select data based on selected ID using checkbox before.

然后,在其他PHP文件中,我使用此代码使用复选框根据所选的ID选择数据。

$cbarray = array();
if (isset($_GET['submit_location'])) {
  $cbarray = $_GET['chk_loc']; }

for ($i=0; $i<count($cbarray); $i++) {
  $sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'");
      while($ccb= mysql_fetch_row($sqlcb)) {
                print_r($ccb); }
}

But when i run it, it appear the notice :

但当我运行它时,它显示了通知:

Array to string conversion in .... on line 62

在....数组字符串转换在第62行

On line 62 which is on ($sqlcb = mysql_query) part. I already use var_dump to check the array, and it print the array like this :

在第62行($sqlcb = mysql_query)部分上。我已经使用var_dump来检查数组,它输出的数组如下:

array(4) { [0]=> string(5) "LO001" [1]=> string(5) "LO003" [2]=> string(5) "LO004" [3]=> string(5) "LO005" } 

Is there anyway to solve this problem? thank you.

有办法解决这个问题吗?谢谢你!

2 个解决方案

#1


3  

The Checkbox in php is processed as a array. Here in the code the checkbox with id chk_loc is stored in $cbarray as a array. $sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'"); in this code the where clause accept a string and you are providing a array.

php中的复选框作为数组进行处理。在代码中,id为chk_loc的复选框作为数组存储在$cbarray中。$sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'");在这段代码中,where子句接受一个字符串并提供一个数组。

And use PDO to prevent sql injection PDO PHP

使用PDO来防止sql注入PDO PHP

#2


1  

The problem is because of this statement,

问题在于,

$sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'");
                                                                 ^ see here

$cbarray is actually an array, not a string. You can see it's structure using var_dump($cbarray);.

$cbarray实际上是一个数组,而不是字符串。您可以看到使用var_dump($cbarray)的结构;

So the solution is:

所以解决方法是:

Use implode() function to join the array elements with a string and use it in your query, like this:

使用内爆()函数将数组元素与字符串连接并在查询中使用,如下所示:

$cbarray = array();
if (isset($_GET['submit_location'])) {
    $cbarray = $_GET['chk_loc'];

    $query = "SELECT * FROM location WHERE loc_id IN ('" . implode("','", array_map('mysql_real_escape_string', $cbarray)) . "')";
    $sqlcb = mysql_query($query);
    while($ccb= mysql_fetch_row($sqlcb)) {
        print_r($ccb); 
    }
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Sidenote: Don't use mysql_*函数,它们在PHP 5.5时被弃用,在PHP 7.0中被全部删除。使用mysqli或pdo代替。这就是为什么不应该使用mysql_*函数。

#1


3  

The Checkbox in php is processed as a array. Here in the code the checkbox with id chk_loc is stored in $cbarray as a array. $sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'"); in this code the where clause accept a string and you are providing a array.

php中的复选框作为数组进行处理。在代码中,id为chk_loc的复选框作为数组存储在$cbarray中。$sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'");在这段代码中,where子句接受一个字符串并提供一个数组。

And use PDO to prevent sql injection PDO PHP

使用PDO来防止sql注入PDO PHP

#2


1  

The problem is because of this statement,

问题在于,

$sqlcb = mysql_query("SELECT * FROM location WHERE loc_id = '$cbarray'");
                                                                 ^ see here

$cbarray is actually an array, not a string. You can see it's structure using var_dump($cbarray);.

$cbarray实际上是一个数组,而不是字符串。您可以看到使用var_dump($cbarray)的结构;

So the solution is:

所以解决方法是:

Use implode() function to join the array elements with a string and use it in your query, like this:

使用内爆()函数将数组元素与字符串连接并在查询中使用,如下所示:

$cbarray = array();
if (isset($_GET['submit_location'])) {
    $cbarray = $_GET['chk_loc'];

    $query = "SELECT * FROM location WHERE loc_id IN ('" . implode("','", array_map('mysql_real_escape_string', $cbarray)) . "')";
    $sqlcb = mysql_query($query);
    while($ccb= mysql_fetch_row($sqlcb)) {
        print_r($ccb); 
    }
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Sidenote: Don't use mysql_*函数,它们在PHP 5.5时被弃用,在PHP 7.0中被全部删除。使用mysqli或pdo代替。这就是为什么不应该使用mysql_*函数。