Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/155870-solved-check-it-record-exists-in-mysql/
Share on other sites

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']}' );

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

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.