www.webdeveloper.com
Results 1 to 8 of 8

Thread: INSERT is adding extra empty row

  1. #1
    Join Date
    Mar 2005
    Location
    Spencer, WV
    Posts
    27

    INSERT is adding extra empty row

    I'm working on a cart progaram, early stages. I have several areas where i echo variable results, etc... and I track it through phpmyadmin

    The INSERT is adding an empty row and indexing seems to be inconsistent. I would like to add just the one row (at the end/bottom).

    My code is as follows:

    I post an invcode from a order button click. this works!
    I retrieve data from the corresponding record in table rockid. this works!
    The insert into usercart1 never shows more than 1 row affected! yet,
    on the first submission (i.e., delete all usercart records in phpmyadmin prior to submitting) I get a blank row then the data that was sent.
    my second submission inserts the data between the blank and first filled row (not sure if this is a consistent trend) I think it puts a new submitted invcode row at the top sometimes???)

    Anyway I would like one row inserted at a time from top to bottom

    PHP Code:
    //** Performing SQL query (single item sent from order button click) **
    $sentcode $_POST["invcode"];
    $query "SELECT picpath, itemcode, itemdesc, itemqty, itemprice FROM rockid WHERE itemcode='$sentcode'";
    $result mysql_query($query) or die('Query failed:(here!) ' mysql_error());


    //-- Writing results to $datadata array --
    $fieldcounter 0;
    while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
       foreach (
    $line as $itdata) {
    //echo "<br />";
    //   echo $itdata;
       
    $datadata[$fieldcounter] = $itdata;
       
    $fieldcounter $fieldcounter 1;
       }
    }
    echo 
    "<p></p>";
    echo 
    "this is datadata array of sentitem";
    echo 
    "<br />";
       echo 
    $datadata[0];
       echo 
    $datadata[1];
       echo 
    $datadata[2];
       echo 
    $datadata[3];
       echo 
    $datadata[4];
    echo 
    "<p></p>";

    //** Add item to usercart1 **
       
    $query "INSERT INTO usercart1 ( `picpath` , `itemcode` , `itemdesc` , `itemqty` , `itemprice`  ) VALUES ('$datadata[0]', '$datadata[1]', '$datadata[2]', '$datadata[3]', '$datadata[4]')";
    //echo "Updated records: %d\n", mysql_affected_rows();
       
    $result mysql_query($query) or die('Query failed:(or here!) ' mysql_error());

    echo 
    "<br />";

    //** Performing SQL query (all usercart1) **
    $query "SELECT * FROM usercart1";
    $result mysql_query($query) or die('Query failed: ' mysql_error());

    //-- Writing results to $itemdata array --
    echo "this is itemdata array of usercart";
    echo 
    "<br />";
    $rowcounter 0;
    $fieldcounter 0;
    while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
       foreach (
    $line as $cartdata) {
    //echo "<br />";
       
    echo $cartdata;
       
    $itemdata[$fieldcounter][$rowcounter] = $cartdata;
       
    $fieldcounter $fieldcounter 1;
       }
    echo 
    "<br />";
    $fieldcounter 0;
    $rowcounter $rowcounter 1;


    Thanks for the help!

  2. #2
    Join Date
    Jun 2003
    Location
    Indiana, USA
    Posts
    1,138
    I don't know much about PHP and MySQL but you have the following code;
    Code:
    //** Add item to usercart1 **
       $query = "INSERT INTO usercart1 ( `picpath` , `itemcode` , `itemdesc` , `itemqty` , `itemprice`  ) VALUES ('$datadata[0]', '$datadata[1]', '$datadata[2]', '$datadata[3]', '$datadata[4]')";
    //echo "Updated records: %d\n", mysql_affected_rows();
       $result = mysql_query($query) or die('Query failed:(or here!) ' . mysql_error());
    What I see is you having $query equal to the insert string. Then you have $result = mysql_query($query) which looks to me as having result then re-insert the information. I may be wrong, but this is what caught my eye.
    Chris.Martz.me
    Some things Man was never meant to know. For everything else, there's Google.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    I don't see anything there that would cause the insert statement to process more than once, and I don't see anything in the insert statement that would cause more than one row to be inserted.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2005
    Location
    Spencer, WV
    Posts
    27
    I have more info to chew on!

    I have IE, Firefox and Opera browsers.

    The program works just fine in IE and Opera (i.e., NO EXTRA BLANK ROWS INSERTED)

    I end the page with a SELECT * TABLE usercart1 query and echo. When used in conjuction with a Prior Delete:

    PHP Code:
    //** delete blank recors item to usercart1 **
       
    $query "DELETE FROM usercart1 WHERE  itemcode=''";
    //echo "Updated records: %d\n", mysql_affected_rows();
       
    $result mysql_query($query) or die('Query failed:(or here!) ' mysql_error()); 
    it shows that Firefox is ending the page showing me the usercart1 table and no extra blank rows, yet if i go directly to myphpadmin; THERE IT IS, AN EXTRA ROW OF BLANK INFO!

    Obviously a Firefox issue, I THINK?

    Thanks again

  5. #5
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    how can you say it's a firefox issue when the page isn't sent to the browser until after it gets processed.

    Also, I'm thinking this isn't all of your code.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  6. #6
    Join Date
    Mar 2005
    Location
    Spencer, WV
    Posts
    27
    Well I was thinking it was a Firefox issue in the sense that:

    1) This does not occur at all with IE and Opera, and

    2) The Form action POST and corresponding data (in this case I only have one form field set with the name of invcode and I set that = to $sentcode (see below). So when any of the order buttons is clicked the value of form 'invcode' is first updated with that products information, then the form is processed, or sent to the order.php page. This page uses the (only one POST item sent) to update my cart TABLE usercart1, this is done with an INSERT.

    Therefore I think some freaky code has been overlooked.
    Or between when the order button is clicked and when my Firefox Browser is submitting the POST form data to be proccessed in order.php.

    Remember all three browsers do this same thing.

    Order of events:
    1) Click order button
    2) Update hidden formfield 'invcode' with the product code of the item selected.
    3) Form action to order.php

    From below:
    First SELECT the record/row data FROM rockid that corresponds with the item $sentcode.
    Then write those results to an array and echo that out.
    Then SELECT * FROM usercart1 and echo that out
    Then a DELETE FROM usercart1 WHERE $sentdata ="" (comment this one out sometimes as I check)
    Then INSERT INTO usercart1 ($sentdata info)
    Then SELECT * FROM usercart1
    Then turn that into an array that I use to display whats in the cart
    Then at the end I re-echo usercart1

    With Firefox: (with DELETE commented out)
    first item click:
    All the code is processed and both usercart1 echos appear to be complete and correct (first echo is empty and second has one item).
    In myphpadmin there is a blank extra row. (either the usercart1 querys did not detect this or it was put there after the order.php page was run.
    second item click:
    All the code is processed and both usercart1 echos show the extra blank line.
    In myphpadmin there is a blank extra row, but just one not two. (there is always just one extra row never more and if i delete it out with myphpadmin the next click will just add another one!

    The DELETE statement does help me keep things in line for what I want to do, but seems very uneccessary!

    With IE and OPERA: (with DELETE commented out)
    NEVER SEE AN EXTRA ROW AT ANY TIME WITH DELETE COMMENTED OUT OR NOT

    (over the next few days i'll try to run some simple pages with INSERTS in other tables and see what happens)

    Thanks again to everyone checking this out!

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Well here's your problem:

    Order of events:
    1) Click order button
    2) Update hidden formfield 'invcode' with the product code of the item selected.
    3) Form action to order.php
    How are you doing #2? Using JS? That's probably the culprit. if you do a var_dump on the item as it gets sent, you'd probably find the issue (I've had cases where FF adds an extra , to some things.) So this can be a FF issue assuming #2 is done via JS. But you should look at that for the problem.

    Sorry if you mentioned that before, I must have missed it.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  8. #8
    Join Date
    Jul 2004
    Posts
    7
    Hi everyone,
    I seem to have the same exact problem than that guy. I'm running a CMS engine, and it only happens on 1 of my query pages. Here are the codes :

    First page :

    PHP Code:
    echo "<body onload=\"swapNav('$pageArray[$current_content]');\">";

    if(
    checkBan() == false){
        include 
    "template/_main.php";
    }
    else{
        include 
    "template/_banned.php";

    If the user isn't banned :

    PHP Code:
    <?
    checkUser
    ('4');
            if(!isset(
    $_GET['id'])){
                echo 
    "
                    You must select an user to ban.
                "
    ;
            }
            else{
                if(!isset(
    $_GET['confirmed'])){
                    echo 
    "
                        Are you sure you want to ban this user? <a href=\"index.php?content=admin&view=users&action=ban&id=
    $_GET[id]&confirmed=1\">Yes</a> <a href=\"index.php?content=admin&view=users&action=list\">No</a>
                    "
    ;
                }
                else{
                    
    $userSql mysql_query("SELECT last_ip FROM ".$table_suffixe."users WHERE user_id = ".$_GET['id']);
                    
    $userRow mysql_fetch_array($userSql);
                    
    $userInsert = new mySQL;
                    
    $userArray = array("user_id" => $_GET['id'], "ip" => $userRow['last_ip']);
                    
    $userInsert->sqlInsert('bans',$userArray);
                    echo 
    "
                        The user has been banned. You will now be redirected to the users list.
                        <meta http-equiv=\"refresh\" content=\"2;url=index.php?content=admin&view=panel\"> 
                    "
    ;
                }
            }
    ?>
    And the query function :

    PHP Code:
            function sqlInsert($insertTable,$insertArray){
                
    $table_suffixe 'dvengine_';
                
    $insertKeys implode(",",array_keys($insertArray));
                if((
    mysql_query("SELECT $insertKeys FROM ".$table_suffixe.$insertTable) == true) && isset($insertArray)){
                    foreach(
    $insertArray as $key => $value){
                        
    $insertArray[$key] = nl2br(addslashes($value));
                    }
                    
    $insertValues implode("','",$insertArray);
                    
    $insertValues "'".$insertValues."'"
                    
    mysql_query("INSERT INTO ".$table_suffixe.$insertTable." (".$insertKeys.") VALUES (".$insertValues.")");
                }
            } 
    Anyone has an idea about this? It most likely isn't the background-image issue because I actually don't load any background picture ...

    Best regards,

    Francis B.
    Last edited by Francky683; 06-02-2006 at 04:42 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles