简体   繁体   中英

PHP PDO MySQL: Insert statement runs without error, no insert occurs

My code:

<?php

try {
$t = '040485c4-2eba-11e9-8e3c-0231844357e8';

if (array_key_exists('t', $_REQUEST)) {
  $t = $_REQUEST["t"];
}

if (!isset($_COOKIE['writer'])) {
  header("Location: xxx");
  return 0;
}
$writer = $_COOKIE['writer'];

$dbhost = $_SERVER['RDS_HOSTNAME'];
$dbport = $_SERVER['RDS_PORT'];
$dbname = $_SERVER['RDS_DB_NAME'];
$charset = 'utf8' ;
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
$username = $_SERVER['RDS_USERNAME'];
$password = $_SERVER['RDS_PASSWORD'];

$pdo = new PDO($dsn, $username, $password);

$stmt = $pdo->prepare("select writer from mydbtbl where writer=? and t=?");
$stmt->execute(array($writer, $t));
$num = $stmt->fetch(PDO::FETCH_NUM);
if ($num < 1) {
  header("Location: login.php");
  return 0;
}

$dbMsg = "Authorized";

$dbname = 'imgs';
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
$pdo = new PDO($dsn, $username, $password);

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

  $name = $_FILES['filename']['name'];

  // set path of uploaded file
  $path = "./".basename($_FILES['filename']['name']); 

  // move file to current directory
  move_uploaded_file($_FILES['filename']['tmp_name'], $path);

  // get file contents
  $data = file_get_contents($path, NULL, NULL, 0, 60000);    

  $stmt = $pdo->prepare("INSERT INTO file (contents, filename, t) values (?,?,?)");
  $stmt->execute(array
    ($data,
     $name,
     $t)
  );
  $dbMsg = "Added the file to the repository";
 // delete the file
 unlink($path);
}
} catch (Exception $e) {
  $dbMsg = "exception: " . $e->getMessage();
}

In the code you will see that the first part is for doing authentication. Then I create a new PDO object on the img schema, and do my file insert query after that.

Later, where I am printing out $dbMsg, it is saying "added file to the repository". But when I query the database (MySQL on Amazon AWS using MySQL Workbench) nothing has been inserted.

I don't understand why if nothing is getting inserted I am not getting an error message. If it says "added file to the respository", doesn't that mean the insert was successful? The only thing I can think is that using a different schema for this is mucking things up. All of my inserts to ebdb are going through fine

--- EDIT ---

This question was marked as a possible duplicate on my query about not getting an error message on my insert / execute code. This was a useful link and definitely something I will be aware of and check in the future, but ultimately the answer is the one I have provided regarding the terms of service for my aws account

The answer is that the (free) amazon account policy I am working under only allows me to have 1 database / schema. When I switched the table over to ebdb it worked right away. I am answering my own question (rather than deleting) so hopefully others using AWS / MySQL can learn from my experience.

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