Solved

Insert Into adds records sporadically within the database

Posted on 2011-03-05
6
331 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Making Table Thru ASP Response.write 5 19
Two decimal 5 28
Finding Where Clause Value in SQL Views and SP 21 40
Get data from two MySQL tables 6 25
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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)

749 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