ananaz Posted December 23, 2010 Share Posted December 23, 2010 Hello guys, im using this code atm and it's working, but the new rows created are put in the end of the table. I want them to stack up from the beginning pushing older rows down. What should i do to make that work? <?php $con = mysql_connect("localhost","*****","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ananaz_se", $con); $sql="INSERT INTO stuff (adult, namn, url) VALUES ('$_POST[adult]','$_POST[namn]','$_POST')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 row added"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/ Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 Why do you want to do this? It shouldn't matter, you can extract rows out whichever order you want with SQL. Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150819 Share on other sites More sharing options...
shlumph Posted December 23, 2010 Share Posted December 23, 2010 Yes, ordering should be done when pulling from the DB; not inserting. Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150820 Share on other sites More sharing options...
ananaz Posted December 23, 2010 Author Share Posted December 23, 2010 Well i have a field "id" a INT NOT NULL auto_increment and i want to call for example the data from the row with id 1 in a script. Then in another script i want to easily be able to "update" the rows by adding a new row on top so that row will display as id 1. Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150835 Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 Well i have a field "id" a INT NOT NULL auto_increment and i want to call for example the data from the row with id 1 in a script. Then in another script i want to easily be able to "update" the rows by adding a new row on top so that row will display as id 1. Changing around the 'id' column defeats the purpose of having an auto incremented column. Why does the new row have to be on top? If you insert it normally you can easily find the most recent row. Maybe I'm not understanding completely but I don't think inserting a new row on top every time would do any good, probably harm. Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150838 Share on other sites More sharing options...
litebearer Posted December 23, 2010 Share Posted December 23, 2010 1. inserting and updating are 2 different activities 2. an unique auto-increment field serves a very important function 3. to get the most recent row added simply query - order by id DESC limt 1 will return the most recently added row 4. if you put a date_edited field in the table and update it whenever a row is edited you can use that to get the most recently updated Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150840 Share on other sites More sharing options...
ananaz Posted December 23, 2010 Author Share Posted December 23, 2010 Ok I might be explaining it wrong. hmm.. I have a website submitting links just for fun like this: <Website> Link 1 (lolcats) Link 2 (rageguy) Link 3 (something scary) Then i have post.html (what will be acceptable only from my ip later) with a form that leads to "post.php" submitting a new row in my mysql server. I have names on the Links and they are named through mysql data. So the Link 1 will automatically take the name of the name field where the id is 1, and so on with Link 2.. The idea is that i should be able to easily update the website with a new name and link by making the current Link 1 data become Link 2 data ect.. So if i use the post.html and add a new it will look like this. <Website> Link 1 (something new and awesome) Link 2 (lolcats) Link 3 (rageguy) Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150844 Share on other sites More sharing options...
shlumph Posted December 23, 2010 Share Posted December 23, 2010 Ok I might be explaining it wrong. hmm.. You're explaining it perfectly. Don't mess with the primary key ID's in your table. Simply order the result set when you're querying the database with "ORDER BY". Quote Link to comment https://forums.phpfreaks.com/topic/222513-insert-data-into-topbeginning-of-mysql-table/#findComment-1150854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.