?
Solved

Insert Into adds records sporadically within the database

Posted on 2011-03-05
6
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 17

Accepted Solution

by:
GreatGerm earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: blockā€¦
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

765 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