2012-05-02 139 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" />'; 
然而

inset值當我運行該腳本,我碰到下面的錯誤

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

偏題:你的腳本中有一個巨大的安全漏洞。標題(「位置:...」)不會退出腳本的執行! – gd1

回答

1

這可能不是一個PHP的問題:也許你只是違反了foreign key constraint。如果您不瞭解外鍵約束,請立即停止編寫您的應用程序,並完整閱讀我鏈接的維基百科文章。

一些問題:

  • 是你自己設計的數據庫,或者你只是使用它嗎?你能向我們展示你插入東西的表的CREATE TABLE聲明嗎?
  • 您是否檢查$user變量的設置是否正確?echo -ing it?
  • 您是否試圖通過對$title,$link,0 $date$description值進行硬編碼來執行準備的語句?

偏題:您的腳本中存在巨大的安全漏洞。 header("Location: ...")doesn't quit the execution of the script

1

看起來您的字段在saved_holidays表中引用subscriber表中的字段email

你能告訴我們一些數據樣本嗎?

我想你沒有在subscriberID字段中插入現有訂戶的電子郵件。