PHP表单输入字段无法正确保存到数据库

时间:2017-04-16 21:08:45

标签: php mysql

我在PHP中构建了一个表单,它接收数据并将其输入到SQL数据库中。整个数据库工作正常,系统的其他部分正在工作。在这种特定形式中,大多数数据都是输入的。但是,由于没有明显的原因,来自几个特定领域的信息不是。我的表格描述如下:

create table customer
(custid int auto_increment primary key,
fname varchar(30),
lname varchar(30),
address varchar(30),
city varchar(30),
USstate varchar(30),
zip int,
income decimal(15,2),
employment varchar(30),
creditNum int(12),
creditRating decimal(15,2));

并且表单的代码是:

<?php
$db = new PDO('mysql:host=[hostname];dbname=[dbname];charset=[charname'], 
[username], [password]);
echo '<form action="enterCustomer.php" method="post">';
echo 'First name: <input type="text" name="fname" id="fname" hint="first name"><br>';
echo 'Last name: <input type="text" name="lname" id="lname" hint="last name"><br>';
echo 'Income: $<input type="text" name="income" id="income" hint="income"><br>';
echo 'Employment: <input type="text" name="employment" id="employment" hint="employment"><br>';
echo 'Credit Card: <input type="text" name="creditNum" id="creditNum" hint="credit card"><br>';
echo 'Credit Rating: <input type="text" name="creditRating" id="creditRating" hint="credit rating"><br>';
echo 'Address: <input type="text" name="address" id="address" hint="address"><br>';
echo 'City: <input type="text" name="city" id="city" hint="city"><br>';
echo 'State: <input type="text" name="USstate" id="USstate" hint="state"><br>';
echo 'Zip code: <input type="text" name="zip" id="zip" hint="zip code"><br>';
echo '<input type="submit" name="submit" value="Create Account"><br>';
echo '<a href="assn5main.html">Back</a>';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $fname = trim($_POST['fname']);
    $lname = trim($_POST['lname']);
    $income = trim($_POST['income']);
    $employment = trim($_POST['employment']);
    $creditNum = trim($_POST['creditNum']);
    $creditRate = trim($_POST['creditRating']);
    $address = trim($_POST['address']);
    $city = trim($_POST['city']);
    $USstate = trim($_POST['state']);
    $zip = trim($_POST['zip']);
    $stmt = $db->prepare("INSERT INTO customer(fname,lname,address,city,USstate,zip,income,employment,creditNum,creditRating) VALUES(:fname,:lname,:address,:city,:USstate,:zip,:income,:employment,:creditNum,:creditRating)");
$stmt->execute(array(':fname' => $fname, ':lname' => $lname, ':address' => $address, ':city' => $city, ':USstate' => $USstate, ':zip' => $zip, ':income' => $income, ':employment' => $employment, ':creditNum' => $creditNum, ':creditRating' => $creditRating));
}
?>

我通过表单输入信息然后从客户那里做一个select *来测试它。 USstate完全空白,无论我输入什么,creditNum总是用214748364填充,而creditRating特别为null。但是,如果我通过普通的INSERT语句调用它,则所有内容都会正常输入。一切都变得如此错误?

1 个答案:

答案 0 :(得分:3)

  不管我输入什么,

creditNum总是用214748364填写

好2147483647(我猜你错过了一位数)SIGNED INT(12)2147483647...... max SIGNED INT(12) 4012888888881881 example card number

VARCHAR(16)

您应使用$creditRating或更长时间的信用卡号

关于信用评级问题:

您使用两个变量$creditRateNode lastPeek=pq.peek(); tempList3.add(lastPeek.n1.get(n1.size()-1)); 。只使用一个。