Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用外键将PHP表单中的数据插入MySQL数据库?_Php_Mysql_Checkbox_Foreign Keys_Insert Into - Fatal编程技术网

如何使用外键将PHP表单中的数据插入MySQL数据库?

如何使用外键将PHP表单中的数据插入MySQL数据库?,php,mysql,checkbox,foreign-keys,insert-into,Php,Mysql,Checkbox,Foreign Keys,Insert Into,我正在处理一个包含复选框和MySQL数据库的PHP表单。通过在数组上循环,我最终实现了插入多行,每个选定项一行。 但现在,我必须面对另一个问题:在我的数据库中,我有一个主要表来存储一个选项问题,另一个表来存储复选框中的答案。 我想首先执行查询,将one choice答案插入主体表(每个表单一行),以便它生成一个序列ID。 其次,获取此ID并将其与插入复选框表中的每一行关联,以便通过此ID链接两个表。 有可能吗?我该怎么办 下面是HTML代码: <input type="checkbox"


我正在处理一个包含复选框和MySQL数据库的PHP表单。通过在数组上循环,我最终实现了插入多行,每个选定项一行。
但现在,我必须面对另一个问题:在我的数据库中,我有一个主要表来存储一个选项问题,另一个表来存储复选框中的答案。
我想首先执行查询,将one choice答案插入主体表(每个表单一行),以便它生成一个序列ID。
其次,获取此ID并将其与插入复选框表中的每一行关联,以便通过此ID链接两个表。
有可能吗?我该怎么办

下面是HTML代码:

<input type="checkbox" name="nature_contact[]" value="1"><label >Phone</label><br/>
<input type="checkbox" name="nature_contact[]" value="2"><label >Mail</label><br/>
<input type="checkbox" name="nature_contact[]" value="3"><label >Visit</label><br/>
<input type="checkbox" name="nature_contact[]" value="4"><label >Unk</label>    <br/><br/> 
<input type="text" name="coord"/>
            <br/>
<input type="text" name="tel"/>
            <br/><br/>              
<input type="submit" name="add" value="SEND"/>
是的,这是可能的

您需要主表行的最后一个插入id,如:

$lastInsertedID = $db->lastInsertId();
1.)在数据库表中插入问题(主体)

2.)获取最后一个插入的id($lastInsertedID)

3.)在答案表中插入与问题相关的答案,并提供最后插入的id

$query = "INSERT INTO nmp_mfs.t_temporaire (questionID, nature_contact) 
VALUES ($lastInsertedID, $nature_contact)"; // Example
4.)选择问题的ID

5.)获得相应的答案:

$query = "SELECT awnsers WHERE question_id = questionID"; // Simple example
是的,这是可能的

您需要主表行的最后一个插入id,如:

$lastInsertedID = $db->lastInsertId();
1.)在数据库表中插入问题(主体)

2.)获取最后一个插入的id($lastInsertedID)

3.)在答案表中插入与问题相关的答案,并提供最后插入的id

$query = "INSERT INTO nmp_mfs.t_temporaire (questionID, nature_contact) 
VALUES ($lastInsertedID, $nature_contact)"; // Example
4.)选择问题的ID

5.)获得相应的答案:

$query = "SELECT awnsers WHERE question_id = questionID"; // Simple example

为了确保数据同步,您可以使用mysql中的transaction。对不起,我的英语很差,我只想做一些有用的事情。

为了确保数据同步,您可以使用mysql中的transaction。对不起,我的英语很差,我只想做一些有用的事情。

谢谢!我尝试使用(max id),但是lastInsertId()更可靠。但是,我需要将此值添加到函数中:.rtrim(str_repeat(“(?),”,count($_POST[“nature_contact”]),“,”),这对我来说很棘手。我尝试使用str_repeat(“($lastID,”),“但是它重新计算字符串而不是$lastID中的值。是否可以解决?它可以工作!我忘记了:str_repeat(“(“$lastID.”,”,?),“谢谢!我尝试使用(max id),但是lastInsertId()更可靠。但是,我需要将此值添加到函数中:.rtrim(str_repeat(“(?),count($\u POST[“nature\u contact”]),,,,,这对我来说很棘手。我尝试了str\u repeat(“($lastID),”),但它从$lastID中重新计算了一个字符串而不是值。有可能解决吗?它有效!我忘记了:str\u repeat(“($lastID.),?),”中的引号