doodmon Posted September 7, 2006 Share Posted September 7, 2006 So long as I create new records sequentially it will put them in order, but when i delete a record and then try to add a record it will place the new record in the old record's place. I want it to appear at the end of the table, not in the middle. Here is the code to create a new record and remove a record, can I do a sort by the number column after a new one is inserted or other suggestions??:case "create"://pretty basic, just counts the number of rows and puts record with number 1 greater than that in.//problem is it doesn't always put it at the end of the table.$vtip = isset($_POST['vtip']) ? trim($_POST['vtip']) : "";$vtip = str_replace("'", "’", $vtip);$db=mysql_connect ("localhost", "xxx", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("tips");$query = mysql_query("SELECT * FROM tips.new");$x = mysql_num_rows($query);$x++;mysql_query("INSERT INTO new (number, tip) VALUES ('$x','$vtip')");echo "Entry Successful";mysql_close($db);break;case "delete"://$num is the record number being deleted. //After it is removed while loop renumbers all the records that fell after the record so there is no gap.$num = isset($_REQUEST['num']) ? trim($_REQUEST['num']) : "";$db=mysql_connect ("localhost", "xxx", "xxx") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("tips");mysql_query("DELETE FROM new WHERE number='$num'");$x=0;$query = mysql_query("SELECT * FROM new");$numrows = mysql_num_rows($query);while ($row = mysql_fetch_array($query)) { $x++; mysql_query("UPDATE new SET number='$x' WHERE number='$row[0]'");}echo "Deletion Successful";mysql_close($db);break; Quote Link to comment Share on other sites More sharing options...
fenway Posted September 7, 2006 Share Posted September 7, 2006 Not again... don't worry yourself with how/where the DB puts the record. If you want them sorted in a particular way, issue an ORDER BY clause, and add the appropriate column to your table (e.g. a created/modified date, etc.) Don't expect, assume or in any way depend on the PK field for anything at all. Quote Link to comment Share on other sites More sharing options...
doodmon Posted September 7, 2006 Author Share Posted September 7, 2006 Thanks dood... glad you could help without sounding snotty Quote Link to comment Share on other sites More sharing options...
fenway Posted September 7, 2006 Share Posted September 7, 2006 No problem... you'd be surprised how much trouble you'd run into if you start tinkering with primary key values. Quote Link to comment 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.