0

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

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I think you could remove the commented lines of code to make it more readable. – Elzo Valugi May 03 '19 at 10:46
  • 1
    The fact that you have the "added ..." message does not directly mean that the insert was succesful. That line of code is in the same code block but not directly related. To be sure wrap your execute statement into an if like here https://stackoverflow.com/questions/9991882/stmt-execute-how-to-know-if-db-insert-was-successful – Elzo Valugi May 03 '19 at 10:48
  • Possible duplicate of [$stmt->execute() : How to know if db insert was successful?](https://stackoverflow.com/questions/9991882/stmt-execute-how-to-know-if-db-insert-was-successful) – Elzo Valugi May 03 '19 at 10:50
  • 1
    You are missing a quote mark after login.php at the following line: `header("Location: login.php);` Should be: `header("Location: login.php");` That might be the problem. – K. P. May 03 '19 at 10:55

1 Answers1

1

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.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • If you're going to leave the question up you should correct the typo that was pointed out in the comments and also accept your own answer. – Nick May 05 '19 at 03:41