我正在尝试使用以下脚本将隐藏表单中的值插入到数据库中
/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 :(得分:1)
这可能不是PHP问题:也许你只是违反了foreign key constraint。如果您不了解外键约束,请立即停止编写您的应用程序,并仔细阅读我链接的维基百科文章。
有些问题:
CREATE TABLE
陈述吗?$user
检查echo
变量是否已正确设置?$title
,$link
,$date
和$description
值进行硬编码来执行预准备语句?偏离主题:您的脚本中存在巨大的安全漏洞。 header("Location: ...")
doesn't quit the execution of the script!
答案 1 :(得分:1)
subscriberID
表格中的saved_holidays
字段似乎引用了email
表格中的字段subscriber
。
你能告诉我们一些数据样本吗?
我猜您没有在subscriberID
字段中插入现有订阅者的电子邮件。