Jump to content

Append to END of table


doodmon

Recommended Posts

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;
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.