Ollifi Posted August 21, 2011 Share Posted August 21, 2011 How could I add last updated and created dates in mysql table row? Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/ Share on other sites More sharing options...
MarPlo Posted August 21, 2011 Share Posted August 21, 2011 Hy, You can get the last row in your table by sorting DESC, and LIMIT 1. SELECT * FROM `table_name` ORDER BY `id` DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260141 Share on other sites More sharing options...
The Little Guy Posted August 21, 2011 Share Posted August 21, 2011 by running the following code, you will create two datetime columns. 1. is the time it was create 2. is the time it was last updated. When you insert a new row, you need to use MySQL's now() function for the date_add column, the date_update column will automatically create a timestamp alter table some_table add column date_add datetime null, date_updated timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260159 Share on other sites More sharing options...
Ollifi Posted August 21, 2011 Author Share Posted August 21, 2011 ok thank you for help could you say whats wrong with this $query = "UPDATE tolkit SET (lyhenne, nimi, kpl, ostopaikka, valmistaja, lisatty, paivitetty) VALUES ('".$lyhenne."', '".$nimi."', '".$kpl."', '".$ostopaikka."', '".$valmistaja."', '".$lisatty."', '".$paivitetty."') WHERE id='".$id."'"; Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260165 Share on other sites More sharing options...
The Little Guy Posted August 21, 2011 Share Posted August 21, 2011 Because an update query is in this format: $lyhene = mysql_real_escape_string($_POST['lyhene']); $nimi= mysql_real_escape_string($_POST['nimi']); $id = (int)$_POST['id']; $query = "update tolkit set lyhenne = '$lyhene', nimi = '$nimi' ... where id = $id;"; Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260176 Share on other sites More sharing options...
Ollifi Posted August 21, 2011 Author Share Posted August 21, 2011 ok,thanks for helping I think i´ll not need mysql_real_escape_string but btw when I remove many rows from table and i have AUTO INCREMENT in ID field why the ID is not next to existing ones , it´s next to previous ones ? i think you may not understand but ids: 1,2,3,4,5 then remove 4,5 then create new and it gots id 6 how can i make it to got id 4 ? Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260181 Share on other sites More sharing options...
The Little Guy Posted August 21, 2011 Share Posted August 21, 2011 You can, it is not possible. Auto Increment increments, it does not deincrement. Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260261 Share on other sites More sharing options...
fenway Posted August 21, 2011 Share Posted August 21, 2011 Don't play with magic auto-increment values -- leave them alone. Your numbering doesn't matter -- and if it doesn't, use another column, or count yourself. Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260290 Share on other sites More sharing options...
Ollifi Posted August 22, 2011 Author Share Posted August 22, 2011 yes but I want to have auto increment feature since i need it. but thank you for helping (and I found feature to change the auto increment starting number it´s available in phpmyadmin ) Quote Link to comment https://forums.phpfreaks.com/topic/245338-last-updated-and-created-dates/#findComment-1260399 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.