Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:10:14+00:00 2026-06-14T01:10:14+00:00

Possible Duplicate: Access Auto-Increment Value During INSERT INTO Statement I would like to generate

  • 0

Possible Duplicate:
Access Auto-Increment Value During INSERT INTO Statement

I would like to generate the slug for my URLs upon creating the pages to store in the database. The slug should be title-id (e.g titlename-234). I already have the function to strip the title but how can i get the id before inserting the record?

Many thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:10:15+00:00Added an answer on June 14, 2026 at 1:10 am

    You should create a trigger, something like that:

    CREATE TRIGGER trigger_name BEFORE INSERT ON table
    FOR EACH ROW SET slug = CONCAT(NEW.title, "-", NEW.id);
    

    I’m not sure you’ll be able to access the ID column before its written on the database (unless, of course, you generate your IDs yourself, not using the autoincrement).

    If you’re using your DB’s autoincrement (and you should be), try creating a trigger AFTER INSERT, and updating your row in your trigger. That way, even though you’re updating it AFTER your insert, it’ll still run before you can run any other queries (like a SELECT).

    HERE is the documentation on triggers.


    I was wrong. Apparently, you can’t update a table you’re inserting into (using an AFTER INSERT triger), it’ll throw an exception. So, two possible ways to do it using SQL alone:

    The ugly way:

    DELIMITER $$
    CREATE TRIGGER `create_slug` BEFORE INSERT
        ON `events`
        FOR EACH ROW
    BEGIN
        SET new.id = (SELECT MAX(id) FROM events) + 1;
        SET new.slug = CONCAT(new.menu_name, "-", new.id);
    END$$
    DELIMITER ;
    

    It overrides your database’s AUTOINCREMENT. Really, don’t do that.

    Or, you can create a procedure:

    DELIMITER $$
    
    CREATE PROCEDURE insert_event(MenuName VARCHAR(30), Content TEXT, PhotoName TEXT)
    BEGIN
        INSERT INTO events (menu_name, content, photo_name) VALUES (MenuName, Content, PhotoName);
        SET @id = LAST_INSERT_ID();
        UPDATE events SET slug = CONCAT(menu_name, "-", @id) WHERE id = @id;
    END$$
    
    DELIMITER ;
    

    And, instead of calling (as you were calling):

    $query = mysql_query("INSERT INTO events (menu_name, content, photo_name) VALUES ('{$menu_name}', '{$content}', '{$photo_name}');", $connection); 
    

    just call

    $result = mysql_query("CALL insert_event('{$menu_name}', '{$content}', '{$photo_name}');",$connection );
    

    Again, I’ll strongly advice against using mysql_query. It’s outdated, discontinued and unsafe. You should check out mysqli or PDO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Access Auto-Increment Value During INSERT INTO Statement I want to run an
Possible Duplicate: Access JavaScript Object Literal value in same object First look at the
Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) {
Possible Duplicate: How to access web camera settings (like in Skype) using DotNet I
Possible Duplicate: WPF Listview Access to SelectedItem and subitems I have a listview defined
Possible Duplicate: How to access this attribute using jquery, given a div defined by
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Possible Duplicate: How Do I Access This Variable? Lets say I have the code:
Possible Duplicate: How to search for string in MS Access VBA array I am
Possible Duplicate: How to get random record from MS Access database In my project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.