Odd PHP/MySQL Error - This Should Not Be Happening

Discussion in 'PHP' started by zac439, Nov 13, 2009.

  1. #1
    I have an application I'm building. This specific part of the application will:

    1. Connect to proper database/table
    2. Check to see if field exists
    3. If so, report success and update the field to a new value.
    4. If not, echo out an error message claiming the field doesn't exist.

    
    $query = "SELECT * FROM table";
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result)){
         if ($myVariable== $row["fieldToChange"]){
              echo "Success Message";
                 
              $sql="UPDATE table SET fieldToChange= 'newValue' WHERE (id = $row[id])";
                
           if (!mysql_query($sql)){die('Error: ' . mysql_error());}
               }
            }
    
    if (empty($sql)) {
                echo "The error message.";
            }
    
    PHP:
    By all means, this should work. But the odd part is that the fieldToChange actually gets updated in the database, but nothing is output to the browser (even though I echo content BEFORE updating the record).

    Also strange is the fact that I get "The error message." despite $sql being populated with data.

    Ideas, anyone? I've tried just about everything. I'm running PHP 5.
     
    zac439, Nov 13, 2009 IP
  2. travelmoth

    travelmoth Active Member

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    write some html outside the php code..........like


    <html>
    <body>
    fafa
    <?php
    you php code.
    ?>
    </body>
    </html>
     
    travelmoth, Nov 13, 2009 IP
  3. zac439

    zac439 Notable Member

    Messages:
    3,074
    Likes Received:
    214
    Best Answers:
    0
    Trophy Points:
    260
    #3
    For what purpose? I only showed the PHP aspect of my code, I already have a full HTML template that this is being inserted into.

    I should also mention I found another clue:

    When the record I'm updating is added by me manually via PHPMyAdmin, the entire thing works. When the field I'm updating is added by my PHP code, it acts strangely as I have already described.

    Could this perhaps indicate a type mismatch? The string in the database is just numbers and letters, and the string I'm checking it against comes from the query string in the URL (via $_SERVER['QUERY_STRING'])

    EDIT:

    I rearranged my code, and this is really blowing my mind:
    Nothing is sent out to the browser, YET STILL the record is being updated successfully. How is this possible?
     
    Last edited: Nov 13, 2009
    zac439, Nov 13, 2009 IP