php script not inserting data into database

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
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

php script not inserting data into database

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how do you know the query worked? you're not testing what mysql_query() returned...
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

good point. I set $test = mysql_query($query); then echo'd $test, and it came up blank. any ideas why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the query failed or returned zero.. use:

Code: Select all

mysql_query($query) or die(mysql_error());
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post 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??
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

fixed it. I had ID set to tinyint.

thanks for all your help.
Post Reply