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 3490918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:34:27+00:00 2026-05-18T11:34:27+00:00

mysql_insert_id does not return the last inserted id when i place it inside a

  • 0

mysql_insert_id does not return the last inserted id when i place it inside a function.

im kinda confused why it does not.

here is my code:

function addAlbum($artist,$album,$year,$genre) {
    $connection = mysql_connect(HOST,USER,PASS);
    $sql = 'INSERT INTO `'.TABLE_ARTIST.'` (artistName) VALUES ("'.$artist.'")';
    $resultArtist = mysql_query($sql);
    $sql = 'INSERT INTO `'.TABLE_ALBUMS.'` (albumName) VALUES ("'.$album.'")';
    $resultAlbums = mysql_query($sql);
    $sql = 'INSERT INTO `'.TABLE_GENRE.'` (musicGenre) VALUES ("'.$genre.'")';
    $resultGenre = mysql_query($sql);
    $sql = 'INSERT INTO `'.TABLE_YEAR.'` (albumYear) VALUES ("'.$year.'")';
    $resultYear = mysql_query($sql);
    $lastId = mysql_insert_id();
    $sql = 'INSERT INTO `'.TABLE_LINK.'` (albumsId,artistId,genreId,yearId) VALUES ("'.$lastId.'","'.$lastId.'","'.$lastId.'","'.$lastId.'")';
    $resultLink = mysql_query($sql);
    if(!$resultArtist && $resultAlbums && $resultGenre && $resultYear && $resultLink){
        echo mysql_error();    
    }
}

thanks in advance

adam

  • 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-05-18T11:34:27+00:00Added an answer on May 18, 2026 at 11:34 am

    You are calling mysql_insert_id() once after four separate INSERTs, and using that ID four times for albumsId, artistId, genreId and yearId. That doesn’t seem right.

    You should also check that your tables are using AUTO_INCREMENT fields. If not, mysql_insert_id() will not return the insert ID. See the docs:

    http://www.php.net/manual/en/function.mysql-insert-id.php

    I highly recommend that you use prepared statements with mysqli::prepare, perhaps via PDO. It’s ultimately simpler and safer. Here’s an untested example:

    $dsn = 'mysql:dbname=test;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    
    $dbh = new PDO($dsn, $user, $password);
    
    $stmt_artist = $dbh->prepare(
        'INSERT INTO `table_artist` (artistName) VALUES (?)'
    );
    
    $stmt_albums = $dbh->prepare(
        'INSERT INTO `table_albums` (albumName) VALUES (?)'
    );
    
    $stmt_genre = $dbh->prepare(
        'INSERT INTO `table_genre` (musicGenre) VALUES (?)'
    );
    
    $stmt_year = $dbh->prepare(
        'INSERT INTO `table_year` (albumYear) VALUES (?)'
    );
    
    $stmt_link = $dbh->prepare(
        'INSERT INTO `table_link` (albumsId, artistId, genreId, yearId) '.
        'VALUES (?, ?, ?, ?)'
    );
    
    $stmt_albums->execute(array( $artist ));
    $artist_id = $dbh->lastInsertId();
    
    $stmt_albums->execute(array( $album ));
    $album_id = $dbh->lastInsertId();
    
    $stmt_genre->execute(array( $genre ));
    $genre_id = $dbh->lastInsertId();
    
    $stmt_year->execute(array( $year ));
    $year_id = $dbh->lastInsertId();
    
    $stmt_link->execute(array( $artist_id, $album_id, $genre_id, $year_id ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
Possible Duplicate: insert contacts into database but does not want to duplicate already existing
Im using smarty and mysql_real_escape_string() for user input, and when I insert some code
I need to have a function that increments the certain ID in a table
I am looking to parse INSERT and UPDATE MySQL SQL queries in PHP to
I received a MySQL data dump and am trying to insert the data into
In mysql, how do I get the primary key used for an insert operation,
I am trying to insert a very long text string into a MySQL Blob
Using only MySQL , I'm seeing if it's possible run an insert statement ONLY
We have a live MySQL database that is 99% INSERTs, around 100 per second.

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.