Jump to content

Column count doesn't match value count at row 1


smproph

Recommended Posts

Why am I getting this error when there are 3 Fields with 3 values?

 

Column count doesn't match value count at row 1

 

$sql5="INSERT INTO participants (participant_name, team_no, sport)
		VALUES ('".implode("','$_POST[team]'),('",$_POST['participant_name'])."','$_POST[team]','$sport')";

 

Link to comment
Share on other sites

INSERT INTO participants (participant_name, team_no, sport) VALUES ('ply2','Team'),('ply1','Team'),('pl5','Team'),('jfsd','Team'),('da','Team','dodgeball')

 

I can see the dodgeball is only being added to the last player. But I cannot figure out why. Does it have to do to with my implode function?

Link to comment
Share on other sites

It is definitely your implode function call. It took me a while to figure out what you were trying to do. And when you come back to this script in a few months to make changes, it will take you a while to figure out how to do that. Just off the top of my head, a cleaner way to write that would be:

 

$team = 4;
$pnames = array('DavidAM', 'smproph', 'Pikachu2000', 'xyph');
$sport = 'programming';

$values = array();
foreach ($pnames as $pName) {
$values[] = "('$pName', '$team', '$sport')";
}

$sql5="INSERT INTO participants (participant_name, team_no, sport) VALUES " .
implode(',', $values);

 

There may be an even more elegant way of doing it, but that's worth more than twice what you paid me for it.

Link to comment
Share on other sites

Problem is that now I would have to hardcode the players name. I tried adding

 

$name= $_POST['participant_name'];
$pnames = array($name);

 

But as you probably know it is only adding one player instead of however many the user selected and it adds the name as "Array"

Link to comment
Share on other sites

INSERT INTO participants (participant_name, team_no, sport) VALUES ('ply2','Team'),('ply1','Team'),('pl5','Team'),('jfsd','Team'),('da','Team','dodgeball')

 

I can see the dodgeball is only being added to the last player. But I cannot figure out why. Does it have to do to with my implode function?

 

My mistake. You had everything formatted properly, I didn't know what $_POST values were arrays and which were strings. Sorry.

 

DavidAM has the ideal solution IMO. A concatenated string over an array you later implode would save a little bit of memory, but the difference is negligible on any modern system not getting thousands of requests per second :)

Link to comment
Share on other sites

Yeah, I was just showing an example. Last time I provided an example using the OP's code, with the $_POST intact, I got blasted for not sanitizing the inputs. So, I thought I would just give an example and hope the "programmer" could understand the concept and translate to his/her needs.

 

By the way - It is NOT a good idea to use $_POST (or $_GET or even $_COOKIE) directly in a query without sanitizing them first . So, to expand the example:

 

$values = array();
foreach ($_POST['participant_name'] as $pName) {
$values[] = sprintf("('%s', %d, '%s')", 
	mysql_real_escape_string($pName),
	int_val($_POST['team'],
	mysql_real_escape_string($sport));
/* UNLESS the team_no column is a VARCHAR or CHAR or TEXT 
	in which case it would be:
$values[] = sprintf("('%s', '%s', '%s')", 
	mysql_real_escape_string($pName),
	mysql_real_escape_string($_POST['team']),
	mysql_real_escape_string($sport));
*/
}

$sql5="INSERT INTO participants (participant_name, team_no, sport) VALUES " .
implode(',', $values);

 

I'm guessing we are using mySql here. If not, use the appropriate escaping mechanism for your database of choice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.