Jump to content


Photo

check for duplicates and prevent them from being added to a DB


  • Please log in to reply
4 replies to this topic

#1 koolaidman52

koolaidman52
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 15 July 2006 - 09:27 PM

im coding a database of cartoon series for my site, where users can sign up and add, edit, and do someother stuff with cartoons series, i have a page addseries.php and i have some problem code in the file:

...
$que = @mysql_query("SELECT series_title FROM series WHERE series_title = $_POST[title]");
				if (mysql_num_rows($que) == 0)
				{
				$que = "INSERT INTO series (series_title, creator, begin_year, end_year, seasons, station , series_description ) VALUES ('$_POST[title]', '$_POST[creator]', '$_POST[begin_year]', '$_POST[end_year]', '$_POST[seasons]', '$_POST[station]', '$_POST[description]')";
				@mysql_query($que, $connection);
				$spam = @mysql_query($que, $connection);
					if ($spam) 
					{
					  echo "<p>Added series</p><br />";
					}
                                                   }
				else
				{
					echo "That series already exists in the database";
					echo $add_series;
                                                                }
...
the user supplies $_POST[title]', '$_POST[creator]', '$_POST[begin_year]', '$_POST[end_year]', '$_POST[seasons]', '$_POST[station]', '$_POST[description] (im lazy, i just did a copy and paste).

i was testing the page with "captain planet" as $_POST[title], and i found that im able to continue adding as many "captain planets" as I want (I never get  "That series already exists in the database"). obviosly my code is not working, but I can't fgure out why.

any of you wizards know what i'm doing wrong?

ps im also getting
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\omnibase\addseries.php on line 47"
too
Btcartoons.com --  coded entirely in Mom's basement.

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 10:24 PM

give this query a shot:

"SELECT series_title FROM series WHERE series_title = '{$_POST['title']}'"

when you get an invalid resource error, it's usually an indicator that your query failed.  in that case, add an or die(mysql_error()) to the offending query to see what the problem is.  it's useful for debugging, but can easily be removed once you've got everything working.

#3 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 15 July 2006 - 10:31 PM

The warning is telling you that your query is bad:

$que = @mysql_query("SELECT series_title FROM series WHERE series_title = $_POST[title]");

make sure the field names are correct and are you checking that the user always gives $_POST['title'] a value?

$title = $_POST['title'];
$que = @mysql_query("SELECT series_title FROM series WHERE series_title = '$title'");

The above will make it easier to debug, if neccessary, when you have alot of code to search through
:: gucci.com ::
:: blockbuster.com ::

--------------------

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 10:51 PM

This wont give a nice message to the user, but place a UNIQUE index on the series_title column. That way it wont let duplicates be entered.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 koolaidman52

koolaidman52
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 15 July 2006 - 11:11 PM

oh thats clever pixy, maybe version 0.2, it won't be implemented for a few months because i still have to work out some session issues and user authentication/ registration stuff

anyway, what the first dude said worked but ill keep the UNIQUE thing in mind
Btcartoons.com --  coded entirely in Mom's basement.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users