Jump to content

Archived

This topic is now archived and is closed to further replies.

koolaidman52

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

Recommended Posts

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:

[code]
...
$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;
                                                                }
...
[/code]
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

Share this post


Link to post
Share on other sites
give this query a shot:

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

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.

Share this post


Link to post
Share on other sites
The warning is telling you that your query is bad:

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

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

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

The above will make it easier to debug, if neccessary, when you have alot of code to search through

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.