无法获取PHP代码以将信用卡信息插入数据库

时间:2018-12-02 02:39:23

标签: php html sql

所以,我真的很难在这里调试我的代码。我正在尝试完成一个简单的图书电子商务网站,在该网站上,用户在选择要购买的图书后输入信用卡信息。

这是我的代码:

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) die("Could not connect to Database");

$card_num = $_POST['card_num'];
$exp_date = $_POST['exp_date'];
$card_type = $_POST['card_type'];
$cvv = $_POST['cvv_code'];
$cust_ID = $_SESSION['cust_ID'];
$purch_amount = $_POST['purch_amt'];

if(!$card_result)
{
     print( "<p>Could not save card information!</p>" ); 
     die( mysql_error() . "</body></html>" );
}
$purch_query = "SELECT Inventory_Amount FROM Product WHERE Product_ID=".$_SESSION['prod_ID']."";
$item_amount = $conn->query($purch_query);

if(!$item_amount)
{
    print( "<p>Could not execute query!</p>" ); 
    die( mysql_error() . "</body></html>" );
}

if($item_amount == 0 || $purch_amount > $item_amount)
{
    echo "<p><strong>Not Enough Product in Stock. <a href='books.php'>Click Here to continue shopping</a></strong></p>";
}   
else
{
    $card_query = "INSERT INTO CreditCard (Customer_ID, CreditCard_Number, CreditCard_Expiration_Date, CreditCard_Type, CreditCard_CVV_Code)
                        VALUES(?,?,?,?,?);";
    $card_result = $conn->prepare($card_query);
    $card_result->bind_param("isssi", $cust_ID, $card_num, $exp_date, $card_type, $cvv);
    $card_result->execute();

    if(!$card_result)
    {
         print( "<p>Could not save card information!</p>" ); 
         die( mysql_error() . "</body></html>" );
    }
    $new_amount = $item_amount - $purch_amount;
    $purch_query2 = "UPDATE Product SET Inventory_Amount=".$new_amount. " WHERE Product_ID=".$prodID."";
    $prod_result = $conn->query($purch_query2);

    if(!$item_amount)
    {
         print( "<p>Could not update inventory!</p>" ); 
         die( mysql_error() . "</body></html>" );
    }

}

在我尝试将卡信息插入数据库后,出现“无法保存卡信息”错误。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

所以,这对我来说是一个愚蠢的错误。数据库名称不正确。

答案 1 :(得分:-2)

我将删除该查询引号内的分号。

发件人:

process.exit(0)

收件人:

Public valor As Integer

Sub Main()

    valor = 0
    Call Increment
    MsgBox valor 'expeted 30
End Sub

Sub Increment()
    valor = 30
End Sub

测试查询是否添加行的更好方法是检查查询对象的响应主体,以查看是否存在受影响的行。

像这样:

public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        BigDecimal bd = new BigDecimal(Double.toString(value));
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
}

希望这会有所帮助。