所以我制作了一个程序来验证卡号。所以现在我想将卡号和其他有用的数据一起存储在mysql表上,但信用卡号没有出现在表上..它只是显示0 ,我将数据类型更改为varchar,现在它只显示数组,我在php页面上收到此错误“在第38行的C:\ xamppp \ htdocs \ cpu5001 \ cards.php中的数组到字符串转换”..这是我的代码:
<style>
#park{
border-radius: 25px;
background:#D0D3D4;
height: 750px;
padding-top: 10px;
margin-top:10px;
}
</style>
<?php
session_start();
$ids=$_SESSION['tsmUserName'];
if (isset($_POST['submit'])){
$number=$_POST['cc'];
$expiray=$_POST['expire'];
$Cardholder=$_POST['cardholder'];
$country=$_POST['country'];
$total=0;
$i=1;
$last4= substr($number,-4,4);
$number=str_split($number);
$number=array_reverse($number);
foreach($number as $digit){
if($i%2==0){
$digit*=2;
if($digit>9){
$digit -=9;
}
}
$total += $digit;
$i++;
}
if($total%10==0){
echo "Your credit card number ending in ".$last4." is valid";
require_once("connection.php");
$my_query="INSERT INTO `card`(`No`, `Username`, `CreditCard`, `ExpirationDate`, `CardHolderName`, `Country`) VALUES (NULL,'$ids','$number','$expiray','$Cardholder','$country')";
$result=mysqli_query($connection,$my_query);
if($result)
{
echo 'thank you';
}
else
{
echo 'error';
}
mysqli_close($connection);
}
else
{
echo "Your credit card number ending in ".$last4." is invalid";
}
}
?>
<html>
<head>
<title>Credit Card Number</title>
</head>
<body>
<label style="margin-left:630px; font-size: 1.6em;"> Credit card info </label>
<div id="park">
<div id="info" style="background:#F5F5DC; width:500px;height:570px; margin-left:450px;border-radius: 25px;margin-top:150px; ">
<img src="credit_2.PNG" style="margin-top:-130px; margin-left:120px;">
<form action="cards.php" method='POST'>
</br></br>
<label style="margin-left:192px; font-size:1.5em;"> Credit Number </label>
</br></br>
<input type="text" name="cc" style="margin-left:150px; width:210px;">
</br></br>
<label style="margin-left:200px; font-size:1.5em;"> Expiray date </label>
</br></br>
<input type="date" name="expire" style="margin-left:150px; width:210px;">
</br></br>
<label style="margin-left:190px; font-size:1.5em;"> Card Holder </label>
</br></br>
<input type="text" name="cardholder" style="margin-left:150px; width:210px;">
</br></br>
<label style="margin-left:210px;font-size:1.5em;"> Country </label>
</br></br>
<input type="text" name="country" style="margin-left:150px; width:210px;">
</br></br>
<input type="submit" name="submit" style="margin-left:230px;">
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
Vitek没错 - 您将字符串$ number转换为带有str_split的数组,所以也许这样做(使用$ numbers而不是$ number):
$numbers=str_split($number);
$numbers=array_reverse($number);
foreach($numbers as $digit){
if($i%2==0){
$digit*=2;
if($digit>9){
$digit -=9;
}
}
$total += $digit;
$i++;
}
正如其他人所提到的,还有几个问题需要提及:
希望这有帮助。
答案 1 :(得分:0)
当您执行$number
时,您已将$number=str_split($number);
重新分配给数组。这样做,你应该没事:
$numberChecker=str_split($number);
$numberChecker=array_reverse($number);
foreach($numberChecker as $digit){
...
}
旁注:你真的应该使用MySQLi prepared statements。