Jump to content

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


koolaidman52

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.