簡體   English   中英

PHP PDO MySQL:Insert語句運行無錯誤,不發生插入

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

我的代碼:

<?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的人可以從我的經驗中學到東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM