0

I want to insert data in a table only if the record with a given CatalogID does not exist.

 $userId = $_POST['userId'];
 $catalogID = $_POST['catalogID'];
 $content_AddedTime = $_POST['content_AddedTime'];

 $query = ("INSERT INTO LibraryMaster (UserID,CatalogID,ContentAddedDateTime)
      VALUES ('$userId','$catalogID','$content_AddedTime')");
 mysql_query($query,$con);
 printf("Records inserted: %d\n", mysql_affected_rows());
 echo($user_Name)

3 Answers 3

2

Create a UNIQUE index on CatalogID.

Sign up to request clarification or add additional context in comments.

4 Comments

in table structuer i may create but i want that if this is already then it should return me YES. also so
Then check for failure of the INSERT query.
how to check this can you please help me out in this
+1, hits the database once, you can check for errors via mysql_error I would recommend using PDO or MySQLi.
0
$query="INSERT INTO LibraryMaster (UserID,CatalogID,ContentAddedDateTime)
SELECT * FROM (SELECT '".$userId."', '".$catalogID."', '".$content_AddedTime."') AS tmp
WHERE NOT EXISTS (
  SELECT CatalogID FROM LibraryMaster WHERE CatalogID = '".$catalogID."t'
 ) LIMIT 1";

Comments

-1

You could use this sort of function

public function Checkrecord($catalogID )
    {
        $query="Select * from table_name where catalog_id=$catalogID ";
                  mysql_query($query,$con);
        if(mysql_num_rows($query)>0)
        {
            //report error
        }
        else
        {
            //insertQuery
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.