Connecting Tech Pros Worldwide Forums | Help | Site Map

UPDATE query not working correcty

monkeyjunky's Avatar
Newbie
 
Join Date: Aug 2007
Location: alabama
Posts: 2
#1: Aug 13 '07
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:
Expand|Select|Wrap|Line Numbers
  1. for ($x = 0; $x < count($twentyArray); $x++) {
  2.     $sql2 = ("UPDATE $db.boxes 
  3.            SET box_num = '".$twenty."'
  4.             WHERE id = '".$twentyArray[x]."'");           
  5.            $res2 = mysql_query($sql2) or die(mysql_error($mysql));
  6.         $twenty += 1;
  7.     }
  8.  
  9.     for ($y = 0; $y < count($thirtyArray); $y++) {
  10.     $sql3 = ("UPDATE $db.boxes 
  11.            SET box_num = '".$thirty."'
  12.             WHERE id = '".$thirtyArray[y]."'");           
  13.            $res3 = mysql_query($sql3) or die(mysql_error($mysql));
  14.         $thirty += 1;
  15.     }
  16.  
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's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#2: Aug 13 '07

re: UPDATE query not working correcty


Expand|Select|Wrap|Line Numbers
  1. WHERE id = '".$twentyArray[x]."'"); 
You have missed the '$' off your 'x' and 'y' variables
monkeyjunky's Avatar
Newbie
 
Join Date: Aug 2007
Location: alabama
Posts: 2
#3: Aug 13 '07

re: UPDATE query not working correcty


Quote:

Originally Posted by code green

Expand|Select|Wrap|Line Numbers
  1. WHERE id = '".$twentyArray[x]."'"); 
You have missed the '$' off your 'x' and 'y' variables

Holy Crap. I can't believe it.
Thanks.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Aug 13 '07

re: UPDATE query not working correcty


Merged duplicate threads.
Reply