Reputation: 205
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)
Upvotes: 0
Views: 791
Reputation: 5016
$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";
Upvotes: 0
Reputation: 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
}
}
Upvotes: -1