Solved

Insert Data from Web Form into mysql Database table Not Working

Posted on 2013-01-18
6
307 Views
Last Modified: 2013-01-18
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
Comment
Question by:JParra72
  • 3
  • 2
6 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38793713
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38793714
In your query it has 53 columns, but while you're passing variables are 56?

just cross check.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38793723
sorry in your query it has 57 fields but in your variable it has 56?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38793885
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
 

Author Closing Comment

by:JParra72
ID: 38794066
Thanks very much!  Your advice and suggestions were very helpful.  Good communication and relevant information presented.

Thanks again.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38794124
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question