I have just moved servers after a crash and PHP has been upgraded from 7.4 to 8.1 and I am getting the below error message when trying to submit a registration form;
Fatal error: Uncaught PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count
I have checked the table names, the amount of ?'s and insert query both add up to 37 as per the database.
I know it's something simple, but can't spot the error, been looking at the code for 3 hours and hopefully a fresh pair of eyes can see something :)
My code is
$folder = "uploads/";
$image = $_FILES['image']['name'];
$path = $folder . uniqid().$image ;
$target_file= $folder.basename($_FILES["image"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$allowed = array('jpeg','png','PNG','JPG' ,'jpg','gif');
$filename = $_FILES['image']['name'];
$ext=pathinfo($filename, PATHINFO_EXTENSION); if(!in_array($ext,$allowed) )
{
$msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
// var_dump($msg);
}
else{
move_uploaded_file( $_FILES['image'] ['tmp_name'], $path);
// var_dump($path);
$imageSource = $path;
}
$id = isset($_POST['id']) && !empty($_POST['id']) && $_POST['id'] != 'auto' ? $_POST['id'] : NULL;
$status = $_POST['status'];
$learner_id = $_POST['learner_id'];
$title = $_POST['title'];
$name = $_POST['name'];
$last_name = $_POST['last_name'];
$sex = $_POST['sex'];
$dob = $_POST['dob'];
$house_number = $_POST['house_number'];
$address_line_one = $_POST['address_line_one'];
$address_line_two = $_POST['address_line_two'];
$city_town = $_POST['city_town'];
$country = $_POST['country'];
$postcode = $_POST['postcode'];
$postcode_enrolment = $_POST['postcode_enrolment'];
$phone = $_POST['phone'];
$mobile_number = $_POST['mobile_number'];
$email_address = $_POST['email_address'];
$ethnic_origin = $_POST['ethnic_origin'];
$ethnicity = $_POST['ethnicitys'];
$health_problem = $_POST['health_problem'];
$health_disability_problem_if_yes = isset($_POST['health_disability_problem_if_yes']) ? json_encode($_POST['health_disability_problem_if_yes']) : '';
$lldd = $_POST['lldd'];
$education_health_care_plan = $_POST['education_health_care_plan'];
$health_problem_start_date = $_POST['health_problem_start_date'];
$education_entry = $_POST['education_entry'];
$emergency_contact_details = $_POST['emergency_contact_details'];
$employment_paid_status = $_POST['employment_paid_status'];
$employment_date = $_POST['employment_date'];
$unemployed_month = $_POST['unemployed_month'];
$education_training_prior = $_POST['education_training_prior'];
$education_claiming = $_POST['education_claiming'];
$claiming_if_yes = $_POST['claiming_if_yes'];
$household_situation = $_POST['household_situation'];
$stmt = $pdo->prepare('INSERT INTO contacts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$result = $stmt->execute([$id, $status, $path, date("Y/m/d"), $learner_id, $title, $name, $last_name, $sex, $dob, $house_number, $address_line_one, $address_line_two, $city_town, $country, $postcode, $postcode_enrolment, $phone, $mobile_number, $email_address, $ethnic_origin, $ethnicity, $health_problem, $health_disability_problem_if_yes, $lldd, $education_health_care_plan, $health_problem_start_date, $education_entry, $emergency_contact_details, $employment_paid_status, $employment_date, $unemployed_month, $education_training_prior, $education_claiming, $claiming_if_yes, $household_situation, null]);
I know this code is slightly older, but a new system is been built but not ready till later next year.
Database
`id` int(11) NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'Referral' COMMENT 'status is Referral,InProgress,S2,S3,NoContact,Old,Declined',
`image` varchar(225) DEFAULT NULL,
`datemovedin` date DEFAULT NULL,
`learner_id` varchar(100) DEFAULT NULL,
`title` varchar(225) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`sex` varchar(50) DEFAULT NULL,
`dob` varchar(50) DEFAULT NULL,
`house_number` varchar(225) DEFAULT NULL,
`address_line_one` varchar(225) DEFAULT NULL,
`address_line_two` varchar(225) DEFAULT NULL,
`city_town` varchar(50) DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`postcode` varchar(50) DEFAULT NULL,
`postcode_enrolment` varchar(50) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
`mobile_number` varchar(50) DEFAULT NULL,
`email_address` varchar(225) DEFAULT NULL,
`ethnic_origin` varchar(225) DEFAULT NULL,
`ethnicitys` varchar(225) DEFAULT NULL,
`health_problem` varchar(225) DEFAULT NULL,
`health_disability_problem_if_yes` text DEFAULT NULL,
`lldd` text DEFAULT NULL,
`education_health_care_plan` varchar(225) DEFAULT NULL,
`health_problem_start_date` varchar(225) DEFAULT NULL,
`education_entry` varchar(225) DEFAULT NULL,
`emergency_contact_details` varchar(225) DEFAULT NULL,
`employment_paid_status` varchar(225) DEFAULT NULL,
`employment_date` varchar(225) DEFAULT NULL,
`unemployed_month` varchar(225) DEFAULT NULL,
`education_training_prior` varchar(225) DEFAULT NULL,
`education_claiming` varchar(225) DEFAULT NULL,
`claiming_if_yes` varchar(225) DEFAULT NULL,
`household_situation` varchar(225) DEFAULT NULL,
`mentor` int(11) DEFAULT NULL
The issue is with your insert statement.
You're only defining the values but not the column definitions.
$stmt = $pdo->prepare('INSERT INTO contacts (id, status, .... the rest of the columns here) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
Also.. it's generally a bad practice to insert with the id defined unless you absolutely mean to do so.
The best practice would be to only define the columns you need.
$stmt = $pdo->prepare('INSERT INTO contacts (status, image, datemovedin, learner_id) VALUES (?, ?, ?, ?)');
$result = $stmt->execute([$status, $path, date("Y/m/d"), $learner_id]);
Problem is here.
$stmt = $pdo->prepare('INSERT INTO contacts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$result = $stmt->execute([$id, $status, $path, date("Y/m/d"), $learner_id, $title, $name, $last_name, $sex, $dob, $house_number, $address_line_one, $address_line_two, $city_town, $country, $postcode, $postcode_enrolment, $phone, $mobile_number, $email_address, $ethnic_origin, $ethnicity, $health_problem, $health_disability_problem_if_yes, $lldd, $education_health_care_plan, $health_problem_start_date, $education_entry, $emergency_contact_details, $employment_paid_status, $employment_date, $unemployed_month, $education_training_prior, $education_claiming, $claiming_if_yes, $household_situation, null]);
Count'svalues (?)
isn't equal withexecuted
variables count. You need to send variables equal to value bound.
Second reason:
You need to exact column names with determines. For example:
INSERT INTO project (category, title, name) VALUES (?,?,?)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.