PHPでサインインAPIを作成したのですが、以下のコードに書かれている箇所でmysqlへのインサートが失敗してしまいます。いろいろ試してみたのですが、どこが間違っているのかわかりません。
いろいろ試した結果以下のことがわかりました。
- lastInsertId()は正しく呼ばれていてidを取得出来ている。
- 1つ前のINSERTは成功している。
- テーブル名、カラム名は正しい。
- エラーは出ない。
どなたか、どこが間違っているのかわかりますか?
lang
1<?php 2header("Content-Type: application/json; charset=UTF-8"); 3header("X-Content-Type-Option: nosniff"); 4header("Access-Control-Allow-Origin: *"); 5header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 6require_once 'functions.php'; 7require_once 'session_util.php'; 8 9$pdo = initDB(); 10switch($_SERVER['REQUEST_METHOD']){ 11 case 'POST': 12 $in = json_decode(file_get_contents('php://input'), true); 13 $error = ''; 14 if(isset($in['mail_address'])&&isset($in['password'])&&isset($in['username'])){ 15 if(!checkEmail($pdo, $in['mail_address'])){ 16 $error = "メールアドレスが登録済みです。"; 17 header("HTTP/1.0 402 Unauthexed"); 18 sendError($error); 19 }else{ 20 $st = $pdo->prepare("INSERT INTO user (mail_address, password, username) VALUES (:mail_address, :password, :username)"); 21 $st->bindParam(':mail_address', $in['mail_address'], PDO::PARAM_STR); 22 $hashedPassword = makeHash($in['password']); 23 $st->bindParam(':password', $hashedPassword, PDO::PARAM_STR); 24 $st->bindParam(':username', $in['username'], PDO::PARAM_STR); 25 if($st->execute()){ 26 $userId = $pdo->lastInsertId('id'); 27 $st2 = $pdo->prepare("INSERT INTO user_profile (user_id, username) VALUES (:user_id, :username)"); 28 $st2->bindValue(":user_id", $userId, PDO::PARAM_INT); 29 $st2->bindParam(":username", $in['username'], PDO::PARAM_STR); 30 if($st2->execute()){ 31 header("HTTP/1.0 200 OK"); 32 onSuccess($pdo, $in['mail_address'], $userId); 33 }else{ 34 $st2->errorInfo(); 35 $error="Un known error."; 36 sendError($st->errorInfo()); 37 } 38 }else{ 39 $error = "server error"; 40 header("HTTP/1.0 500 Internal Server Error"); 41 sendError($error); 42 } 43 } 44 }else{ 45 $error = "メールアドレスとパスワードを入力してください。"; 46 header("HTTP/1.0 400 Bad Request"); 47 sendError($error); 48 } 49 break; 50}
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。