Reputation: 211
I have 1 table and I want to insert 2 values into MySql. However, there is only one value goes into my database and the other remains empty.
My Html table code is below:
<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>
My post.php code is below:
<?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);
?>
My addCreditAssessment.php code is below:
<?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');
}
}
?>
Upvotes: 0
Views: 969
Reputation: 83
POST.PHP
Change the variable name
addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1);
addCreditAssessment.php
function addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1)
Upvotes: 0
Reputation: 9583
You are receiving $name,$nationality,$age
and insert '$nameJoint1','$nationJoint1','$ageJoint1'
at second function addApplicantPersonalDetails2()
.
You need to use the same query for both the function. Your second query should like:
$query2 = "insert into zzz(name,nationality,age) values('$name','$nationality','$age')";
Or you can change your function parameters with '$nameJoint1','$nationJoint1','$ageJoint1'
Upvotes: 0
Reputation: 753
Instead of using nameMain
and nameJoint1
, you can use name[]
for both field
On post.php
, the name user input will belong to $_POST['name']
array.
So that you can use for-loop to submit all these data.
So your html file will looks like this:
<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>
then your function will looks like:
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');
}
}
Upvotes: 0
Reputation: 138
You are using variable that are not define i the function that why empty
Do like this
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');
}
}
It will work for you.
Upvotes: 1
Reputation: 9568
I think there are two errors here. The first lies in this call, where you use the same parameters as the one above:
addApplicantPersonalDetails2($con,$name,$nationality,$age);
I guess what you mean is:
addApplicantPersonalDetails2($con,$nameJoint1,$nationJoint1,$ageJoint1);
Secondly, as @FrayneKonok and @Richard have pointed out, you are using the wrong formal parameters in the query inside the function's body.
Upvotes: 0
Reputation: 628
Your second function is wrong:
Your function is:
addApplicantPersonalDetails2($con,$name,$nationality,$age)
Write your SQL like this:
$query2 = "insert into zzz(name,nationality,age)
values('$name','$nationality','$age')";
You set the old variables from your $_POST request, not from your function
Upvotes: 0