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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器