简体   繁体   English

使用PHP表单将外键数据插入表时遇到问题

[英]Having a problem inserting a foreign key data into a table using a PHP form

I am newbee with PHP and MySQL and need help... 我是PHP和MySQL的新手,需要帮助...

I have two tables (Staff and Position) in the database. 我在数据库中有两个表(Staff和Position)。 The Staff table (StaffId, Name, PositionID (fk)). 人员表(StaffId,名称,PositionID(fk))。 The Position table is populated with different positions (Manager, Supervisor, and so on). 职位表中填充​​了不同的职位(经理,主管等)。 The two tables are linked with a PositionID foreign key in the Staff table. 这两个表与Staff表中的PositionID外键链接。 I have a staff registration form with textfields asking for the relevant attributes and a dynamically populated drop down list to choose the position. 我有一个带有文本字段的职员登记表,询问相关属性,并使用动态填充的下拉列表选择职位。 I need to insert the user's entry into the staff table along with the selected position. 我需要将用户的条目以及所选位置插入人员表。 However, when inserting the data, I get the following error (Cannot add or update a child row: a foreign key constraint fails). 但是,在插入数据时,出现以下错误(无法添加或更新子行:外键约束失败)。 How do I insert the position selected by the user into the staff table? 如何将用户选择的职位插入人员表?
Here is some of my code... 这是我的一些代码...

...
echo "<tr>";
echo "<td>";
echo "*Position:"; 
echo "</td>";
echo "<td>";

//dynamically populate the staff position drop down list from the position table
$position="SELECT PositionId, PositionName
            FROM Position
            ORDER BY PositionId";
$exeposition = mysql_query ($position) or die (mysql_error());
echo "<select name=position value=''>Select Position</option>";
while($positionarray=mysql_fetch_array($exeposition))
{
echo "<option value=$positionarray[PositionId]>$positionarray[PositionName]</option>";
}
echo "</select>";   
echo "</td>";
echo "</tr>"

//the form is processed with the code below

$FirstName = $_POST['firstname'];
$LastName = $_POST['lastname'];
$Address = $_POST['address'];
$City = $_POST['city'];
$PostCode = $_POST['postcode'];
$Country = $_POST['country'];
$Email = $_POST['email'];
$Password = $_POST['password'];
$ConfirmPass = $_POST['confirmpass'];
$Mobile = $_POST['mobile'];
$NI = $_POST['nationalinsurance'];
$PositionId = $_POST[$positionarray['PositionId']];

//format the dob for the database
$dobpart = explode("/", $_POST['dob']);
$formatdob = $dobpart[2]."-".$dobpart[1]."-".$dobpart[0];
$DOB = date("Y-m-d", strtotime($formatdob));

$newReg = "INSERT INTO Staff (FirstName, LastName, Address, City, PostCode,     
Country, Email, Password, Mobile, DOB, NI, PositionId) VALUES ('".$FirstName."', 
'".$LastName."', '".$Address."', '".$City."', '".$PostCode."', '".$Country."',  
'".$Email."', '".$Password."', ".$Mobile.", '".$DOB."', '".$NI."', '".$PostionId."')";

Your time and help is surely appreciated. 您的时间和帮助肯定会得到赞赏。

You seem to have a typo in the INSERT . 您似乎在INSERT有错字。 The variable $PostionId should be $PositionId . 变量$PostionId应该是$PositionId

In addition, make sure you understand that you code is vulnerable to SQL Injection . 另外,请确保您了解自己的代码容易受到SQL Injection的攻击。

In your insert SQL query, you are using this : 在您的插入SQL查询中,您使用的是:

, '".$PostionId."')"

While, a few lines before, you are declaring : 之前几行,您在声明:

$PositionId = $_POST[$positionarray['PositionId']];

The variable used in the insert query is not the same as the one containing the data : it lacks a i -- which, I suppose, is bad. 插入查询中使用的变量与包含数据的变量不同:它缺少一个i ,我想这是不好的。


If this not enough to solve your problem, echoing your SQL query before executing it might help you figure out what is wrong in it. 如果这还不足以解决您的问题,请在执行之前回显您的SQL查询,这可能有助于您找出问题所在。

This kind of error, about foreign keys, is generally caused by inserting a wrong id -- that doesn't have a corresponding row in the referenced table. 这种有关外键的错误通常是由插入错误的ID引起的-在引用表中没有对应的行。


Aside from that, your code is vulnerable to SQL injections : you should escape and filter your data. 除此之外,您的代码还容易受到SQL注入的攻击:您应该转义并过滤数据。

For string values, you can use mysql_real_escape_string : 对于字符串值,可以使用mysql_real_escape_string

$FirstName = mysql_real_escape_string($_POST['firstname']);

For integer values, you can make sure you are injecting integers in the SQL query, using, for instance, intval : 对于整数值,可以确保使用例如intval在SQL查询中注入整数:

$data = intval($_POST['data']);

(Not sure you have an integer value here, so I just took 'data ' as an example) (不确定此处是否有整数值,因此我仅以'data ”为例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM