[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Insert Data from Web Form into mysql Database table Not Working

Hello,

I am trying to insert data collected from a webform into a mysql database table.  Here is the code I am using:
 
 example-code.php

When I click the submit button I get this error:
Column count doesn't match value count at row 1

I counted to make sure the fields and values were the same.  I don't know why I get the error.

Your input is greatly appreciated.

Thanks!example-code.php
0
JParra72
Asked:
JParra72
  • 3
  • 2
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
Clearly it says , it does not match,

$sql_A = mysql_query("INSERT INTO r_gold (signupdate, FirstName, MiddleName, LastName, Gender, Social, Birthday, BirthCountry, BirthCity, BirthSate, CommPay, Firm, TaxId, BAddress, BCity, BState, BZip, RAddress, RCity, RState, RZip, BPhone, RPhone, CPhone, Fax, Email, Years, Months, Licensed, DetailsLicensed, Debt, DetailsDebt, Owner, DetailsOwner, Convicted, DetailsConvicted, SecLic, DetaislSeLic, TaxAut, DetailsTaxAut, Bond, DetailsBond, Lit, DetailsLit, Allias, DetailsAllias, EandO, Carrier, Policy, ExpDate, Numbers, Withhold, Citizen, Agree, Dispute, Acknow) VALUES

[b]53 columns[/b]

 ('$signupdate', '$txtFirstName', '$txtMiddleName', '$txtLastName', '$rdoGender', '$txtSocial', '$txtBirthday', '$txtBirthCountry', '$txtBirthCity', '$txtBirthState', '$rdoCommPay', '$txtFirm', '$txtTaxId', '$txtBAddress', '$txtBCity', '$mnuBState', '$txtBZip', '$txtRAddress', '$txtRCity', '$mnuRState', '$txtRZip', '$txtBPhone', '$txtRPhone', '$txtCPhone', '$txtFax', '$txtEmail', '$txtYears', '$txtMonths', '$rdoLicensed', '$txtDetailsLicensed', '$rdoDebt', '$txtDetailsDebt', '$rdoOwner', '$txtDetailsOwner', '$rdoConvicted', '$txtDetailsConvicted', '$rdoSecLic', '$txtDetailsSecLic', '$rdoTaxAuT', '$txtDetailsTaxAud', '$rdoBond', '$txtDetailsBond', '$rdoLit', '$txtDetailsLit' '$rdoAllias', '$txtDetailsAllias', '$rdoEandO', '$txtCarrier', '$txtPolicy', '$txtExpDate', '$rdoNumbers', '$rdoWithold', '$rdoCitizen', '$rdoAgree', '$rdoDispute', '$rdoAcknow')") or die (mysql_error());	

[b]56 columns[/b]

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
In your query it has 53 columns, but while you're passing variables are 56?

just cross check.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
sorry in your query it has 57 fields but in your variable it has 56?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
Here is the script (I think - there were two posted)

<?php 
     
 $con = mysql_connect("localhost","admin","sample_pword");
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }
 mysql_select_db("test_db1", $con);
	
	
	 $signupdate = date("d-M-y");
				     				   
				   //insert into db
				    $sql_A = mysql_query("INSERT INTO r_gold (signupdate, FirstName, MiddleName, LastName, Gender, Social, Birthday, BirthCountry, BirthCity, BirthSate, CommPay, Firm, TaxId, BAddress, BCity, BState, BZip, RAddress, RCity, RState, RZip, BPhone, RPhone, CPhone, Fax, Email, Years, Months, Licensed, DetailsLicensed, Debt, DetailsDebt, Owner, DetailsOwner, Convicted, DetailsConvicted, SecLic, DetaislSeLic, TaxAut, DetailsTaxAut, Bond, DetailsBond, Lit, DetailsLit, Allias, DetailsAllias, EandO, Carrier, Policy, ExpDate, Numbers, Withhold, Citizen, Agree, Dispute, Acknow) VALUES ('$signupdate', '$txtFirstName', '$txtMiddleName', '$txtLastName', '$rdoGender', '$txtSocial', '$txtBirthday', '$txtBirthCountry', '$txtBirthCity', '$txtBirthState', '$rdoCommPay', '$txtFirm', '$txtTaxId', '$txtBAddress', '$txtBCity', '$mnuBState', '$txtBZip', '$txtRAddress', '$txtRCity', '$mnuRState', '$txtRZip', '$txtBPhone', '$txtRPhone', '$txtCPhone', '$txtFax', '$txtEmail', '$txtYears', '$txtMonths', '$rdoLicensed', '$txtDetailsLicensed', '$rdoDebt', '$txtDetailsDebt', '$rdoOwner', '$txtDetailsOwner', '$rdoConvicted', '$txtDetailsConvicted', '$rdoSecLic', '$txtDetailsSecLic', '$rdoTaxAuT', '$txtDetailsTaxAud', '$rdoBond', '$txtDetailsBond', '$rdoLit', '$txtDetailsLit' '$rdoAllias', '$txtDetailsAllias', '$rdoEandO', '$txtCarrier', '$txtPolicy', '$txtExpDate', '$rdoNumbers', '$rdoWithold', '$rdoCitizen', '$rdoAgree', '$rdoDispute', '$rdoAcknow')") or die (mysql_error());					
				if(!$sql_A) echo 'There has been an error creating your account. Please contact the webmaster.';
		mysql_close($con);		
       ?>

Open in new window

If this creates a query string at all, I would be surprised.  If it does, your script relies on REGISTER GLOBALS and that is a catastrophe waiting to happen.  This article explains why.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7317-Register-Globals-a-bad-idea-from-day-one.html

The script may also rely on MAGIC QUOTES, which is another of PHP's worst ideas.  If it does not, then your query will fail if there is an apostrophe in any field.  You must use the PHP function mysql_real_escape_string() to prepare external variables for use in a query.  The magic quotes issues are discussed in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html

I recommend you consider getting this book.  It will help you get a foundation in PHP and MySQL.
http://www.sitepoint.com/books/phpmysql5/

Here is how you might want to create the query - in a separate variable instead of loaded into the function call.  With this you can print out the query and see the data fields!

<?php 
error_reporting(E_ALL);
$con = mysql_connect("localhost","admin","sample_pword");
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}
$x = mysql_select_db("test_db1", $con);
if (!$x) die( mysql_error());
	
