2012-05-02 31 views
-1

私はデータベースがPDO挿入エラーPHP

/detect user session 
if (!isset($_SESSION['user'])) 
{ 
//if no session take to login page 
header('location:login_main.php'); 
} 

//if session detected connect to database using pdo 
$db = getConnection(); 

//get holiday infor from hidden form 
$user = $_SESSION['user']; 
$title = $_POST['title']; 
$link = $_POST['link']; 
$date = $_POST['date']; 
$description = $_POST['description']; 

//insert the values in to favorties table 
$sql = "INSERT INTO saved_holidays (subscriberID, link, pubDate, title, description, dateSaved) 
VALUES (:subscriberID, :link, :pubDate, :title, :description, now())"; 

$stmt = $db->prepare($sql); 
$stmt->bindParam(':subscriberID', $user); 
$stmt->bindParam(':link', $link); 
$stmt->bindParam(':pubDate',$date); 
$stmt->bindParam(':title', $title); 
$stmt->bindParam(':description', $description); 
$stmt->execute(); 

echo 'you have sucessfully saved the holiday offer.<meta http-equiv="refresh" content="2; url=index.php" />'; 

にして隠しフォームから取られたインセット値に次のスクリプトを使用しようとしている私は、次のようなエラーに取得するスクリプトを実行すると

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity  constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails  (`unn_w11023553/saved_holidays`, CONSTRAINT `holidays_ibfk_1` FOREIGN KEY (`subscriberID`)  REFERENCES `subscriber` (`email`) ON UPDATE CASCADE)' in [OMISSIS] 

誰かが、私が間違って何をやったかおかげ

+0

トピックオフ:あなたのスクリプトには大きなセキュリティホールがあります。 header( "Location:...")はスクリプトの実行を終了しません! – gd1

答えて

1

を教えてくださいすることができますこれは、PHPの問題ではないかもしれません:多分あなただけforeign key constraintに違反しています。外部キーの制約について知らない場合は、今すぐあなたのアプリケーションの作成をやめ、ウィキペディアの記事I linkedを全面的に読んでください。

いくつかの質問:あなたは、データベースを自分で

  • を設計したのか、あなたはそれを使用していますか?あなたは私たちにあなたが何かを挿入しているテーブルのCREATE TABLEステートメントを表示できますか?
  • $user変数が正しく設定されていることを確認しましたか?echo -ing?
  • $title,$link,$date、および$descriptionの値をハードコードして準備済みの文を実行しようとしましたか?

オフトピック:あなたのスクリプトには大きなセキュリティホールがあります。 header("Location: ...")doesn't quit the execution of the script

1

フィールドのsaved_holidaysテーブルは、subscriberテーブルのフィールドemailを参照しているようです。

いくつかのデータサンプルを表示できますか?

subscriberIDフィールドに既存のサブスクライバの電子メールを挿入していないと思います。

関連する問題