Home > OS >  Unable to use PHP form after server move
Unable to use PHP form after server move

Time:11-09

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

CodePudding user response:

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]);

CodePudding user response:

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's values (?) isn't equal with executed 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 (?,?,?)
  •  Tags:  
  • php
  • Related