
PHPで編集機能を作成していたところ、トランザクションエラーが発生しましたが、
原因が特定できません。
一体何が原因なのでしょうか?
エラー
Fatal error: Uncaught PDOException: There is no active transaction in /var/www/html/Portfolio/public_html/account_edit_insert.php:163 Stack trace: #0 /var/www/html/Portfolio/public_html/account_edit_insert.php(163): PDO->rollBack() #1 {main} thrown in /var/www/html/Portfolio/public_html/account_edit_insert.php on line 163
account_edit_insert.php
<?php ini_set("display_errors", 1); error_reporting(E_ALL); session_start(); // 文字化け対策 header("Content-type: text/html; charset=UTF-8"); require_once('./functions.php'); require_once('../config/db.php'); // ログインしていなければ login_form.php に遷移 require_logined_session(); // 初期化 $errors = array(); // POST時 if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'POST') { $_SESSION["name"] = filter_input(INPUT_POST, 'name'); $_SESSION["email"] = filter_input(INPUT_POST, 'email'); $_SESSION["password"] = filter_input(INPUT_POST, 'password'); $_SESSION["tel"] = filter_input(INPUT_POST, 'tel'); $name = $_SESSION["name"]; $email = $_SESSION["email"]; $password = $_SESSION["password"]; $tel = $_SESSION["tel"]; // // バリデーション // // 入力チェック if (empty($_SESSION["name"])) { $errors[] = "名前が入力されていません。"; } if (empty($_SESSION["email"])) { $errors[] = "E-mailが入力されていません。"; } if (empty($_SESSION["password"])) { $errors[] = "パスワードが入力されていません。"; } if (empty($_SESSION["tel"])) { $errors[] = "電話番号が入力されていません。"; } // 名前の文字数チェック if (strlen($_SESSION["name"]) >= 60) { $errors[] = "氏名が長すぎます。"; } // パスワード文字数チェック(8文字以上かどうか) if (preg_match("/^[a-zA-Z1-9]{1,7}$/", $_SESSION['password'])) { $errors[] = "パスワードは8文字以上で入力してください。"; } // 電話番号の文字数チェック(10文字 or 11文字) if (strlen($_SESSION['tel']) >= 1 && strlen($_SESSION['tel']) <= 9 && preg_match("/^[0-9]+$/", $_SESSION['tel'])) { $errors[] = "電話番号は10文字か11文字で入力してください。"; } elseif (strlen($_SESSION['tel']) >= 12 && preg_match("/^[0-9]+$/", $_SESSION['tel'])) { $errors[] = "電話番号は10文字か11文字で入力してください。"; } // メールアドレス形式チェック if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_SESSION['email']) && $_SESSION['email'] !== '') { $errors[] = "メールアドレスに間違いがあります。"; } // 電話番号の形式チェック if (preg_match("/[-]+/", $_SESSION['tel'])) { $errors[] = "電話番号はハイフンなしで入力してください。"; } // 電番番号の数字チェック // メールアドレス形式チェック if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_SESSION["email"]) && $_SESSION["email"] !== "") { $errorMsg[] = "メールアドレスに間違いがあります。"; } if (count($errors) > 0) { $_SESSION["errors"] = $errors; header("Location: account_edit.php"); } if (count($errors) === 0) { try { $dbh = new PDO($dsn, $user, $password); // プリペアドステートメント $statement = $dbh->prepare("UPDATE users SET name = :name, email = :email, password = :password, tel = :tel WHERE id = :id"); // トランザクション開始 $dbh->beginTransaction(); if ($statement) { // プレースホルダへ実際の値を設定する $statement->bindValue(':name', $_SESSION['name'], PDO::PARAM_STR); $statement->bindValue(':email', $_SESSION['email'], PDO::PARAM_STR); $statement->bindValue(':password', $_SESSION['password'], PDO::PARAM_STR); $statement->bindValue(':tel', $_SESSION['tel'], PDO::PARAM_STR); $statement->bindValue(':id', $_SESSION['id'], PDO::PARAM_STR); // クエリ実行 $statement->execute(); // トランザクションコミット $dbh->commit(); } } catch (PDOException $e) { $dbh->rollBack(); print('Error:' .$e->getMessage()); $errors["error"] = "データベース接続失敗しました。"; } } } // // Twig // // Composerで作成されたautoload.phpを読み込む require_once('../vendor/autoload.php'); // Twig_Loader_Filesystemを使う。account_edit_insert.phpからのtemplatesディレクトリを指定。(相対パス) $loader = new Twig_Loader_Filesystem('../templates'); // $loaderをTwigの環境設定としてTwig instance を生成 $twig = new Twig_Environment($loader); // render // render echo $twig->render('account_edit_insert.html', array( 'errors' => $errors, 'name' => $name, 'email' => $email, 'password' => $password, 'tel' => $tel ) );
・接続DBは portfolioデータベースの中の usersテーブル
追記 エラー
Error:SQLSTATE[HY000] [1045] Access denied for user 'andrew'@'localhost' (using password: YES)
mysql 権限
mysql テーブル
回答2件
あなたの回答
tips
プレビュー