1

My problem is that when I use the PDO::PARAM_LOB parameter in the bindParam() method the photo sent from the html form isn’t inserted in the MariaDB table. phpMyAdmin subsequently shows the table column ‘photo’ to be empty. The image is successfully inserted in the table when I don’t use the PDO::PARAM_LOB but this causes a Notice: Array to string conversion in C:\xampp\htdocs\TestSite\php-sellform-handler.php on line 80 to display to the user on the web page. There are other fields on the same form (which for simplicity I haven’t shown) which upload text and use PDO::PARAM_STR. This text data is always successfully inserted in the same table. How do I get insert to work using PDO::PARAM_LOB?

The nearest problem to mine that I can find on S/O is PHP/PDO/MySQL: inserting into MEDIUMBLOB stores bad data However that involves the image actually being inserted in the table, but in a corrupted form.

I’m using Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.2 and 10.1.30-MariaDB

Here is my code:

$host = '127.0.0.1';
$db = 'test_db2';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";



$opt = [PDO::ATTR_ERRMODE =>PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false 
    ];


$pdo=new PDO($dsn, $user, $pass, $opt);



if (isset($_FILES ['photo'])) {

$photo=$_FILES['photo'];


$stmt_user = $pdo->prepare("INSERT INTO users_table1(photo) VALUES(:photo)");

$stmt_user->bindParam(':photo', $photo, PDO::PARAM_LOB);    

$stmt_user->execute();
}

Thanks for any help!

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • 1
    Try $photo = file_get_contents($_FILES['photo']['tmp_name']) – Peter Chaula Apr 20 '18 at 11:15
  • 1
    What are you trying to insert, the contents of the image? If so, you need to read the contents of `$_FILES['photo']['tmp_name']`. The variable `$_FILES['photo']` itself is an array so that will not insert the image nor the path to the image. – jeroen Apr 20 '18 at 11:16
  • Thanks very much to Peter and Jeroen. It works! I knew it had to be a lack of basic knowledge on my part. Prob a simple matter for you but a big deal for me. As well as solving my problem it will allow me to expand my knowledge of this area of code. I will, if it's possible, accept both your answers as answers in the morning (in northern Bohemia) – coding for jebus Apr 20 '18 at 13:38
  • @codingforjebus - Yeah, uploading images (etc) with PHP is quirky. As you found out, MySQL and PDO were not the real problem. – Rick James May 10 '18 at 01:37

0 Answers0