Solved

Insert Into adds records sporadically within the database

Posted on 2011-03-05
6
323 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now