如果设置外键,如何在数据库中插入值?

时间:2015-01-22 13:52:24

标签: php

<?php

if(isset($_GET['email']) && !empty($_GET['email']))
{

$email = mysql_real_escape_string($_GET['email']);
$sml="UPDATE  USERS SET password=$_POST[password]  where email='$email' ";
$account=mysql_query("INSERT INTO ACCOUNT(email) SELECT email from USERS WHERE email='$email' ") or die('Error:' .mysql_error());

if (mysql_query($sml,$con))
{
header('Location: ../home.html');
}

else{
die('Eror: ' . mysql_error());
}
}
else
{die('Eror: ' . mysql_error());}

mysql_close($con);

?>

如何在帐户表中插入电子邮件,作为帐户表的外键。

我想在帐户表中插入相同的电子邮件值以用于其他表格引用。

1 个答案:

答案 0 :(得分:0)

您必须先添加用户。如果父(用户)表中没有匹配的记录,则无法将记录添加到Accounts表中。

这就是外键约束的工作原理。例如,如果Users表中的记录包含user_id = 1,则只允许在Accounts表中包含user_id = 1的记录。等等...

提示1:

尽量不要将$_POST['key']直接放入查询中,因为这会使其容易受到sql注入。

提示2:

在标题功能之后使用exit。