Solved

Insert Data from Web Form into mysql Database table Not Working

Posted on 2013-01-18
6
300 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

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 108

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now