Solved

Insert Into adds records sporadically within the database

Posted on 2011-03-05
6
332 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

726 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