Inserting into a database problem???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Inserting into a database problem???

Post by mmc01ms »

hi the problem i have is that my function won't insert anything into the table i want it to. can anyone see if there is a problem with my coding, the database connections are fine.

Code: Select all

<? function insert_vinyl()
	{
		$title = $_POST['title'];
		$artist = $_POST['artist_id'];
		$release_date = $_POST['year']."-".$_POST['month']."-".$_POST['date'];
		$genre = $_POST['genre_id'];
		$price = $_POST['price'];
		$qty = $_POST['qty'];
		$info = $_POST['comments'];
		
		$link_id = db_connect();
		
		$query = "insert into vinyls(cat_no, title, artist_id, release_date, genre_ref, price, stock_level, vinyl_information)
 		 values( NULL, "$title", "$artist", "release_date" , "$price" , "$qty" ,"$info")";
		
		$result = mysql_query($query, $link_id);
		
		if($result)
		{
			echo 'Succesfully Entered';
		}
		else
			echo 'ERROR';
	}
 
	
	if($_GET['action'] == "addrecord")
	{
		insert_vinyl();
		exit;
		break;
	}else



		
	
	
	
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The query seems fine (though I'd suggest putting your values in single quotes to make the query easier to read).

Modify your result statement a little:

Code: Select all

$result = mysql_query($query, $link_id) or die(mysql_error());
This will output the error returned by mysql, if the query doesn't work.

Also, output $query before you send it to the db. It may not look exactly like you expect it to (there might be un-escaped quotes in there which are scewing up the query).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

thanks for the speedy reply i'v fixed a few bugs however still no luck the output i get from the error is

Code: Select all

insert into vinyls(cat_no, title, artist_id, release_date, genre_ref, price, stock_level, vinyl_information) values( 'NULL', 'Call On Me Remix', 'Jay-Z', '2001-4-3' , '4.99' , '3' , 'test')Column count doesn't match value count at row 1

the above code also includes the output of the query to see if there was a problem with the insert data wise.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

'NULL' should be NULL
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

tried that doesn't seem to make a diffrence what does this error usually mean?
Bitmaster
Forum Newbie
Posts: 20
Joined: Thu Nov 21, 2002 8:42 am

Post by Bitmaster »

Code: Select all

cat_no => NULL  (must be without quotes !)
title => 'Call On Me Remix'
artist_id => 'Jay-Z'
release_date => '2001-4-3' 
genre_ref => '4.99'  (this should probably go into price column)
price => '3' (this should probably go into stock_level column)
stock_level => 'test' (this should probably go into vinyl_information column)
vinyl_information => ????
So you try to insert 7 values into 8 columns => your error message
More specifically, I think you're missing the genre_ref column into your values list.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

cheers dude. stupid how you can miss simple errors.
Post Reply