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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:52:03+00:00 2026-05-28T17:52:03+00:00

I think most people had serious problems with inserting a value/data into a database

  • 0

I think most people had serious problems with inserting a value/data into a database (mysql). When I’m inserting a value into a database, I assign an unique id (INT) for that line. When I query the database, easily I can read/modify/delete that line.

With function for() (in php) I easily can read values/data from the database. The problem occurs when I delete a line (in the middle for example).

E.g:

DB:

ID | column1 | column2 | ... | columnN
--------------------------------------
1 | abcdefgh | asdasda | ... | asdasdN
2 | asdasddd | asdasda | ... | asdasdN
...
N | asdewfew | asddsad | ... | asddsaN

php:

for($i = 0; $i <= $n; $i++){
   $sql = mysql_query("SELECT * FROM db WHERE ID = '$i' ");
   //Code;
}

*$n = last column value from ID


Am I need to reorganize the entire database to have a correct “flow” (1, 2, 3, .. n)? Or am I need to UPDATE the each cell?

  • 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-28T17:52:04+00:00Added an answer on May 28, 2026 at 5:52 pm

    What you’re doing here is unnecessary thanks to AUTO_INCREMENT in mysql. Run this command from PhpMyAdmin (or another DB management system):

    ALTER TABLE db MODIFY COLUMN ID INT NOT NULL AUTO_INCREMENT;
    

    Now when insert a row into db mysql will assign the ID for you:

    INSERT INTO db (id, column1, column2) VALUES(NULL, 'abc', 'def');
    

    SELECT * FROM db;:

    +--+-------+-------+
    |id|column1|column2|
    +--+-------+-------+
    |1 |old    |oldrow |
    +--+-------+-------+
    |2 |abc    |def    | <--- Your newly inserted row, with unique ID
    +--+-------+-------+
    

    If you delete a row, it is true that there will be an inconsistency in the order of the ID’s, but this is ok. IDs are not intended to denote the numeric position of a row in a table. They are intended to uniquely identify each row so that you can perform actions on it and reference it from other tables with foreign keys.


    Also if you need to grab a group of ID’s (stored in an array, for example), it is much more efficient to perform one query with an IN statement.

    $ids = array(1,2,3,4,5,6);
    $in = implode(',', $ids);
    mysql_query('SELECT * FROM db WHERE id IN ('.$in.')');
    

    However, if you want all rows just use:

    SELECT * FROM dbs;
    

    But be weary of bobby tables.

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

Sidebar

Related Questions

I think most people know how to do this via the GUI (right click
I think most people here understand the importance of fully automated builds. The problem
As I think most people know already, or if you don't, FPDF released a
I think I've got it down to the most basic case: int main(int argc,
Like most people new to Git, I've had my share of confusion trying to
I have seen other people who have had this question, but most of the
It recently occurred to me that I (and I think most people) learned computer
I think most C++ programmers here would agree that polluting the global namespace is
I think most of us agree that it's a good idea to use a
I understand how JS is run and I think I understand most of the

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.