Solved

Insert Into adds records sporadically within the database

Posted on 2011-03-05
6
330 Views
Last Modified: 2012-05-11
I have the following code which inserts selected items into the database.  Everything seems to work fine but in most cases it does not enter records in order.  I have had a few records appear in  the middle, some on the end of the database.

I need them to appear on the bottom of the database in the order I select them.


      <?php
        if (isset($_POST['Sel']) && is_array($_POST['Sel']))
        {
            $L1 = $_SESSION['SelM1'];
            for ($x=count($_POST['Sel'])-1;$x>=0;$x--) {
                  $selected_value = $_POST['Sel'][$x];
                $sql = "SELECT * FROM PL WHERE Username='$UN' AND PLTitle='$L1' AND     PLItem='$selected_value'";
                $Recordset6 = mysql_query($sql, $connAdmin) or die(mysql_error());
                $row_Recordset6 = mysql_fetch_assoc($Recordset6);
                $totalRows_Recordset6 = mysql_num_rows($Recordset6);
                if ($totalRows_Recordset6 < 1) {
            mysql_query("INSERT INTO PL (Username, PLTitle, PLItem)
                    VALUES ('$UN', '$L1', '$selected_value')");
                      mysql_query("COMMIT");
                }
            }
                  unset($_POST['Sel']);
              echo "<script type='text/javascript'>window.location = './PLB.php'</script>";
          }
      ?>
0
Comment
Question by:corterp
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:GreatGerm
ID: 35043084
What kind of order are you looking for in your db?  What is the structure of the table?

Usually, the order in the table doesn't matter for functions since you can always just use ORDER BY on a SELECT query.
0
 

Author Comment

by:corterp
ID: 35043097
I do not want to set ORDER, i want it to read records from top to bottom so the last record entered is the last record read but when records are being added they are at times added into the database above other records, not on the bottom.

What I am building is a playlist and I want it to be in the order I choose.

Thank you in advance.

0
 

Author Comment

by:corterp
ID: 35043172
One thing I forgot to mention when I add records.  The first set appear on the bottom of the database as they should, any items added after this are placed on top and bottom of that first set.

Thanks again.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 17

Accepted Solution

by:
GreatGerm earned 500 total points
ID: 35043230
There really is no top or bottom in a table like you're thinking.  Even if you're viewing your table in something like phpMyAdmin it will show you a SELECT with ORDER BY the primary.  What you can do is add an index that auto-increments and make it the primary.  By default, SELECT queries will return data in ascending order of the primary.
0
 

Author Comment

by:corterp
ID: 35043310
That worked.

Thank you for your help.  I have no problem working with databases in VB but for some reason I was completely lost here.  Everything is working great now.
0
 

Author Closing Comment

by:corterp
ID: 35043313
Great Help
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question