我需要通过PDO从表中删除记录。我做了一些代码(与没有base64编码的下面相同)并且它有效。然后我在我的数据库上添加了base64_encoding,并将参数发送到MySQL。出于各种原因需要这样做,因此我无法抛弃base64编码。不幸的是,这打破了它(数据库中没有任何反应)。我无法弄清楚如何调试它,因为查询不会抛出错误。我跟着这个guide尝试调试它但没有抛出错误或被记录。有谁知道base64格式(例如:' siecX ==')一般会导致MySQL或PDO出现问题? 我的代码:
<?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();
}
?>
答案 0 :(得分:1)
在前者中,使用对变量的引用,而在后者中,使用值本身。
通过查看其使用语法可以更清楚:
public bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
和
public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )