koolaidman52 Posted July 15, 2006 Share Posted July 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14711-check-for-duplicates-and-prevent-them-from-being-added-to-a-db/ Share on other sites More sharing options...
akitchin Posted July 15, 2006 Share Posted July 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14711-check-for-duplicates-and-prevent-them-from-being-added-to-a-db/#findComment-58700 Share on other sites More sharing options...
Drumminxx Posted July 15, 2006 Share Posted July 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14711-check-for-duplicates-and-prevent-them-from-being-added-to-a-db/#findComment-58704 Share on other sites More sharing options...
pixy Posted July 15, 2006 Share Posted July 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14711-check-for-duplicates-and-prevent-them-from-being-added-to-a-db/#findComment-58708 Share on other sites More sharing options...
koolaidman52 Posted July 15, 2006 Author Share Posted July 15, 2006 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 stuffanyway, what the first dude said worked but ill keep the UNIQUE thing in mind Quote Link to comment https://forums.phpfreaks.com/topic/14711-check-for-duplicates-and-prevent-them-from-being-added-to-a-db/#findComment-58712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.