php - 通知: Array to string conversion with update statment

标签 php mysql sql pdo

我正在尝试创建一个系统来激活或停用我的帖子。

首先,我从我的网址获取 sts 并将其存储在我的 $status 中。

然后,如果 $status == 0 我会将我的帖子转为活跃状态,如果 $status == 1 我会将我的帖子转为非活跃状态。

我正在尝试使用 PDO 执行此操作,但出现以下错误:

我遇到此错误:

注意:$updateStatus = $pdo->prepare("UPDATE ...) 中的数组到字符串转换

我已经在括号中使用了 {$data} 但它不起作用!

有人可以帮我吗?

 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();
        }
    }  

最佳答案

此错误是因为 $data 是您尝试与字符串连接的数组。相反,做这样的事情:

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

从技术上讲,由于 status 是通过脚本而不是用户数据设置的,因此不需要使用 PDO::prepare() 进行清理。因此,您可以说 SET status=1,而不必担心绑定(bind)该值。

关于php - 通知: Array to string conversion with update statment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22850358/

相关文章:

php - 用于检查字符串的 MySQL 数据类型资源

mysql - 使用其他 2 个表的差异创建第 3 个表 SQL

sql - 按列排序表,但如果为0,则忽略-SQLite

php - Laravel MongoDB 分组依据

php - 使用 php 和 javascript 进行 URL 编码?

php - PHP 和 SQL 中的日期格式错误

mysql - 在SQL中获取周边地点

php - 通过变量串联访问数组元素的任何方法

php - 检查 MySql 表中的行

mysql - 按大小写忽略 LEFT JOIN 查询中的多条记录