duan4739 2017-04-16 21:08
浏览 91
已采纳

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?

  • 写回答

1条回答 默认 最新

  • douyi9705 2017-04-16 21:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!