使用一个提交按钮将数据从 2 个表单传递到数据库
Posted
技术标签:
【中文标题】使用一个提交按钮将数据从 2 个表单传递到数据库【英文标题】:Passing data from 2 forms with the use of one submit button to the database 【发布时间】:2016-03-29 13:59:03 【问题描述】:我对 php 和 html 还很陌生,自从我尝试解决这个问题已经 2 天了。
我正在尝试将这些表单中的数据传递到我的数据库表中,但我无法这样做,因为这两个表单只使用相同的标签。 The 2 Forms The table to which I'm trying to send the data
<?php
if(isset($_POST['submitted']))
include('config/config1.php');
global $conn; /* para makuha ang variable galing sa config/config1. */
$firstname1 = $_POST["firstname"];
$middlename1 = $_POST["middlename"];
$lastname1 = $_POST['lastname'];
$sex1 = $_POST['sex'];
$household_head1 = $_POST['household_head'];
$household1 = $_POST['household'];
$civil_status1 = $_POST['civil_status'];
$bday1 = $_POST['birthdate'];
$bplace1 = $_POST['birthplace'];
$citizenship1 = $_POST['citizenship'];
$occupation1 = $_POST['occupation'];
$sponsors_female1 = $_POST['sponsors_female'];
$sponsors_male1 = $_POST['sponsors_male'];
$email1 = $_POST['email'];
$date_of_seminar1 = $_POST['date_of_seminar'];
/**** Query ****/
$insrtBptm = "INSERT INTO parishioner (firstname,middlename,lastname,sex,household_head,household,civil_status,birthdate,birthplace,citizenship,occupation,sponsors_female,sponsors_male,email,date_of_seminar)
VALUES ('$firstname1','$middlename1','$lastname1','$sex1','$household_head1','$household1','$civil_status1','$bday1','$bplace1','$citizenship1','$occupation1','$sponsors_female1','$sponsors_male1','$email1','$date_of_seminar1')";
$update = "UPDATE parishioner SET Parishioner_idParishioner = idParishioner
WHERE Parishioner_idParishioner IS NULL AND idParishioner IS NOT NULL";
header("Location:Home.php");
/**** Validation ****/
if(!mysqli_query($conn,$insrtBptm))
die("Error not queried". mysqli_error());
if(!mysqli_query($conn,$update))
die("Update error !". mysqli_connect_error());
mysqli_close($conn);
?>
【问题讨论】:
您可以对所有字段使用单一表单@Jake152 从您的屏幕截图中,提交按钮仅发布丈夫表单的数据。 而不是使用两个表单,您应该使用单个表单,对于丈夫和妻子的相同字段,您可以使用数组名称 ="wife[first_name]" 和丈夫姓名 ="husband[first_name]"提交你会得到两阵妻子和丈夫 您可以使用命名分离,例如:您的标签名称为两种形式,因此您应该命名为男性,如 和女性,如 【参考方案1】:我会怎么做,
使用Wife
和Husband
创建一个表单,然后在它们的input
名称前加上wi_
和hu_
分别,一旦提交表单,我将处理它在服务器端,在验证/验证之后,我将进行查询以插入两行具有相应输入的数据。
【讨论】:
【参考方案2】:别再担心了!为 php 创建另一个第二个变量(即:firstname2、middlename2 等)。然后做这个魔术
$insrtMar = "INSERT INTO parishioner (firstname,middlename,lastname,sex,household_head,household,civil_status,birthdate,birthplace,citizenship,occupation,sponsors_female,sponsors_male,email,date_of_seminar) VALUES ('$firstname1','$middlename1','$lastname1','$sex1','$household_head1','$household1','$civil_status1','$bday1','$bplace1','$citizenship1','$occupation1','$sponsors_female1','$sponsors_male1','$email1','$date_of_seminar1'),('$firstname2','$middlename2','$lastname2','$sex2','$household_head2','$household2','$civil_status2','$bday2','$bplace2','$citizenship2','$occupation2','$sponsors_female2','$sponsors_male2','$email2','$date_of_seminar2')";
所以在查询中简称:INSERT INTO tablename (columnnames) VALUES (*1st php variables), (*2nd php variables)
维奥拉!
【讨论】:
以上是关于使用一个提交按钮将数据从 2 个表单传递到数据库的主要内容,如果未能解决你的问题,请参考以下文章