Solved

Insert Data from Web Form into mysql Database table Not Working

Posted on 2013-01-18
6
301 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

14 Experts available now in Live!

Get 1:1 Help Now