Log in

View Full Version : PHP Error



Medyman
07-16-2007, 06:26 PM
Hey guys...

I've been getting a "Column count doesn't match value count at row 1" error.

I'm not sure why it's happening.

The php is below:

<?php
if(isset($_POST['add']))
{
include 'config.php';
include 'opendb.php';

$name = $_POST['name'];
$company = $_POST['company'];
$street = $_POST['street'];
$apt = $_POST['apt'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$ext = $_POST['ext'];
$email = $_POST['email'];
$type = $_POST['type'];
$template = $_POST['template'];
$web1 = $_POST['web1'];
$web2 = $_POST['web2'];
$web3 = $_POST['web3'];
$web4 = $_POST['web4'];
$web5 = $_POST['web5'];
$cms = $_POST['cms'];
$sother = $_POST['sother'];
$othertext = $_POST['othertext'];
$programming = $_POST['programming'];
$domain = $_POST['domain'];
$hosting = $_POST['hosting'];
$management = $_POST['management'];
$seo = $_POST['seo'];
$ses = $_POST['ses'];
$marketing = $_POST['marketing'];
$budget = $_POST['budget'];
$date = $_POST['date'];
$month = $_POST['month'];
$year = $_POST['year'];


$query = "INSERT INTO custom (id, name, company, street, apt, city, state, zip, phone, ext, email, type, template, web1, web2, web3, web4, web5, cms, sother, othertext, programming, domain, hosting, management, seo, ses, marketing, budget, date, month, year)
VALUES ('','${name}','${company}','${street}','${city}','${state}','${zip}''${phone}','${ext}','${email}','${template}','${web1}','${web2}','${web3}','${web4}','${web5 }','${cms}','${sother}','${othertext}','${programming}','${domain}','${hosting}','${management}','${seo}','${ses}','${marketing}','${budget}','${date}','${month }','${year}')";
mysql_query($query) or die(mysql_error());

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

include 'closedb.php';
echo "Values Added";
}
else
{
?>

Twey
07-16-2007, 07:07 PM
Ouch, that's a lot of redundant code!
<?php
if(isset($_POST['add'])) {
include 'config.php';
include 'opendb.php';

$fields = array('name', 'company', 'street', 'apt', 'city', 'state',
'zip', 'phone', 'ext', 'email', 'type', 'template', 'web1', 'web2',
'web3', 'web4', 'web5', 'cms', 'sother', 'othertext', 'programming',
'domain', 'hosting', 'management', 'seo', 'ses', 'marketing', 'budget',
'date', 'month', 'year');
$afields = array();

for($i = 0; $i < count($fields); ++$i)
$afields[$fields[$i]] = mysql_real_escape_string($_POST[$fields[$i]]);

mysql_query(sprintf(
'INSERT INTO custom (&#37;s) VALUES (\'%s\')',
implode(', ', $fields),
implode('\', \'', array_values($afields))
)) or die(mysql_error());

mysql_query('FLUSH PRIVILEGES') or die('Error, insert query failed');

include 'closedb.php';
echo 'Values added';
} else {
?>

Medyman
07-16-2007, 07:24 PM
Thanks Twey!

If you don't mind, could you explain the code a bit. I'm just starting out with PHP and would like to understand what you just did there.

Also, what exactly was causing the error before?

Lastly, how can I make it so that the PHP sends me an email whenever this form is submitted?

djr33
07-16-2007, 07:56 PM
I'll save Twey a bit of work.

And I'll add a mail function in too.


<?php
if(isset($_POST['add'])) { //if adding data
include 'config.php'; //include setup
include 'opendb.php'; //open db

$fields = array('name', 'company', 'street', 'apt', 'city', 'state',
'zip', 'phone', 'ext', 'email', 'type', 'template', 'web1', 'web2',
'web3', 'web4', 'web5', 'cms', 'sother', 'othertext', 'programming',
'domain', 'hosting', 'management', 'seo', 'ses', 'marketing', 'budget',
'date', 'month', 'year'); //save desired POST vars
$afields = array(); //setup a blank array for use later

for($i = 0; $i < count($fields); ++$i)
//set $i to 0, repeat while $i< the number of fields, add one to $i each loop.
$afields[$fields[$i]] = mysql_real_escape_string($_POST[$fields[$i]]);
//now use the empty array to store each of the fields
//while escaping the values of the POST vars for safe use in the database
//if this wasn't done, then you could be hacked, using info in the POST vars
//such as $_POST['field'] = "value; DELETE * FROM `mytable`";
mysql_query(sprintf( //see note below
'INSERT INTO custom (&#37;s) VALUES (\'%s\')',
implode(', ', $fields),
implode('\', \'', array_values($afields))
)) or die(mysql_error());
//insert the values into the database, or, if that fails, stop the script and display the error
mysql_query('FLUSH PRIVILEGES') or die('Error, insert query failed');
//end access
include 'closedb.php'; //close connection
echo 'Values added'; //display confirm
} else { //otherwise do whatever is after the code here..
?>

Note: sprintf() is a bit confusing, so you can see the page here:
http://www.php.net/manual/en/function.sprintf.php

Medyman
07-16-2007, 09:08 PM
Thanks djr33.

I'm a little confused. Where is the mail function in all of that?

Twey
07-16-2007, 09:19 PM
I was wondering the same...

djr33
07-17-2007, 12:14 AM
I got distracted, commenting everything.

Here's the code for that:
...
include 'closedb.php'; //close connection
mail('your@email.com','Subject-- like Added!','Body content here, including $_POST['var'] if you want, but add them correctly to the string',"From: auto@mailer.com\r\nReply-to: auto@mailer.com\r\nMore headers");