我在插入表
时遇到错误JSON:
{"machine":"1","postage":"1","tracking":"1","lve":"1","notice":"4","content":"12","refresh":"1","location":"cityname"}
我怎样才能做到这一点?
THX
修改的 修复了你们提到的错误
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中给出
答案 0 :(得分:2)
您忘记创建预准备语句,在绑定参数之前尝试此操作:
$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 :(得分:1)
几个错误:
添加pdo prepare:
$preparedStatement = $dbh->prepare($sql);
将所有bind_param更改为:
$preparedStatement->bindParam(':display', $result->display);
将执行添加到if条件中,以便查看它是否有效:
if( $preparedStatement->execute() === FALSE){
Throw new \Exception('Bad query insertion!');
}