如何将值插入数据库?

时间:2018-04-01 17:35:41

标签: php mysqli

我有一个查询。我无法将任何值插入数据库。如果你能指出代码部分的错误,那将对我有很大的帮助。我得到了正确的json输出,但值没有插入db。请指导我

Register.php:

  $check_partnerEmail = $conn->query("SELECT partnerEmail FROM tbl_partner 
WHERE partnerEmail='$partnerEmail'");
 $partnerEmailCount=$check_partnerEmail->num_rows;

  if ($emailCount==0) {

$newPartner= "INSERT INTO tbl_partner(partnerFirstName, partnerLastName, partnerEmail, partnerPassword, partnerContact, partnerAddress, partnerPracticeArea, partnerExperience, partnerFirmName, partnerBankName, partnerBankRNumber, partnerBankAcNumber, partnerBankAcType, partnerLicenseState, partnerLicenseIdNumber, partnerYearAdmitted) VALUES('$partnerFirstName','$partnerLastName','$partnerEmail','$hashedClientPassword','$partnerContact', '$partnerAddress', '$partnerPracticeArea', '$partnerExperience', '$partnerFirmName', '$partnerBankName', '$partnerBankRNumber', '$partnerBankAcNumber', '$partnerBankAcType', '$partnerLicenseState', '$partnerLicenseIdNumber', '$partnerYearAdmitted' )";

if ($newPartner=== false){
      echo "error!";
    $result = array();
    $result[] = array("status" => "Error");
      }else
    {
     echo "Your have been signed up - please now Log In";
     $result = array();
     $result[] = array("First Name" => $partnerFirstName, "Last Name" => $partnerLastName, "Email" => $partnerEmail, "Password" => $hashedClientPassword, "Contact" => $partnerContact, "Address" => $partnerAddress "Practice Area" => $partnerPracticeArea "Experience" => $partnerExperience "Firm Name" => $partnerFirmName "Bank Name" => $partnerBankName "AC Number" => $partnerBankAcNumber "Routing Number" => $partnerBankRNumber "AC Type" => $partnerBankAcType "State" => $partnerLicenseState "License ID" => $partnerLicenseIdNumber "Year Admitted" => $partnerYearAdmitted "status" => "success");
    } 

}else {

echo "Already Exists";
$result = array();
$result[] = array("status" => "Error");

 }

echo json_encode($result);

}

Connection.php

<?php
    $host="localhost";
    $username="root";
    $password="";
    $dbname="Test";

    $conn=mysqli_connect($host,$username,$password,$dbname);
    if(!$conn)
    {
        die("Error :".mysqli_error($conn));
    }
?>

Register.html

<!DOCTYPE HTML>  
<html>
<head>

</head>
<body>  

<h2>Reg User</h2>
<form method="post" action="clientRegister.php">
      <label>
        First Name:<input type="text" name="clientFirstName"><br/>
        Last Name:<input type="text" name="clientLastName"><br/>
        Email:<input type="text" name="clientEmail"><br/>
        Password:<input type="password" name="clientPassword"><br/>
        Confirm Password:<input type="password" name="clientCPassword"><br/>
        Contact:<input type="text" name="clientContact"><br/>
        <input type="submit" value="Register" name="submit">
      </label>
    </form>



</body>
</html>

2 个答案:

答案 0 :(得分:0)

1-您在此lin上有语法错误,在每个语句之间使用

 $result[] = array("First Name" => $partnerFirstName, "Last Name" => $partnerLastName, "Email" => $partnerEmail, "Password" => $hashedClientPassword, "Contact" => $partnerContact, "Address" => $partnerAddress "Practice Area" => $partnerPracticeArea "Experience" => $partnerExperience "Firm Name" => $partnerFirmName "Bank Name" => $partnerBankName "AC Number" => $partnerBankAcNumber "Routing Number" => $partnerBankRNumber "AC Type" => $partnerBankAcType "State" => $partnerLicenseState "License ID" => $partnerLicenseIdNumber "Year Admitted" => $partnerYearAdmitted "status" => "success");

这是真的

 $result[] = array("First Name" => $partnerFirstName, "Last Name" => $partnerLastName, "Email" => $partnerEmail, "Password" => $hashedClientPassword, "Contact" => $partnerContact, "Address" => $partnerAddress ,"Practice Area" => $partnerPracticeArea, "Experience" => $partnerExperience ,"Firm Name" => $partnerFirmName, "Bank Name" => $partnerBankName ,"AC Number" => $partnerBankAcNumber, "Routing Number" => $partnerBankRNumber, "AC Type" => $partnerBankAcType, "State" => $partnerLicenseState, "License ID" => $partnerLicenseIdNumber, "Year Admitted" => $partnerYearAdmitted, "status" => "success");

2 - 您在最后一行也有语法错误。删除}

3 - 在注册php文件中包含连接数据库文件以进行执行查询

include 'Connection.php';
.
.
.
    $newPartner = "INSERT INTO tbl_partner(partnerFirstName, partnerLastName, partnerEmail, partnerPassword, partnerContact, partnerAddress, partnerPracticeArea, partnerExperience, partnerFirmName, partnerBankName, partnerBankRNumber, partnerBankAcNumber, partnerBankAcType, partnerLicenseState, partnerLicenseIdNumber, partnerYearAdmitted) VALUES('$partnerFirstName','$partnerLastName','$partnerEmail','$hashedClientPassword','$partnerContact', '$partnerAddress', '$partnerPracticeArea', '$partnerExperience', '$partnerFirmName', '$partnerBankName', '$partnerBankRNumber', '$partnerBankAcNumber', '$partnerBankAcType', '$partnerLicenseState', '$partnerLicenseIdNumber', '$partnerYearAdmitted' )";
          if ($conn->query($newPartner) === TRUE) {
              echo "New record created successfully";
          } else {
              echo "Error: " . $newPartner . "<br>" . $conn->error;
          }

.
.
.

答案 1 :(得分:-1)

使用$ result = con&gt;查询($ newpartner)执行查询$ newpartner;