Insert data only if record does not exist

前端 未结 3 1106
无人及你
无人及你 2020-12-22 05:16

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

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


        
相关标签:
3条回答
  • 2020-12-22 05:26

    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
            }
        }
    
    0 讨论(0)
  • 2020-12-22 05:42

    Create a UNIQUE index on CatalogID.

    0 讨论(0)
  • 2020-12-22 05:46
    $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";
    
    0 讨论(0)
提交回复
热议问题