简体   繁体   中英

PDO delete is not working and not throwing errors (when adding base64)

I need to delete a record from a table via PDO. I made some code (the same as below without the base64 encoding) and it worked. Then I added base64_encoding on my database and on the parameters send to MySQL. This is needed for various reasons, so I can't ditch the base64 encoding. 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? 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.

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 ]]] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM