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

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

问题描述:

I've built a form in PHP that takes data and inputs it into a SQL database. The database is working fine as a whole, and the other portions of the system are working. In this particular form, most of the data is being input. However, for no discernible reason, the information from a few particular fields isn't. My table is described like this:

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));

and the code for the form is:

<?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));
}
?>

I've tested it by entering the information through the form and then doing a select * from customers. USstate is left entirely blank, creditNum is always filled in with 214748364 no matter what I enter, and creditRating is specifically null. However, if I call it through a normal INSERT statement, everything is entered normally. Where did everything go so wrong?

creditNum is always filled in with 214748364 no matter what I enter

well 2147483647 (i guess you missed one digit) is maximum value for SIGNED INT(12)

2147483647...... max SIGNED INT(12)

4012888888881881 example card number

you should use VARCHAR(16) or longer for credit card numbers


Regarding credit rating issue:

you use two variables $creditRating and $creditRate. use only one.