注意:使用update语句进行数组到字符串转换

Im trying to create a system to active or inactive my posts.

First I get my sts from my url and I store in my $status.

Then if $status == 0 I will transform my post to active and if $status == 1 I´ll turn my post to inactive.

I´m trying to do this with PDO but I´m getting this error below:

I´m having this error:

Notice: Array to string conversion in $updateStatus = $pdo->prepare("UPDATE ...)

I´m already using {$data} in brackets but its not working!

Somebody there can give me a help?

 if(isset($_GET['sts']))
    {
        $topicId = $_GET['id'];
        $status = $_GET['sts'];
        if($status == '0')
        {
            $data = array('status' => '1');
            $updateStatus = $pdo->prepare("UPDATE news SET {$data} WHERE id=:id");  
            $updateStatus->bindValue(':id',$topicId );
            $updateStatus->execute();

        }
        else
        {
            $data = array('status' => '0');
            $updateStatus = $pdo->prepare("UPDATE news SET {$data} WHERE id=:id");  
            $updateStatus->bindValue(':id', $topicId);
            $updateStatus->execute();
        }
    }  

This error is because $data is an array that you are trying to concatenate with a string. Instead, do something like this:

$updateStatus = $pdo->prepare("UPDATE news SET status=:status WHERE id=:id");  
$updateStatus->bindValue(':status', 1);
$updateStatus->bindValue(':id', $topicId);
$updateStatus->execute();

Technically since status is set via your script and not user-data, it doesn't need to be sanitized with PDO::prepare(). So you can say SET status=1, and not worry about binding that value.