调用成员函数bindParam()

时间:2019-07-17 19:44:08

标签: php pdo

怎么了?

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = htmlentities($_POST['update_name']);
    $surname = htmlentities($_POST['update_surname']);
    $profile_description = htmlentities($_POST['update_profile_description']);

    $consulta = $conexao_pdo->prepare('UPDATE users SET name = :name, surname=$surname, profile_description = :profile_description WHERE username = :username');
    $consulta->bindParam(':username', $user_logged);
    $consulta->bindParam(':name', $name);
    $consulta->bindParam(':surname', $surname);
    $consulta->bindParam(':profile_description', $profile_description);
    if ($consulta->execute()) {
        //saved
    }
}

给我错误

  

致命错误:未捕获的错误:在堆栈跟踪中的bool上调用成员函数bindParam():#0 {main}引发

2 个答案:

答案 0 :(得分:1)

您正在将 bindparam()作为变量发送

surname=$surname 

更改为:

surname = :surname

我还删除了htmlentities,因为插入时可能会有混乱。

这是最终代码:

    <?php 

 if($_SERVER['REQUEST_METHOD'] == 'POST'){

$name = $_POST['update_name'];
$surname = $_POST['update_surname'];
$profile_description = $_POST['update_profile_description'];

  $consulta = $conexao_pdo->prepare('UPDATE users SET name = :name, surname = :surname, profile_description = :profile_description WHERE username = :username');
  $consulta->bindParam(':username', $user_logged);
  $consulta->bindParam(':name', $name);
  $consulta->bindParam(':surname', $surname);
  $consulta->bindParam(':profile_description', $profile_description);  
 if ($consulta->execute()) {
  //saved
}
}
 ?>

请记住,在向数据库发送数据时不应使用htmlentities,因为它会删除一些字符,仅在显示数据库中未插入或更新的数据时才使用

答案 1 :(得分:0)

例如,写不带(:)的数组键:

$ consulta-> bindParam('name',$ name);