Skip to content Skip to sidebar Skip to footer

Insert Data Only If Record Does Not Exist

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 = $_P

Solution 1:

Create a UNIQUE index on CatalogID.

Solution 2:

$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";

Solution 3:

You could use this sort of function

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

Post a Comment for "Insert Data Only If Record Does Not Exist"