How do i insert a a new record between existing records using positioning

Started on the old forum in September 2010 · view the original in the archive · old links redirect here

4 replies · 898 views · started 16 years ago · 1 min read

4 participants
Mysterio Lead · 630 posts #2

I wonder why people always want to do difficult tasks. Your question can be: How do I print it? Because in the database is the same, if the record is first or is second in the list.

mysterio.al - programming is a functional art

Vayne Developer · 106 posts #4

use

UPDATE table SET field

Nous Ne Dansos Pas, Nous Sommes Le Danse.!

something else Fellow · 2,296 posts #5

The Problem is that im guessing "id" is the primary key making it harder to restructure the datbase accordingly due to after "2 good 2" there might be another thousand entrys in the database making everything after 2 having to be renumbered id+1 I guess 1 way would be to use a temporary table transfer all data to that then return it to the example table eg:

$data=mysql_query(SELECT info, position, id FROM example");
while($list=mysql_fetch_array($data)){    
$done=mysql_query("INSERT INTO temp_table SET info='".$list[0]."', positione='".$list[1]."'");
if($done) print $list[2].': done';
}

mysql_query("TRUNCATE TABLE 'example'"); //delete contents of example table (might need permisions to do this)

$data=mysql_query(SELECT info, position, id FROM temp_table");
while($list=mysql_fetch_array($data)){
$done=mysql_query("INSERT INTO example SET info='".$list[0]."', positione='".$list[1]."'");
if($done) print $list[2].': done';
}

also if you do use this method i would recommend backing up your database before doing so ... as if you have a large database the script may time out. (in which case you would need to limit your calls to database and do it in sections)

There is probably is an easier way of doing this.....

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close