I want to make a recipe website.In that I had added the add to favourite button.If i click my favourite button the recipe which i clicked will add to favourites.I i re-click it will remove from my favourites.It is controlled by the status field in the favourites table in sql.How could I do this?

Dani AI

Generated

— two common DB-side patterns solve this: the insert/delete approach that mentioned, or keeping a status/flag you toggle (what you described). Both are fine; delete keeps the table smaller, a status bit preserves history and lets you soft‑unfavourite. Below is a concise, safe T-SQL toggle procedure for SQL Server that performs an atomic toggle (avoids duplicates under normal concurrency) and returns the new state so the UI can update immediately.

CREATE PROCEDURE dbo.ToggleFavourite
    @UserID INT,
    @RecipeID INT
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    BEGIN TRAN;

    UPDATE dbo.Favourites
    SET Status = CASE WHEN Status = 1 THEN 0 ELSE 1 END
    WHERE UserID = @UserID AND RecipeID = @RecipeID;

    IF @@ROWCOUNT = 0
    BEGIN
        INSERT INTO dbo.Favourites (UserID, RecipeID, Status, CreatedAt)
        VALUES (@UserID, @RecipeID, 1, GETUTCDATE());

        SELECT 1 AS NewStatus;
    END
    ELSE
    BEGIN
        SELECT Status AS NewStatus
        FROM dbo.Favourites
        WHERE UserID = @UserID AND RecipeID = @RecipeID;
    END;

    COMMIT TRAN;
END;

Notes and practical tips:

  • Add a UNIQUE constraint on (UserID, RecipeID) to guarantee one row per pair and avoid accidental duplicates.
  • Use BIT for Status (0/1) and a CreatedAt / UpdatedAt timestamp if you need history or analytics.
  • If you expect high concurrent clicks, consider selecting with UPDLOCK, HOLDLOCK or use a single-row lock pattern to avoid races.
  • If you prefer ’s delete/insert approach, enforce the same uniqueness and return the final state from the procedure so the client stays in sync.
  • For UX, do an optimistic toggle on click and revert if the stored-proc returns the opposite state. ’s front-end link can handle button animation, but keep DB logic idempotent and transactional.

Recommended Answers

All 2 Replies

If you want to delete the recipe from the favourites all together you could do the following

You need 3 tables:

  • UserTable

    • UserID (primary key)
  • RecipeTable

    • RecipeID (primary key)
  • FavouritesTable

    • FavouritesID (primary key)
    • UserID (foreign key)
    • RecipeID (foreign key)

You'll also need 2 stored procedures:

  • Insert
    stored procedure that is executed when the favourite box is and INSERTs a new record with the user's ID and the recipe ID

  • Delete
    stored procedure that is executed when the favourite box is unticked and DELETES a SINGLE record with that user's ID and the ID for that recipe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.