I am building a speaker form for my website which basically just takes the inputted data from the user and puts it in the database. The website also allows you to save or submit the form and I check that using
if(isset($_POST['save'])
The problem I getting is, when i leave some text boxes blank and hit "save", the database gives an error saying this:
( ! ) PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'phone_number' at row 1 in C:\wamp64\wwwotarywebsite\speakerform.php on line 98
I checked the code and the database to see if there was something i misspelled or anything else wrong in the database that I found research on. So basically I want the users to be allowed to "save" the form while also leaving some text boxes blank. Here is my code and SQL format
PHP code
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;
Since phone_number
is declared as an INT
field, you have to provide a valid integer value for it, and an empty string is not a valid integer.
You could replace empty strings with 0
.
$phone_number = trim($_POST['phone_number']);
if ($phone_number == '') {
$phone_number = 0;
}
But the best solution would be to change this field in the database to VARCHAR
. Phone numbers are not really numbers, they often have other characters, e.g. +1 (202) 555-1212
.
You have all fields in the DB set to NOT NULL. This means that an insert statement must contain a valid value for every single field. You will need to drop the NOT NULL constraints from the those fields that you want to be optional.
Try removing the NOT NULL from the attributes you want to keep empty at times. eg- try changing - fullname
varchar(200) NOT NULL, phone_number
int(200) NOT NULL, to - fullname
varchar(200) , phone_number
int (200),