Hyperjase Posted April 27, 2009 Share Posted April 27, 2009 Hey guys, I'm trying to knock a quick piece of code up to check it a record already exists in one table, if it does, stop the query and inform the user. I've been looking around and using mysql_num_rows but no matter what i do, it keeps erroring out. Here is what I have, all I need to check is that the data entered in the category form doesn't already exist in the table. $result = mysql_query( "SELECT * FROM category.catname WHERE catname=".$_POST['category'] ); if (mysql_num_rows($result) > 0) { echo "Already exists"; exit; } else { I think the mysql_query may well be wrong, but the table is in the category section, with catname being the row. Thanks guys, your work is invaluable, specially to me!!! Jason Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/ Share on other sites More sharing options...
Maq Posted April 27, 2009 Share Posted April 27, 2009 If you used or die(mysql_error()) you would have gotten an error for this. You need single quotes around the POST value you want to compare the column to. Try: $result = mysql_query( "SELECT * FROM category.catname WHERE catname='{$_POST['category']}' ); Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/#findComment-820401 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 SELECT * FROM category WHERE catname = ? category.catname means 'table `catname` in database `category`' Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/#findComment-820405 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Don't forget the ending " $result = mysql_query( "SELECT * FROM category.catname WHERE catname='{$_POST['category']}'" ); Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/#findComment-820407 Share on other sites More sharing options...
Maq Posted April 27, 2009 Share Posted April 27, 2009 Yeah, those 2 things would help too... Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/#findComment-820427 Share on other sites More sharing options...
Hyperjase Posted April 27, 2009 Author Share Posted April 27, 2009 Fantastic, thanks guys - that worked a treat. I've used the above code too, error_reporting, just for future info. Whilst I'm on this subject .... the bit where it says it exists .... between the curly braces - is there any way of making it "go-to" another part of the script ... this part - if (isset($_GET['addcat'])) { - then inputting a message above the same form to say that it already exists ... or can I just move the whole query to the start of the form? I know that php evaluates everything before it outputs it ... so that should work? Cheers! Jason Quote Link to comment https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/#findComment-820458 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.