PHP-为什么我的数据被插入mySQL表两次?

时间:2017-02-05 22:52:20

标签: php mysql mysqli phpmyadmin

当我运行下面的php文件时,它会将数据插入mysql表两次。我不会在任何地方再次运行insert命令,两个条目都有唯一ID。有什么想法吗?

我的愚蠢在这里:

<?php
require "conn.php";
require "stripe-php-4.4.0/init.php";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phonenum = $_POST['phonenum'];
$pin = $_POST['pin'];
$device = $_POST['device'];
$timeout = $_POST['timeout'];
$token = $_POST['stripeToken'];


// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("secretkey");

// Create a Customer:
$customer = \Stripe\Customer::create(array(
  "email" => $email,
  "source" => $token,
));

// Save the customer ID and other info in a database for later.
$newCustID = $customer->id;
$insert_new_user = "INSERT INTO zeus_borrowers (fname,lname,email,phonenum,pin,stripecusid,timeout,device) VALUES ('$fname','$lname','$email','$phonenum','$pin','$newCustID','$timeout','$device')";
$result = mysqli_query($conn ,$insert_new_user);

if($conn->query($insert_new_user) === TRUE){
}
else{
    echo "Error: " . $insert_new_user . "<br>" . $conn->error;
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

在if语句中,它再次插入行。

解决方案:

(1)删除if语句。问题:现在不会检查错误。

(2)放置模具();之后

$result = mysqli_query($conn ,$insert_new_user);

问题:现在不会检查错误。

(3)最佳选择:删除行

$result = mysqli_query($conn ,$insert_new_user);

如果语句将查询并检查错误。