数据没有使用mysqli和php插入数据库

时间:2017-02-17 14:42:26

标签: php mysql mysqli http-post sql-insert

我正在尝试将POST的内容写入我们的mysql数据库。数据似乎没有插入,我没有收到任何错误。我正在使用的db登录也具有远程访问权限。查询本身从变量中获取正确的数据。所以我不确定我做错了什么。

我将查询的值写入error_log,当我通过workbench直接插入时,插入成功。

   [17-Feb-2017 07:02:47 America/Los_Angeles] INSERT INTO         
   consolidated.ACI_BNF (ConsumerName, AccountNumber,    
   NotificationType, ConfNumber, PaymentDate, PaymentAmount, 
   PaymentFee, FundingType, PaymentMethod, ProductMethod, 
   ImportDate)
   VALUES ('JOHN DOE', '00123546', 'CREATE', '000123456',    '2017-02-17', '444.37', '4.95 ', 'CREDIT CARD', '', 'ONE_TIME_PAY', '2017-02-17T10:02:47')

PHP代码

$hostname_Database = "somedb";
$database_Database = "consolidated";
$username_Database = "username";
$password_Database = "password";

$mysqli = new mysqli($hostname_Database, $username_Database,$password_Database, $database_Database); 

$sql = "INSERT INTO consolidated.ACI_BNF (ConsumerName, AccountNumber, NotificationType, ConfNumber, PaymentDate, PaymentAmount, PaymentFee,  
FundingType, PaymentMethod, ProductMethod, ImportDate)
VALUES ('$AccountHolderName', '$accountNumber', '$notification',   
'$ConfirmationNumber', '$PaymentDate', '$PaymentAmount', '$PaymentFee',   
'$FundingType', '$PaymentMethod', '$ProductMethod', '$ImportDate')";


   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
     }

   $result = $mysqli->query($sql); 
   if (!$result) {
    printf("%s\n", $mysqli->error);
    exit();
    }

     echo "Query run. Inserted UserID " . $mysqli->insert_id . "<br />";


        mysqli_close($mysqli);

2 个答案:

答案 0 :(得分:0)

您应该使用:

$mysqli->connect_errno

而不是:

mysqli_connect_errno()

答案 1 :(得分:0)

也许你忘了写提交的名字。例如:

in html:

     <input type="submit" name="submit" >

在php中:

    if(isset($_POST["submit"})){
           $user = $_POST["username"];
    }
    ...