我的代码:
<?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();
}
在代码中您将看到第一部分用于进行身份验证。然后我在 img 架构上创建一个新的 PDO 对象,然后执行文件插入查询。
后来,我打印 $dbMsg 时,它说“已将文件添加到存储库”。但是当我查询数据库(使用 MySQL Workbench 的 Amazon AWS 上的 MySQL)时,没有插入任何内容。
我不明白为什么如果没有插入任何内容我没有收到错误消息。如果显示“已将文件添加到存储库”,是不是意味着插入成功?我唯一能想到的是,为此使用不同的模式会把事情搞砸。我对 ebdb 的所有插入都进展顺利
--- 编辑 ---
这个问题在我关于插入/执行代码上没有收到错误消息的查询中被标记为可能重复。这是一个有用的链接,我将来肯定会注意并检查,但最终的答案是我提供的有关我的 aws 帐户服务条款的链接
最佳答案
答案是,我正在使用的(免费)亚马逊帐户政策仅允许我拥有 1 个数据库/架构。当我将表切换到 ebdb 时,它立即开始工作。我正在回答我自己的问题(而不是删除),因此希望使用 AWS/MySQL 的其他人可以从我的经验中学习。
关于PHP PDO MySQL : Insert statement runs without error, 没有发生插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55968166/