我有1个表,我想在MySql中插入2个值。但是,我的数据库中只有一个值,另一个值为空。
我的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');
}
}
?>
答案 0 :(得分:1)
您正在使用未定义的变量i为空的原因
这样做
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');
}
}
它适用于你。
答案 1 :(得分:0)
你的第二个功能是错误的:
您的职能是:
$query2 = "insert into zzz(name,nationality,age)
values('$name','$nationality','$age')";
像这样写你的SQL:
line 21, in <module> average =
sum(student_info[student_name]/len(student_info))TypeError: 'float' object
is not iterable"
您可以从$ _POST请求设置旧变量,而不是从函数
设置答案 2 :(得分:0)
我认为这里有两个错误。 第一个是在这个调用中,你使用与上面相同的参数:
addApplicantPersonalDetails2($con,$name,$nationality,$age);
我猜你的意思是:
addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1);
其次,正如@FrayneKonok和@Richard指出的那样,你在函数体内的查询中使用了错误的形式参数。
答案 3 :(得分:0)
您正在接收$name,$nationality,$age
并在第二个函数'$nameJoint1','$nationJoint1','$ageJoint1'
插入addApplicantPersonalDetails2()
。
您需要对该功能使用相同的查询。 您的第二个查询应该是:
$query2 = "insert into zzz(name,nationality,age) values('$name','$nationality','$age')";
或者您可以使用'$nameJoint1','$nationJoint1','$ageJoint1'
答案 4 :(得分:0)
您可以使用nameMain
作为字段
nameJoint1
和name[]
在post.php
上,名称用户输入将属于$_POST['name']
数组。
这样您就可以使用for循环来提交所有这些数据。
所以你的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="name[]" class="form-control" autocomplete="off" required></td>
<td><input type="text" id="nameJoint1" name="name[]" class="form-control" autocomplete="off" required></td>
</tr>
<tr>
<td>Nationality(Country)</td>
<td><input type="text" id="nationMain" name="nation[]" class="form-control" autocomplete="off" required></td>
<td><input type="text" id="nationJoint1" name="nation[]" class="form-control" autocomplete="off" required></td>
</tr>
<tr>
<td>Age</td>
<td><input type="number" id="ageMain" name="age[]" class="form-control" autocomplete="off" required></td>
<td><input type="number" id="ageJoint1" name="age[]" class="form-control" autocomplete="off" required></td>
</tr>
然后您的功能将如下所示:
function addApplicantPersonalDetails($con,$name,$nationality,$age){
if(count($name) == count($nationality) == count($age) = $count){
for($i = 0; $i < $count; $i++){
$query = "insert into zzz(name,nationality,age)
values('$name[$i]','$nationality[$i]','$age[$i]')";
//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');
}
}
答案 5 :(得分:0)
<强> post.php中强>
更改变量名称
addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1);
<强> addCreditAssessment.php 强>
function addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1)