简体   繁体   English

将外键自动插入表中的简单方法

[英]Simple way to insert foreign key automatically into a table

I have two table program(p_id(pk)AI, program_name and another table graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk) . In the program table values are already inserted as 我有两个表program(p_id(pk)AI, program_name ,另一个表graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk) 。在程序表中已经插入了值

p_id Program_name
    1    B.tech CSE
    2    B.tech IT.

..so on. ..等等。 I select from a drop down list B.tech CSE and then redirect to the survey form. 我从下拉列表中选择B.tech CSE,然后重定向到调查表。 I enter the total_PO1, total_PO2 there and submit it.Now i want to insert in the graduate_survey table where in the fk field the p_id of B.tech CSE should automatically enter so that i can know the survey is done for which program..Is there any query in MySQL to do that? 我在此处输入total_PO1,total_PO2并将其提交。现在我想将其插入在graduate_survey表中,在fk字段中,B.tech CSE的p_id应该自动输入,以便我可以知道针对哪个程序进行了调查。在MySQL中有任何查询可以做到这一点? The insert operation should be done in php code.Please suggest any query. 插入操作应使用php代码完成。请提出任何查询。

Your approach seems to be incorrect. 您的方法似乎不正确。 When you generate your dropdown menu in HTML, then the display value (the value that the user actually sees and selects) should be the program name, but the underlying value should be the id of the same record. 当您使用HTML生成下拉菜单时,显示值(用户实际看到并选择的值)应该是程序名称,但是基础值应该是同一记录的ID。

When you send the data to the server, then you should send the underlying id value, not the display value. 将数据发送到服务器时,应发送基础ID值,而不是显示值。 Using HTML's standard dropdown menu, you would create sg like this as an output: 使用HTML的标准下拉菜单,您将像这样创建sg作为输出:

<select name="programs">
    <option value="1">B.tech CSE</option>
    <option value="2">B.tech IT</option>
</select>

If this control is submitted via a form, then the browser will send the value of the selected option automatically. 如果此控件是通过表单提交的,则浏览器将自动发送所选选项的value If you use ajax, then your code has to retrieve the selected value of the select element. 如果使用ajax,则代码必须检索select元素的选定值。 In the php code you just insert the underlying value into the graduate survey table. 在php代码中,您只需将基础值插入到毕业生调查表中即可。

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

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