Page 1 of 1

Inserting into a database problem???

Posted: Tue Dec 14, 2004 5:46 pm
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



		
	
	
	
?>

Posted: Tue Dec 14, 2004 5:53 pm
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).

Posted: Tue Dec 14, 2004 6:14 pm
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.

Posted: Tue Dec 14, 2004 7:38 pm
by timvw
'NULL' should be NULL

Posted: Wed Dec 15, 2004 3:18 am
by mmc01ms
tried that doesn't seem to make a diffrence what does this error usually mean?

Posted: Wed Dec 15, 2004 5:32 am
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.

Posted: Wed Dec 15, 2004 2:08 pm
by mmc01ms
cheers dude. stupid how you can miss simple errors.