Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

php form to mysql database

$
0
0

I am having some trouble figuring out how to post data from a form into mysql table.Initially I tried to create an insert query with my 2 arrays like this:

$sql = "INSERT INTO tbl_form ($fields) VALUES ('$str')";

but it was complaining about array to string conversion php error.

Here is my php code:

$fields = ['requested_effective_date', 'employer_group_name', 'group_number', 'billing_mode', 'other', 'proposed_name_insured', 'ssn', 'member_number', 'sex', 'age','birth_date', 'daytime_phone_number', 'home_address', 'city', 'state', 'zip', 'jobtitle_occupation', 'hours_worked_per_week','date_hired', 'beneficiary_name_relationship', 'proposed_name_insured_activity', 'used_tobacco_products', 'email_address', 'spouse','sex_spouse', 'spouse_dob', 'children1', 'sex_children1', 'children1_dob1', 'children2', 'sex_children2', 'children2_dob2', 'children3', 'sex_children3', 'children3_dob3','children4', 'sex_children4', 'children4_dob4', 'plan_units_of_coverage', 'plan_modal_premium', 'total_premium_due','coverage_type', 'riders', 'section_125', 'name_of_company', 'replacement', 'plans_option'];foreach ($fields as $field) {    if (!isset($_POST[$field])) {        $_POST[$field] = null;    }}$formData = ['requested_effective_date' => $_POST['requested_effective_date'],'employer_group_name' => $_POST['employer_group_name'],'group_number' => $_POST['group_number'],'billing_mode' => $_POST['billing_mode'],'other' => $_POST['other'],'proposed_name_insured' => $_POST['proposed_name_insured'],'ssn' => $_POST['ssn'],'member_number' => $_POST['member_number'],'sex' => $_POST['sex'],'age' => $_POST['age'],'birth_date' => $_POST['birth_date'],'daytime_phone_number' => $_POST['daytime_phone_number'],'home_address' => $_POST['home_address'],'city' => $_POST['city'],'state' => $_POST['state'],'zip' => $_POST['zip'],'jobtitle_occupation' => $_POST['jobtitle_occupation'],'hours_worked_per_week' => $_POST['hours_worked_per_week'],'date_hired' => $_POST['date_hired'],'beneficiary_name_relationship' => $_POST['beneficiary_name_relationship'],'proposed_name_insured_activity' => $_POST['proposed_name_insured_activity'],'used_tobacco_products' => $_POST['used_tobacco_products'],'email_address' => $_POST['email_address'],'spouse' => $_POST['spouse'],'sex_spouse' => $_POST['sex_spouse'],'spouse_dob' => $_POST['spouse_dob'],'children1' => $_POST['children1'],'sex_children1' => $_POST['sex_children1'],'children1_dob1' => $_POST['children1_dob1'],'children2' => $_POST['children2'],'sex_children2' => $_POST['sex_children2'],'children2_dob2' => $_POST['children2_dob2'],'children3' => $_POST['children3'],'sex_children3' => $_POST['sex_children3'],'children3_dob3' => $_POST['children3_dob3'],'children4' => $_POST['children4'],'sex_children4' => $_POST['sex_children4'],'children4_dob4' => $_POST['children4_dob4'],'plan_units_of_coverage' => $_POST['plan_units_of_coverage'],'plan_modal_premium' => $_POST['plan_modal_premium'],'total_premium_due' => $_POST['total_premium_due'],'coverage_type' => $_POST['coverage_type'],'riders' => $_POST['riders'],'section_125' => $_POST['section_125'],'name_of_company' => $_POST['name_of_company'],'replacement' => $_POST['replacement'],'plans_option' => $_POST['plans_option']];$servername = "localhost";$username = "root";$password = "mysqlpass";// Create connection$conn = new mysqli($servername, $username, $password, 'benefits_form');// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}$str = implode(",", $formData);//echo "Connected successfully";//$str = json_encode($formData);//dd($str);//$sql = "INSERT INTO tbl_form (form_data) VALUES ('$str')";  $sql = "INSERT INTO tbl_form (requested_effective_date, employer_group_name, group_number, billing_mode, other, proposed_name_insured, ssn, member_number, sex, age,birth_date, daytime_phone_number, home_address, city, `state`, zip, jobtitle_occupation, hours_worked_per_week,date_hired, beneficiary_name_relationship, proposed_name_insured_activity, used_tobacco_products, email_address, spouse,sex_spouse, spouse_dob, children1, sex_children1, children1_dob1, children2, sex_children2, children2_dob2, children3, sex_children3, children3_dob3,children4, sex_children4, children4_dob4, plan_units_of_coverage, plan_modal_premium, total_premium_due,coverage_type, riders, section_125, name_of_company, replacement, plans_option) VALUES ('$str')";if (mysqli_query($conn, $sql)) {    echo "Record inserted successfully";} else {    echo "Could not insert record: " . mysqli_error($conn);}mysqli_close($conn);

mysql create table:

CREATE TABLE `tbl_form` (    `requested_effective_date` VARCHAR(50) NULL DEFAULT NULL COLLATE 'latin1_swedish_ci',    `employer_group_name` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `group_number` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `billing_mode` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `other` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `proposed_name_insured` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `ssn` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `member_number` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `age` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `birth_date` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `daytime_phone_number` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `home_address` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `city` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `state` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `zip` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `jobtitle_occupation` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `hours_worked_per_week` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `date_hired` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `beneficiary_name_relationship` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `proposed_name_insured_activity` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `used_tobacco_products` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `email_address` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `spouse` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex_spouse` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `spouse_dob` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children1` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex_children1` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children1_dob1` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children2` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex_children2` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children2_dob2` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children3` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex_children3` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children3_dob3` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children4` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `sex_children4` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `children4_dob4` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `plan_units_of_coverage` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `plan_modal_premium` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `total_premium_due` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `coverage_type` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `riders` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `section_125` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `name_of_company` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `replacement` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci',    `plans_option` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'latin1_swedish_ci')COLLATE='latin1_swedish_ci'ENGINE=InnoDB;

I keep getting message 'Could not insert record: Column count doesn't match value count at row 1' even though I have the same number of values for the number of columns.

I am expecting when a user enters data on the form and when they click submit form, the data should be saved on mysql table. Then my plan is to export this data into a csv file for further reporting.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>