[英]PDO delete is not working and not throwing errors (when adding base64)
I need to delete a record from a table via PDO. 我需要通过PDO从表中删除一条记录。 I made some code (the same as below without the base64 encoding) and it worked. 我做了一些代码(与下面的代码相同,但没有base64编码),并且可以正常工作。 Then I added base64_encoding on my database and on the parameters send to MySQL. 然后,在数据库和发送给MySQL的参数上添加了base64_encoding。 This is needed for various reasons, so I can't ditch the base64 encoding. 由于各种原因需要这样做,因此我无法放弃base64编码。 Unfortunately this broke it (nothing happens in the database). 不幸的是,它破坏了它(数据库中什么也没有发生)。 I can't figure out how to debug this because the query doesn't throw an error. 我不知道如何调试它,因为查询不会引发错误。 I followed this guide trying to debug it but nothing throws an error or is logged. 我按照本指南尝试对其进行调试,但没有任何错误或记录错误。 Does anyone knows that the base64 format (for example:'siecX==') can cause issues in MySQL or PDO in general? 有谁知道base64格式(例如:'siecX ==')通常会在MySQL或PDO中引起问题? My code: 我的代码:
<?php
$dbname="dbname";
$table="whitelisted_parameters";
session_start();
include_once(dirname(__DIR__)."../../config/database_conf.php");
try {
$conn = new PDO("mysql:host=$hostname; dbname=$dbname", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql= "DELETE FROM $table WHERE virtual_host_id=:id_virtual_host_selected AND `page`=:page AND `parameter`=:parameter AND `method`=:method";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id_virtual_host_selected", $_SESSION['id_virtual_host_selected'], PDO::PARAM_INT);
$fap=base64_encode($_POST['page']);
echo $fap."||";// I used a temp value to log the variables
$stmt->bindParam(":page", $fap, PDO::PARAM_STR);
$fap=base64_encode($_POST['parameter']);
echo $fap."||";
$stmt->bindParam(":parameter", $fap, PDO::PARAM_STR);
$fap=base64_encode($_POST['method']);
echo $fap."||";
$stmt->bindParam(":method", $fap, PDO::PARAM_STR);
$stmt->execute();
// Close DB connection
$dbh = null;
echo "parameter is removed";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
You're confusing the bindParam
and bindValue
calls. 您混淆bindParam
和bindValue
调用。
In the former, a reference to the variable is used, whereas, in the latter, the value itself is used. 在前者中,使用对变量的引用,而在后者中,使用值本身。
This would be clearer by seeing their usage syntax: 通过查看它们的用法语法,这将更加清楚:
public bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
and 和
public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.