Solved

Insert Data from Web Form into mysql Database table Not Working

Posted on 2013-01-18
6
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

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 110

Expert Comment

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

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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