UPDATE query not working correcty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkeyjunky
    New Member
    • Aug 2007
    • 2

    UPDATE query not working correcty

    I have written a database driven php program to keep up with inventory.
    I added a field labeled 'box_num' into the existing database. Each entry has it's own id labeled 'id'. Each entry has one of two possible types, '20' or '30'.
    My script runs two select queries, storing 'id' for each '20' entry in an array, and each '30' entry into a different array. I am then attempting to assign each entry a 'box_num' based upon its 'id' value. I am looping through the arrays as follows:
    Code:
    for ($x = 0; $x < count($twentyArray); $x++) {
        $sql2 = ("UPDATE $db.boxes 
               SET box_num = '".$twenty."'
                WHERE id = '".$twentyArray[x]."'");           
               $res2 = mysql_query($sql2) or die(mysql_error($mysql));
            $twenty += 1;
        }
    
        for ($y = 0; $y < count($thirtyArray); $y++) {
        $sql3 = ("UPDATE $db.boxes 
               SET box_num = '".$thirty."'
                WHERE id = '".$thirtyArray[y]."'");           
               $res3 = mysql_query($sql3) or die(mysql_error($mysql));
            $thirty += 1;
        }
    I am not getting any errors, but the query acts as if the WHERE clause is not even there. It assigns the same number to each entry. Through my debugging efforts I have deduced that:
    1) The $twentyArray and $thirtyArray are being populated
    2) The $twenty and $thirty values begin at 2000 and 3000, respectively. The loops are iterating through each variable, because when I echo the value of each variable at the end of the script, it prints the value increased by the correct number of entries for the specific type.
    I can only assume that the problem is with my WHERE clause.
    Does this make sense?
    Can anyone help me?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    WHERE id = '".$twentyArray[x]."'");
    You have missed the '$' off your 'x' and 'y' variables

    Comment

    • monkeyjunky
      New Member
      • Aug 2007
      • 2

      #3
      Originally posted by code green
      Code:
      WHERE id = '".$twentyArray[x]."'");
      You have missed the '$' off your 'x' and 'y' variables
      Holy Crap. I can't believe it.
      Thanks.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Merged duplicate threads.

        Comment

        Working...