将数据插入MySql时出错

时间:2017-07-04 03:42:00

标签: php html mysql database

我有1个表,我需要一次插入两个客户端详细信息。但是,将第二个客户端的数据插入MySql时出错。此外,当我第二次插入时,两个结果都无法键入。 MySql表: enter image description here

这是Html表:

   <form name="creditAssessPage2" id="basicform" method="post" action="post.php" enctype="multipart/form-data">
         <tr>
             <td>Occupation</td>
             <td><input type="text"   id="occupationMain" name="occupationMain" class="form-control" autocomplete="off" ></td>
             <td><input type="text"   id="occupationJoint1" name="occupationJoint1" class="form-control" autocomplete="off" ></td>
        </tr>
        <tr>
            <td>Employment</td>
            <td contenteditable="true">
            <input list="employTypeList" name="employTypeMain" id="employTypeMain" class="form-control">
                 <datalist id="employTypeList">
                      <option value="">
                      <option value="Self-employed">
                      <option value="Employed">
                      <option value="Unemployed">
                      <option value="Retired">
                 </datalist>
            </td>
            <td contenteditable="true">
                <input list="employTypeList" name="employTypeJoint1" id="employTypeJoint1" class="form-control">
                <datalist id="employTypeList">
                     <option value="">
                     <option value="Self-employed">
                     <option value="Employed">
                     <option value="Unemployed">
                     <option value="Retired">
                </datalist>
            </td>
        </tr>
        <tr>
            <td>Company</td>
            <td><input type="text"   id="companyMain" name="companyMain" class="form-control" autocomplete="off" ></td>
            <td><input type="text"   id="companyJoint1" name="companyJoint1" class="form-control" autocomplete="off" ></td>
        </tr>
        <div class="form-group">
<button type="submit" name="submit" class="btn btn-default">Create</button><br/>
</div>
    </form>

这是我的post.php代码:

<?php 

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

session_start();
$con = open_connection();



$occupationMain = $_POST['occupationMain'];
$employTypeMain = $_POST['employTypeMain'];
$companyMain = $_POST['companyMain'];

$occupationJoint1 = $_POST['occupationJoint1'];
$employTypeJoint1 = $_POST['employTypeJoint1'];
$companyJoint1 = $_POST['companyJoint1'];

addemployementdetails($con,$occupationMain,$employTypeMain,$companyMain);
addemployementdetails2($con,$occupationJoint1,$employTypeJoint1,$companyJoint1);

close_connection($con);
?>

这是dbCreditAssessment.php的代码:

<?php

function addemployementdetails($con,$occupationMain,$employTypeMain,$companyMain){

    $query = "insert into employementdetails(Occupation,EmploymentStatus,NameOfBusiness) 
            values('$occupationMain','$employTypeMain','$companyMain')";            
                 //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 addemployementdetails2($con,$occupationJoint1,$employTypeJoint1,$companyJoint1){
    $query2 = "insert into employementdetails(Occupation,EmploymentStatus,NameOfBusiness)
            values('$occupationJoint1','$employTypeJoint1','$companyJoint1')"; 


                 $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');
                 }
}

2 个答案:

答案 0 :(得分:1)

实际上,您的表格具有 EmploymentDetailsID 属性作为主键,每个条目必须具有唯一键。但是您没有在每个条目中插入唯一值。现在解决这个问题的方法如下......

更改表格并生成 EmploymentDetailsID 自动增量列。为此,您可以使用下面给出的代码......

  

ALTER TABLE employementdetails MODIFY COLUMN EmploymentDetailsID varchar auto_increment

答案 1 :(得分:0)

问题是MySql表的设计,employementdetails表需要有AI ID。在这种情况下,必须自动生成employementdetailsID。