php pdo bind_param 错误

php pdo bind_param error

我 运行 插入 table

时出错

JSON:

{"machine":"1","postage":"1","tracking":"1","lve":"1","notice":"4","content":"12","refresh":"1","location":"cityname"}

我怎样才能做到这一点?

感谢

编辑 修复了你们提到的错误

function addOptions ($postData, $dbh) {

//print_r($postData);
$result = json_decode($postData);
//$location = $result->location;
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO options (location, display_time ,content_time, refresh_time, machine_data, postage_data, tracking_data, lve_data) 
                    VALUES (:location, :display_time, :content_time, :refresh_time, :machine_data, :postage_data, :tracking_data, :lve_data) 
                    ON DUPLICATE KEY UPDATE 
                    display_time= :display_time, refresh_time= :refresh_time, machine_data= :machine_data, content_time= :content_time, postage_data= :postage_data, tracking_data= :tracking_data, lve_data= :lve_data";
$preparedStatement = $dbh->prepare($sql);
$preparedStatement->bindParam(':location', $result->location);
$preparedStatement->bindParam(':display_time', $result->display);
$preparedStatement->bindParam(':content_time', $result->content);
$preparedStatement->bindParam(':refresh_time', $result->refresh);
$preparedStatement->bindParam(':machine_data', $result->machine);
$preparedStatement->bindParam(':postage_data', $result->postage);
$preparedStatement->bindParam(':tracking_data', $result->tracking);
$preparedStatement->bindParam(':lve_data', $result->lve);
$preparedStatement->execute();

}

现在我收到这条错误消息

警告:PDOStatement::execute() 要求参数 1 为数组,第 186 行 D:\xampp\htdocs\admin\include\function.php 中给出的字符串

您忘记创建准备好的语句,请在绑定参数之前尝试此操作:

$preparedStatement = $dbh->prepare($sql);

编辑:

只需更改此:

(既然你已经绑定了参数,你就可以执行查询!否则执行的参数将包括准备语句的参数)

$preparedStatement->execute($sql);

对此:

$preparedStatement->execute();

有关 PDO execute() 命令的更多信息,请参阅手册:http://php.net/manual/en/pdostatement.execute.php

引自那里:

public bool PDOStatement::execute ([ array $input_parameters ] )

几个错误:

  1. 添加 pdo 准备:

    $preparedStatement = $dbh->prepare($sql);
    
  2. 将您所有的 bind_param 更改为:

    $preparedStatement->bindParam(':display', $result->display);
    
  3. 将执行添加到 if 条件中,以便您查看它是否有效:

    if( $preparedStatement->execute() === FALSE){
    
       Throw new \Exception('Bad query insertion!');
    }