gpt4 book ai didi

php - 使用 2 个输入值将数据插入到 MySql

转载 作者:行者123 更新时间:2023-11-29 10:44:29 25 4
gpt4 key购买 nike

我有 1 个表,我想将 2 个值插入到 MySql 中。但是,只有一个值进入我的数据库,另一个仍为空。

我的mysql表的设计: enter image description here

我的 Html 表格代码如下:

<table>

<tr>
<th></th>
<th>Main Applicant</th>
<th>Joint Applicant1</th>

</tr>
<tr>
<td>Name</td>
<td><input type="text" id="nameMain" name="nameMain" class="form-control" autocomplete="off" required></td>
<td><input type="text" id="nameJoint1" name="nameJoint1" class="form-control" autocomplete="off" required></td>


</tr>

<tr>
<td>Nationality(Country)</td>
<td><input type="text" id="nationMain" name="nationMain" class="form-control" autocomplete="off" required></td>
<td><input type="text" id="nationJoint1" name="nationJoint1" class="form-control" autocomplete="off" required></td>

</tr>
<tr>
<td>Age</td>
<td><input type="number" id="ageMain" name="ageMain" class="form-control" autocomplete="off" required></td>
<td><input type="number" id="ageJoint1" name="ageJoint1" class="form-control" autocomplete="off" required></td>


</tr>

我的 post.php 代码如下:

       <?php 


require_once 'db/dbfunction.php';
require_once 'db/dbCreditAssessment.php';

session_start();
$con = open_connection();



$name = $_POST['nameMain'];
$nationality = $_POST['nationMain'];
$age = $_POST['ageMain'];


$nameJoint1 = $_POST['nameJoint1'];
$nationJoint1 = $_POST['nationJoint1'];
$ageJoint1 = $_POST['ageJoint1'];

addApplicantPersonalDetails($con,$name,$nationality,$age);

addApplicantPersonalDetails2($con,$name,$nationality,$age);

close_connection($con);


?>

我的 addCreditAssessment.php 代码如下:

    <?php

function addApplicantPersonalDetails($con,$name,$nationality,$age){

$query = "insert into zzz(name,nationality,age)
values('$name','$nationality','$age')";
//echo "{$sqlString}";



$insertResult = mysqli_query($con, $query);


if($insertResult){
echo " Applicant Detail Added !<br />";
echo "<a href='index.php'>Back to Home</a>";
}
else {
echo " Error !";
echo "{$query}";
//header('Location: post.php');
}


}

function addApplicantPersonalDetails2($con,$name,$nationality,$age){



$query2 = "insert into zzz(name,nationality,age)
values('$nameJoint1','$nationJoint1','$ageJoint1')";


$insertResult2 = mysqli_query($con, $query2);



if($insertResult2){
echo " Applicant Detail Added !<br />";
echo "<a href='index.php'>Back to Home</a>";
}
else {
echo " Error !";
echo "{$query2}";
//header('Location: post.php');
}
}


?>

最佳答案

您使用的变量未在函数中定义,为什么为空

这样做

function addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1){
$query2 = "insert into zzz(name,nationality,age)
values('$nameJoint1','$nationJoint1','$ageJoint1')";


$insertResult2 = mysqli_query($con, $query2);



if($insertResult2){
echo " Applicant Detail Added !<br />";
echo "<a href='index.php'>Back to Home</a>";
}
else {
echo " Error !";
echo "{$query2}";
//header('Location: post.php');
}
}

这对你有用。

关于php - 使用 2 个输入值将数据插入到 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44879391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com