Page 1 of 1

php script not inserting data into database

Posted: Thu Aug 04, 2005 4:36 pm
by ferric
I have a mysql script that should, under all circumstances, insert data into a database. here's the part of the script responsible for the insertion:

Code: Select all

if (($reviewer) && ($review)) {
		$episode = getepname($epcode);
		$username = "*****";
		$password = "*****";
		$database = "quahog5news_com_-_interactive";
			
		mysql_connect(localhost,$username,$password);
		@mysql_select_db($database) or die("Unable to select database");
		$query = "INSERT INTO reviews (id, epcode, epname, reviewer, review, rating) VALUES (
		'',
		'$epcode',
		'$episode',
		'$reviewer',
		'$review',
		'$rating'
		)"; 
		
		mysql_query($query);
		mysql_close();

		//echo $query;
		echo "Thank you for submitting a review for the episode " . $episode . ".  Please click <b><a href=index.php?p=reviews>here</a></b> to return to the main reviews page, or click <b><a href=index.php?p=reviews&epcode=" . $epcode . ">here</a></b> to see your review!<br><BR><BR><center><h2><a href=index.php?p=reviews><< BACK</a></h2></center>";
	} else {
		echo "Please fill out all fields before submitting a review.";
		halt;
	}
Now, you will notice that echo $query is commented out. if I uncomment it, it returns: INSERT INTO reviews (id, epcode, epname, reviewer, review, rating) VALUES ( '', 'FG103', 'Mind Over Murder', 'fds', 'fds', '2' )

All those values are correct for the datatypes of the fields. Also, no error is returned. Everything is outputted ok, but nothing is inserted into the database. is there something simple i'm overlooking? Also, $review and $reviewer do have values.

Posted: Thu Aug 04, 2005 5:50 pm
by feyd
how do you know the query worked? you're not testing what mysql_query() returned...

Posted: Thu Aug 04, 2005 7:15 pm
by ferric
good point. I set $test = mysql_query($query); then echo'd $test, and it came up blank. any ideas why?

Posted: Thu Aug 04, 2005 7:30 pm
by feyd
the query failed or returned zero.. use:

Code: Select all

mysql_query($query) or die(mysql_error());

Posted: Thu Aug 04, 2005 7:37 pm
by ferric
Thanks. that returned: Duplicate entry '127' for key 1

right now there are only 127 rows in my table. any idea whats causing this??

Posted: Thu Aug 04, 2005 7:42 pm
by ferric
fixed it. I had ID set to tinyint.

thanks for all your help.