我正在为我的网站构建一个扬声器表单,它基本上只是从用户那里获取输入的数据并将其放入数据库中。该网站还允许您保存或提交表格,我使用
检查if(isset($_POST['save'])
我遇到的问题是,当我将一些文本框留空并点击" save"时,数据库会出错说:
( ! ) PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'phone_number' at row 1 in C:\wamp64\www\rotarywebsite\speakerform.php on line 98
我检查了代码和数据库,看看我发现研究中是否有错误拼写或其他任何错误的数据库。所以基本上我希望允许用户"保存"表格同时也留下一些文本框空白。这是我的代码和SQL格式
PHP代码
if(isset($_POST['save'])){
$fullname = trim($_POST['fullname']);
$phone_number = trim($_POST['phone-number']);
$cell_day_of = trim($_POST['cell-dayof']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
$city = trim($_POST['city']);
$state = trim($_POST['state']);
$zip = trim($_POST['zip']);
$titles = trim($_POST['titles']);
$company = trim($_POST['company']);
$affiliations = trim($_POST['affiliations']);
$website = trim($_POST['website']);
$topic = trim($_POST['topic']);
$summary = trim($_POST['summary']);
$availability = trim($_POST['availability']);
$notice = trim($_POST['notice']);
$guest = trim($_POST['guest']);
$materials = trim($_POST['materials']);
$projector = trim($_POST['projector']);
$equipment = trim($_POST['equipment']);
$educating = trim($_POST['educating']);
$support = trim($_POST['support']);
$international = trim($_POST['international']);
$familiar = trim($_POST['familiar']);
$member = trim($_POST['member']);
$allergies = trim($_POST['allergies']);
$bio = trim($_POST['bio']);
$introduce = trim($_POST['introduce']);
$last_need = trim($_POST['last-need']);
$timestamp = time();
$stmtUpdate = $handler->prepare("INSERT INTO formdata (user_id, fullname, phone_number, cell_dayof, email, address, city, state, zip, titles, company, affiliations, website, topic, summary, availability, notice, guest, materials, projector, equipment, educating, support, international, familiar, member, allergies, bio, introduce, last_need, time_stamp)VALUES(:userid, :fullname, :phone_number, :cell_dayof, :email, :address, :city, :state, :zip, :titles, :company, :affiliations, :website, :topic, :summary, :availability, :notice, :guest, :materials, :projector, :equipment, :educating, :support, :international, :familiar, :member, :allergies, :bio, :introduce, :last_need, :timestamp)");
$stmtUpdate->bindParam(':userid', $userid, PDO::PARAM_INT);
$stmtUpdate->bindParam(':fullname', $fullname, PDO::PARAM_STR);
$stmtUpdate->bindParam(':phone_number', $phone_number, PDO::PARAM_INT);
$stmtUpdate->bindParam(':cell_dayof', $cell_dayof, PDO::PARAM_INT);
$stmtUpdate->bindParam(':email', $email, PDO::PARAM_STR);
$stmtUpdate->bindParam(':address', $address, PDO::PARAM_STR);
$stmtUpdate->bindParam(':city', $city, PDO::PARAM_STR);
$stmtUpdate->bindParam(':state', $state, PDO::PARAM_STR);
$stmtUpdate->bindParam(':zip', $zip, PDO::PARAM_INT);
$stmtUpdate->bindParam(':titles', $titles, PDO::PARAM_STR);
$stmtUpdate->bindParam(':company', $company, PDO::PARAM_STR);
$stmtUpdate->bindParam(':affiliations', $affiliations, PDO::PARAM_STR);
$stmtUpdate->bindParam(':website', $website, PDO::PARAM_STR);
$stmtUpdate->bindParam(':topic', $topic, PDO::PARAM_STR);
$stmtUpdate->bindParam(':summary', $summary, PDO::PARAM_STR);
$stmtUpdate->bindParam(':availability', $availability, PDO::PARAM_STR);
$stmtUpdate->bindParam(':notice', $notice, PDO::PARAM_STR);
$stmtUpdate->bindParam(':guest', $guest, PDO::PARAM_STR);
$stmtUpdate->bindParam(':materials', $materials, PDO::PARAM_STR);
$stmtUpdate->bindParam(':projector', $projector, PDO::PARAM_STR);
$stmtUpdate->bindParam(':equipment', $equipment, PDO::PARAM_STR);
$stmtUpdate->bindParam(':educating', $educating, PDO::PARAM_STR);
$stmtUpdate->bindParam(':support', $support, PDO::PARAM_STR);
$stmtUpdate->bindParam(':international', $international, PDO::PARAM_STR);
$stmtUpdate->bindParam(':familiar', $familiar, PDO::PARAM_STR);
$stmtUpdate->bindParam(':member', $member, PDO::PARAM_STR);
$stmtUpdate->bindParam(':allergies', $allergies, PDO::PARAM_STR);
$stmtUpdate->bindParam(':bio', $bio, PDO::PARAM_STR);
$stmtUpdate->bindParam(':introduce', $introduce, PDO::PARAM_STR);
$stmtUpdate->bindParam(':last_need', $last_need, PDO::PARAM_STR);
$stmtUpdate->bindParam(':timestamp', $timestamp, PDO::PARAM_INT);
$stmtUpdate->execute();
}
SQL
CREATE TABLE `formdata` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`fullname` varchar(200) NOT NULL,
`phone_number` int(200) NOT NULL,
`cell_dayof` int(200) NOT NULL,
`email` varchar(200) NOT NULL,
`address` varchar(300) NOT NULL,
`city` varchar(300) NOT NULL,
`state` varchar(300) NOT NULL,
`zip` int(200) NOT NULL,
`titles` varchar(200) NOT NULL,
`company` varchar(200) NOT NULL,
`affiliations` varchar(200) NOT NULL,
`website` varchar(200) NOT NULL,
`topic` varchar(200) NOT NULL,
`summary` varchar(200) NOT NULL,
`availability` varchar(200) NOT NULL,
`notice` varchar(200) NOT NULL,
`guest` varchar(200) NOT NULL,
`materials` varchar(200) NOT NULL,
`projector` varchar(200) NOT NULL,
`equipment` varchar(200) NOT NULL,
`educating` varchar(200) NOT NULL,
`support` varchar(200) NOT NULL,
`international` varchar(200) NOT NULL,
`familiar` varchar(200) NOT NULL,
`member` varchar(200) NOT NULL,
`allergies` varchar(200) NOT NULL,
`bio` varchar(200) NOT NULL,
`introduce` varchar(200) NOT NULL,
`last_need` varchar(200) NOT NULL,
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
答案 0 :(得分:1)
由于phone_number
被声明为INT
字段,因此必须为其提供有效的整数值,并且空字符串不是有效的整数。
您可以使用0
替换空字符串。
$phone_number = trim($_POST['phone_number']);
if ($phone_number == '') {
$phone_number = 0;
}
但最好的解决方案是将数据库中的此字段更改为VARCHAR
。电话号码不是真正的数字,它们通常有其他字符,例如+1 (202) 555-1212
。
答案 1 :(得分:0)
您将数据库中的所有字段设置为NOT NULL。这意味着insert语句必须包含每个字段的有效值。您需要从那些您想要选择的字段中删除NOT NULL约束。
答案 2 :(得分:0)
尝试从要保持为空的属性中删除NOT NULL。
例如-
尝试更改 - fullname
varchar(200)NOT NULL,
phone_number
int(200)NOT NULL,
到 -
fullname
varchar(200),
phone_number
int(200),