MYSQL Column Count trouble

dmnEPC

New member
Dec 23, 2010
5,994
95
0
So I just put this together and have stared at it about as long as I can stand. Hoping one of you guys would be willing to take a peek at it for me and see if you point me in the right direction.

Basically what I am trying to do is pull all the zipcodes, cities, state, counties in the US from a SQL database and then auto post them to Wordpress. Problem I am having is error @ the wordpress post.

Error is
Invalid query dumbass: Column count doesn't match value count at row 1

I have tried to add the "ID" and add the blank '' in the values sections of the wp_posts. As well as the post_id and also the meta_id followed by the blank '' in the wp_postsmeta section as recommended all over google as the solution. Any suggestions would much be appreciated. And a few boobs for your trouble

PHP:
<?php

	$link = @mysql_connect('localhost','username','password');
	if (!$link) {
		echo ('<p>Unable to connect to the ' .
				'database server at this time.</p>' );
				exit();
			
	}
else
	{
		echo 'Connected successfully';
	}

	$db_selected = mysql_select_db('database', $link);
	if (!db_selected)
	{
		die ('Can\'t use mysql db : ' . mysql_error());
		
	}

$post_author = "2";  //Wordpress Author ID	

//Pull Data From Database
$result = @mysql_query('SELECT Zip_Code, City, State, County FROM ZIP_CODES');
		if (!$result) {
			die('<p>Error getting zip: ' . mysql_error() . 
			'</p>');
		}

//Insert data from zip code table in Wordpress

while ( $row = mysql_fetch_array($result)) {
	
	$zip = $row['Zip_Code'];
	$city = $row['City'];
	$state = $row['State'];
	$county = $row['County'];
 
 }
{
	$post_content = "blah blah blah $county blah blah blah?  blah blah blah $city blah blah blah
	blah blah blah $state $zip"; 

	$post_title = "TITLE of POST".$city;
	$post_name = "title of post".strtolower($city);
	
	$sql = "
	INSERT INTO
		wp_posts
			(
				post_author,
				post_date,
				post_date_gmt,
				post_content,
				post_title,
				post_status,
				post_name,
				post_modified,
				post_modified_gmt,
				post_type
				
			)
	VALUES
		(
		'$post_author',
		NOW(),
		NOW(),
		'$post_content',
		'$post_title',
		'publish',
		'open',
		'open',
		'$post_name',
		NOW(),
		NOW(),
		'page'
		
		)
	";
	echo "<PRE>";
	print_r($sql);
	echo "</PRE>";
	
	$results = mysql_query($sql);
		if (!$results)
		{
			die('Invalid query dumbass: ' . mysql_error());
		}
		else
		{
			$postid = mysql_insert_id();
			$sql = "INSERT INTO
					wp_postmeta
					(
						post_id,
						meta_key,
						meta_value
					)	
					VALUES
					(
						'post_id',
						'_wp_page_template',
						'default'
					)	
					";
	
			$results = mysql_query($sql);
			echo "<BR>
				Created page for ".$city;
		}


}	
?>

rbfh1.jpg

fYtHE.jpg

B4vv8.jpg

6mxx5.jpg

7fi7c.jpg

WI51H.jpg
 


IN the insert statement, if you have values listed, you have to have the column names to match. In your example, you have more values than you do columns, so it doesn't know where to put them.

To be certain, you can always list all the columns, and if it has a default value, or you don't want to put anything there, you can put NULL in its place in the values list.
 
IN the insert statement, if you have values listed, you have to have the column names to match. In your example, you have more values than you do columns, so it doesn't know where to put them.

To be certain, you can always list all the columns, and if it has a default value, or you don't want to put anything there, you can put NULL in its place in the values list.

That was it. Was missing 2. Probably help if I learned to count a little better. Now I have to go fix my SELECT statement to read more than 1 row and I should be go to go. Thanks again for your help.

PHP:
INSERT INTO
		wp_posts
			(
				post_author,
				post_date,
				post_date_gmt,
				post_content,
				post_title,
				post_status,
				comment_status,
				ping_status,
				post_name,
				post_modified,
				post_modified_gmt,
				post_type
				
			)
	VALUES
		(
		'$post_author',
		NOW(),
		NOW(),
		'$post_content',
		'$post_title',
		'publish',
		'open',
		'open',
		'$post_name',
		NOW(),
		NOW(),
		'page'
		
		)
	";
 
^ This

Anything you want to do in wordpress, chances are they have a function for it. Always look first.
 
Yeah man like mentioned use the built in WordPress stuff. Also don't DIE if there is an error, again use the WordPress error handler.