2013-06-18 71 views
0

只有當給定的CatalogID記錄不存在時,我纔想在表中插入數據。僅當記錄不存在時才插入數據

$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) 

回答

2

CatalogID上創建一個UNIQUE索引。

+0

表structuer我可以創造,但我想,如果這已經是那麼它應該返回我YES。也如此 –

+0

然後檢查「INSERT」查詢的失敗。 –

+0

如何檢查這個,你可以請幫我在這個 –

-1

你可以使用這種功能

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
$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"; 
相關問題