// LEARN ABOUT THE ISO-8601 FORMAT DATE!	
$signupdate = date("d-M-y");
				     				   
// CREATE A QUERY STRING IN A SEPARATE VARIABLE
$sql_A = "INSERT INTO r_gold (signupdate, FirstName, MiddleName, LastName, Gender, Social, Birthday, BirthCountry, BirthCity, BirthSate, CommPay, Firm, TaxId, BAddress, BCity, BState, BZip, RAddress, RCity, RState, RZip, BPhone, RPhone, CPhone, Fax, Email, Years, Months, Licensed, DetailsLicensed, Debt, DetailsDebt, Owner, DetailsOwner, Convicted, DetailsConvicted, SecLic, DetaislSeLic, TaxAut, DetailsTaxAut, Bond, DetailsBond, Lit, DetailsLit, Allias, DetailsAllias, EandO, Carrier, Policy, ExpDate, Numbers, Withhold, Citizen, Agree, Dispute, Acknow) VALUES ('$signupdate', '$txtFirstName', '$txtMiddleName', '$txtLastName', '$rdoGender', '$txtSocial', '$txtBirthday', '$txtBirthCountry', '$txtBirthCity', '$txtBirthState', '$rdoCommPay', '$txtFirm', '$txtTaxId', '$txtBAddress', '$txtBCity', '$mnuBState', '$txtBZip', '$txtRAddress', '$txtRCity', '$mnuRState', '$txtRZip', '$txtBPhone', '$txtRPhone', '$txtCPhone', '$txtFax', '$txtEmail', '$txtYears', '$txtMonths', '$rdoLicensed', '$txtDetailsLicensed', '$rdoDebt', '$txtDetailsDebt', '$rdoOwner', '$txtDetailsOwner', '$rdoConvicted', '$txtDetailsConvicted', '$rdoSecLic', '$txtDetailsSecLic', '$rdoTaxAuT', '$txtDetailsTaxAud', '$rdoBond', '$txtDetailsBond', '$rdoLit', '$txtDetailsLit' '$rdoAllias', '$txtDetailsAllias', '$rdoEandO', '$txtCarrier', '$txtPolicy', '$txtExpDate', '$rdoNumbers', '$rdoWithold', '$rdoCitizen', '$rdoAgree', '$rdoDispute', '$rdoAcknow')";
$res_A = mysql_query($sql_A);					
if(!$res_A)
{
    echo "FAIL: $sql_A <br/>BECAUSE: " . mysql_error();
}

Open in new window

Best of luck with the project, ~Ray
0
 
JParra72Author Commented:
Thanks very much!  Your advice and suggestions were very helpful.  Good communication and relevant information presented.

Thanks again.
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now